Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
I have an NSOpenPanel, and I want to save all the filenames in an array and save it to my users defaults. Here is my code it works fine but, it saves the filename perfectly but when the user selects a new file it removes the old filename. So the array always has one object instead of multiple.

Can someone help me?

Code:
  NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
                [randomSelection addObject:filename];
                
                
                NSArray *removeablearray = [randomSelection mutableCopy];
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:removeablearray] forKey:@"remove"];
                NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
                NSLog(@"array TESTING:: %@", archivedArrayR);
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Repost your code and put a detailed comment about what each line is doing. Describe each step, and what the value of any variables are afterward.

After you do so, if there is a line or lines that are ambiguous to you, or you still don't understand why you end up with the behavior you describe, let us know.

-Lee

P.S. It's better to post a full method than a snippet.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
Your code makes a new, empty, array, sticks an object in it, makes a copy of the array, sticks the array in an archive, and then takes the array out of the archive.

At no point does the array ever have additional values put in it - just that first one. It seems to me that your issue is that you're making a new, empty, array every time and never populating it with old data.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I have an NSOpenPanel, and I want to save all the filenames in an array and save it to my users defaults. Here is my code it works fine but, it saves the filename perfectly but when the user selects a new file it removes the old filename. So the array always has one object instead of multiple.

Can someone help me?

Code:
  NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
                [randomSelection addObject:filename];
                
                
                NSArray *removeablearray = [randomSelection mutableCopy];
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:removeablearray] forKey:@"remove"];
                NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
                NSLog(@"array TESTING:: %@", archivedArrayR);

Looks like your code does exactly what you describe.
randomSelection is a mutable array with one object.
removeablearray is a mutable array with one object.
The userdefaults key "remove" is set to an array with one object.
You read that into archivedArrayR and get an array with one object.
 

cbreak

macrumors member
Mar 22, 2008
42
0
You create an empty mutable array "randomSelection" and add one single thing. It is normal that it contains only one single thing.
 

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
Your code makes a new, empty, array, sticks an object in it, makes a copy of the array, sticks the array in an archive, and then takes the array out of the archive.

At no point does the array ever have additional values put in it - just that first one. It seems to me that your issue is that you're making a new, empty, array every time and never populating it with old data.

Thanks for your reply, any idea how I will have the array have multiple values?
 

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
By adding multiple things to it.
This is my code:

Code:
- (IBAction)addToTable:(id)sender {
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
    [oPanel setTitle:@"Choose Source"];
    [oPanel setAllowsMultipleSelection:NO];
    [oPanel setCanChooseDirectories:YES];
    [oPanel setCanCreateDirectories:YES];
    [oPanel setDirectoryURL:[NSURL fileURLWithPath:[@"~/User/Movies/" stringByExpandingTildeInPath]]];
    [oPanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        
      {
            if (result == NSFileHandlingPanelOKButton) {
                NSURL*  source = [[oPanel URLs] objectAtIndex:0];
                NSURL* SOUL;
                SOUL = [source copy];
                NSLog(@"Source is %@", SOUL);
                NSString *source2 = [SOUL path];
                
            NSURL *url = [[oPanel URLs] objectAtIndex:0];
                NSString *filename = [[url path] lastPathComponent];
                NSLog(@"%@",filename);
                NSImage *iconImage;
                iconImage = [[NSWorkspace sharedWorkspace] iconForFile:source2];
                
                NSDictionary *obj = @{@"image": iconImage = [[NSWorkspace sharedWorkspace] iconForFile:source2],
                                                                                                                      
                                      @"name": [filename stringByDeletingPathExtension]};
                [_tableContents addObject:obj];
                
                
                
                NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
                [randomSelection addObject:filename];
                
                
                NSArray *removeablearray = [randomSelection mutableCopy];
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:removeablearray] forKey:@"remove"];
                NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
                NSLog(@"array TESTING:: %@", archivedArrayR);
                
                
                
                NSArray *array = [_tableContents mutableCopy];
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"annotationKey"];
                NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                NSLog(@"array:: %@", archivedArray);
                [_tableView reloadData];
            }
            
            
            
            
        }
        
        
        
    }
     ];
    

}
 

LinusR

macrumors 6502
Jan 3, 2011
332
515
Thanks for your reply, any idea how I will have the array have multiple values?

I assume what you actually want to do is something like that:

Code:
NSMutableArray *randomSelection = [[NSMutableArray alloc] init]; // Do you need these?
[randomSelection addObject:filename];
                            
NSArray *removeablearray = [randomSelection mutableCopy]; // Or that?

// Added code

NSArray *loadadData = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
NSMutableArray *mutableLoadedData = [[NSMutableArray alloc] initWithArray:loadedData];
[mutableLoadedData addObject:filename];

//
                
[[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]mutableLoadedData[/B]] forKey:@"remove"];
NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
NSLog(@"array TESTING:: %@", archivedArrayR);

Try it that way, now it should work properly. Hopefully… ;-)
 
Last edited:

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
I assume what you actually want to do is something like that:

Code:
NSMutableArray *randomSelection = [[NSMutableArray alloc] init]; // Do you need these?
[randomSelection addObject:filename];
                            
NSArray *removeablearray = [randomSelection mutableCopy]; // Or that?

// Added code

NSArray *loadadData = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
NSMutableArray *mutableLoadedData = [[NSMutableArray alloc] initWithArray:loadedData];
[mutableLoadedData addObject:filename];

//
                
[[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]mutableLoadedData[/B]] forKey:@"remove"];
NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
NSLog(@"array TESTING:: %@", archivedArrayR);

Try it that way, now it should work properly. Hopefully… ;-)

Thanks it worked perfectly :)

But I have one last problem.
When I remove one cell from the table view. it appears to empty the whole array. I just want the object that was removed to be taken out of the array :)

This is my code:

Code:
- (IBAction)removeFromTable:(id)sender {
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes];
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];
    
    NSArray *array = [_tableContents mutableCopy];
    
    
    
    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection removeObjectsInArray:_tableContents];

    NSArray *removeablearray = [randomSelection mutableCopy];
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:removeablearray] forKey:@"remove"];
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"annotationKey"];
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];


}
 

LinusR

macrumors 6502
Jan 3, 2011
332
515
Thanks it worked perfectly :)

But I have one last problem.
When I remove one cell from the table view. it appears to empty the whole array. I just want the object that was removed to be taken out of the array :)

This is my code:

Code:
- (IBAction)removeFromTable:(id)sender {
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes];
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];
    
    NSArray *array = [_tableContents mutableCopy];
    
    
    
    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection removeObjectsInArray:_tableContents];

    NSArray *removeablearray = [randomSelection mutableCopy];
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:removeablearray] forKey:@"remove"];
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"annotationKey"];
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];


}

Have a look at what you're actually doing here: After removing the specific objects you create a mutable copy and try to do exactly the same again. That cannot work. Try it that way:

Code:
- (IBAction)removeFromTable:(id)sender
{
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes]; // [B](1)[/B]
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];
    
    // You don't need these:

    NSArray *array = [_tableContents mutableCopy]; // You don't need that
    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection removeObjectsInArray:_tableContents]; // You don't need that, you did that already at [B](1)[/B]

    NSArray *removeablearray = [randomSelection mutableCopy];

    //
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]_tableContents[/B]] forKey:@"remove"]; // Simply use what you've already done at [B](1)[/B]
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]_tableContents[/B]] forKey:@"annotationKey"]; // Why not take the array you have already altered?
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];
}

Hope this works as expected.
 
Last edited:

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
Have a look at what you're actually doing here: After removing the specific objects you create a mutable copy and try to do exactly the same again. That cannot work. Try it that way:

Code:
- (IBAction)removeFromTable:(id)sender
{
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes]; // [B](1)[/B]
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];
    
    // You don't need these:

    NSArray *array = [_tableContents mutableCopy]; // You don't need that
    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection removeObjectsInArray:_tableContents]; // You don't need that, you did that already at [B](1)[/B]

    NSArray *removeablearray = [randomSelection mutableCopy];

    //
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]_tableContents[/B]] forKey:@"remove"]; // Simply use what you've already done at [B](1)[/B]
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:[B]_tableContents[/B]] forKey:@"annotationKey"]; // Why not take the array you have already altered?
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];
}

Hope this works as expected.

Hi I used the code you just sent but I got this in my problem reporter...
Code:
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSArray initWithArray:range:copyItems:]: array argument is not an NSArray'


----------

Hi I used the code you just sent but I got this in my problem reporter...
Code:
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSArray initWithArray:range:copyItems:]: array argument is not an NSArray'

Also it worth knowing that I have the annotationkey to just store the nstableview data. While the remove key store the filenames. The annotationkey looks like this:
Code:
array:: (
        {
        image = "<NSImage 0x61000027e540 Size={32, 32} Reps=(\n    \"NSBitmapImageRep 0x6100002a1ce0 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000168d00\",\n    \"NSBitmapImageRep 0x6100002a5460 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165400\",\n    \"NSBitmapImageRep 0x6100002a5520 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165700\",\n    \"NSBitmapImageRep 0x6100002a5580 Size={512, 512} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165580\",\n    \"NSBitmapImageRep 0x6100002a55e0 Size={512, 512} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001642c0\",\n    \"NSBitmapImageRep 0x6100002a5640 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001657c0\",\n    \"NSBitmapImageRep 0x6100002a56a0 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000164bc0\",\n    \"NSBitmapImageRep 0x6100002a5700 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165880\",\n    \"NSBitmapImageRep 0x6100002a5760 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000168100\",\n    \"NSBitmapImageRep 0x6100002a57c0 Size={64, 64} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=64x64 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165280\",\n    \"NSBitmapImageRep 0x6100002a5820 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000167b00\",\n    \"NSBitmapImageRep 0x6100002a5880 Size={64, 64} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=64x64 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165040\",\n    \"NSBitmapImageRep 0x6100002a58e0 Size={16, 16} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=16x16 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000164980\",\n    \"NSBitmapImageRep 0x6100002a5940 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000166600\",\n    \"NSBitmapImageRep 0x6100002a59a0 Size={1024, 1024} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=1024x1024 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000164140\"\n)>";
        name = 3;
    },
        {
        image = "<NSImage 0x610000460f40 Size={32, 32} Reps=(\n    \"NSBitmapImageRep 0x6100002a5a00 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000167c80\",\n    \"NSBitmapImageRep 0x6100002a5a60 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000167ec0\",\n    \"NSBitmapImageRep 0x6100002a5ac0 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000164740\",\n    \"NSBitmapImageRep 0x6100002a5b20 Size={512, 512} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000163a80\",\n    \"NSBitmapImageRep 0x6100002a5b80 Size={512, 512} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000166a80\",\n    \"NSBitmapImageRep 0x6100002a5be0 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000165640\",\n    \"NSBitmapImageRep 0x6100002a5c40 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001681c0\",\n    \"NSBitmapImageRep 0x6100002a5ca0 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001666c0\",\n    \"NSBitmapImageRep 0x6100002a5d00 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000168b80\",\n    \"NSBitmapImageRep 0x6100002a5d60 Size={64, 64} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=64x64 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001660c0\",\n    \"NSBitmapImageRep 0x6100002a5dc0 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000167500\",\n    \"NSBitmapImageRep 0x6100002a5e20 Size={64, 64} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=64x64 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000167140\",\n    \"NSBitmapImageRep 0x6100002a5e80 Size={16, 16} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=16x16 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6100001693c0\",\n    \"NSBitmapImageRep 0x6100002a5ee0 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000169900\",\n    \"NSBitmapImageRep 0x6100002a5f40 Size={1024, 1024} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=1024x1024 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x610000169c00\"\n)>";
        name = 4;
    }
)

While the remove key looks like this:

Code:
array TESTING:: (
    file2,
    file1
)
 

LinusR

macrumors 6502
Jan 3, 2011
332
515
Oh yes, this is worth knowing ;-)

What line is the argument error referring to? There's no such method call within the lines you've posted…
And where do you get the specific filename? It isn't stored inside the table view data, is it?
 

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
Oh yes, this is worth knowing ;-)

What line is the argument error referring to? There's no such method call within the lines you've posted…
And where do you get the specific filename? It isn't stored inside the table view data, is it?

I don't know were the agreement error is referring to sorry :(
The specific file name is taken from the open panel...

Once again here is all my code:
Code:
- (IBAction)addToTable:(id)sender {
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
    [oPanel setTitle:@"Choose Source"];
    [oPanel setAllowsMultipleSelection:NO];
    [oPanel setCanChooseDirectories:YES];
    [oPanel setCanCreateDirectories:YES];
    [oPanel setDirectoryURL:[NSURL fileURLWithPath:[@"~/User/Movies/" stringByExpandingTildeInPath]]];
    [oPanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
        
                {
            if (result == NSFileHandlingPanelOKButton) {
                NSURL*  source = [[oPanel URLs] objectAtIndex:0];
                NSURL* SOUL;
                SOUL = [source copy];
                NSLog(@"Source is %@", SOUL);
                NSString *source2 = [SOUL path];
                // Show path on textfield
                
                //Save path in plist file
            NSURL *url = [[oPanel URLs] objectAtIndex:0];
                NSString *filename = [[url path] lastPathComponent];
                NSLog(@"%@",filename);
                NSImage *iconImage;
                iconImage = [[NSWorkspace sharedWorkspace] iconForFile:source2];
                
                NSDictionary *obj = @{@"image": iconImage = [[NSWorkspace sharedWorkspace] iconForFile:source2],
                                                                                                                      
                                      @"name": [filename stringByDeletingPathExtension]};
                [_tableContents addObject:obj];
                
                
                
                NSMutableArray *randomSelection = [[NSMutableArray alloc] init]; // Do you need these?
                [randomSelection addObject:filename];
                
                NSArray *removeablearray = [randomSelection mutableCopy]; // Or that?
                
                // Added code
                
                NSArray *loadadData = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
                NSMutableArray *mutableLoadedData = [[NSMutableArray alloc] initWithArray:loadadData];
                [mutableLoadedData addObject:filename];
                
                //
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:mutableLoadedData] forKey:@"remove"];
                NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
                NSLog(@"array TESTING:: %@", archivedArrayR);
                
                NSArray *array = [_tableContents mutableCopy];
                
                [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"annotationKey"];
                NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                NSLog(@"array:: %@", archivedArray);
                [_tableView reloadData];
            }
            
            
            
            
        }
        
        
        
    }
     ];
    

}

- (IBAction)removeFromTable:(id)sender
{
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes]; // (1)
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];
    
    // You don't need these:
    
    NSArray *array = [_tableContents mutableCopy]; // You don't need that
    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection removeObjectsInArray:_tableContents]; // You don't need that, you did that already at (1)
    
    NSArray *removeablearray = [randomSelection mutableCopy];
    
    //
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:_tableContents] forKey:@"remove"]; // Simply use what you've already done at (1)
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);
    
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:_tableContents] forKey:@"annotationKey"]; // Why not take the array you have already altered?
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];
}


And this is the error
Code:
*** -[NSArray initWithArray:range:copyItems:]: array argument is not an NSArray
2013-12-15 17:25:53.242 DesktopTidy[7085:303] (
	0   CoreFoundation                      0x00007fff8aa6a41c __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x00007fff8fcfbe75 objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff8a955844 -[NSArray initWithArray:range:copyItems:] + 692
	3   DesktopTidy                         0x0000000100007931 __26-[AppDelegate addToTable:]_block_invoke + 1169
	4   AppKit                              0x00007fff8cf4aa76 -[NSSavePanel _didEndSheet:returnCode:contextInfo:] + 102
	5   AppKit                              0x00007fff8cc798c2 -[NSWindow endSheet:returnCode:] + 368
	6   AppKit                              0x00007fff8cf4cf06 -[NSSavePanel ok:] + 353
	7   AppKit                              0x00007fff8cb3e3d0 -[NSApplication sendAction:to:from:] + 327
	8   AppKit                              0x00007fff8cb3e24e -[NSControl sendAction:to:] + 86
	9   AppKit                              0x00007fff8cb8ad7d -[NSCell _sendActionFrom:] + 128
	10  AppKit                              0x00007fff8cb8a7cd -[NSButtonCell performClick:] + 558
	11  AppKit                              0x00007fff8cb54f90 -[NSButton performKeyEquivalent:] + 324
	12  AppKit                              0x00007fff8cb54c33 -[NSView _performKeyEquivalent:conditionally:] + 180
	13  AppKit                              0x00007fff8cb54e2d -[NSControl _performKeyEquivalent:conditionally:] + 126
	14  AppKit                              0x00007fff8cb54cf0 -[NSView performKeyEquivalent:] + 162
	15  AppKit                              0x00007fff8cb54c33 -[NSView _performKeyEquivalent:conditionally:] + 180
	16  AppKit                              0x00007fff8cb54cf0 -[NSView performKeyEquivalent:] + 162
	17  AppKit                              0x00007fff8cb54c33 -[NSView _performKeyEquivalent:conditionally:] + 180
	18  AppKit                              0x00007fff8cb54cf0 -[NSView performKeyEquivalent:] + 162
	19  AppKit                              0x00007fff8cb54c33 -[NSView _performKeyEquivalent:conditionally:] + 180
	20  AppKit                              0x00007fff8cb54b09 -[NSWindow performKeyEquivalent:] + 61
	21  AppKit                              0x00007fff8cf46d47 -[NSSavePanel performKeyEquivalent:] + 1247
	22  AppKit                              0x00007fff8cbc3d04 -[NSWindow keyDown:] + 30
	23  AppKit                              0x00007fff8cf46e83 -[NSSavePanel keyDown:] + 119
	24  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	25  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	26  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	27  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	28  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	29  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	30  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	31  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	32  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	33  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	34  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	35  AppKit                              0x00007fff8cb20d9a forwardMethod + 122
	36  FinderKit                           0x00007fff8ef0ee11 -[FI_TBrowserViewController(Actions) handleKeyDown:currentKey:] + 97
	37  FinderKit                           0x00007fff8ef46baf -[FI_TIconViewController handleKeyDown:currentKey:] + 53
	38  FinderKit                           0x00007fff8ef41a61 -[FI_TIconView keyDown:] + 128
	39  AppKit                              0x00007fff8cb2281b -[NSWindow sendEvent:] + 1843
	40  AppKit                              0x00007fff8cac3ca2 -[NSApplication sendEvent:] + 3395
	41  AppKit                              0x00007fff8c913a29 -[NSApplication run] + 646
	42  AppKit                              0x00007fff8c8fe803 NSApplicationMain + 940
	43  DesktopTidy                         0x000000010000aa22 main + 34
	44  libdyld.dylib                       0x00007fff971a75fd start + 1
)
 

LinusR

macrumors 6502
Jan 3, 2011
332
515
No problem, try this one:

Code:
- (IBAction)removeFromTable:(id)sender
{
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes];
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];

    // @"remove" has just filenames, so we remove objects at indexes "indexes" in the specific array:

    NSArray *loadedRemoves =  [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
    NSMutableArray *mutableLoadedRemoves = [[NSMutableArray alloc] initWithArray:loadedRemoves];
    [mutableLoadedRemoves removeObjectsAtIndexes:indexes];

    // …and now save the altered array with key @"remove" again
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:mutableLoadedArray] forKey:@"remove"];
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    //
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:_tableContents] forKey:@"annotationKey"];
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];
}

Now you remove exactly the indexes from your remove-Array as you did with your annotations. The key @"remove" saves the array with your filenames, @"annotationKey" saves the entire table view data.

The argument error occurs whilst using an NSSavePanel. Do you use any save panels?
 

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
No problem, try this one:

Code:
- (IBAction)removeFromTable:(id)sender
{
    NSIndexSet *indexes = [_tableView selectedRowIndexes];
    [_tableContents removeObjectsAtIndexes:indexes];
    [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationSlideDown];

    // @"remove" has just filenames, so we remove objects at indexes "indexes" in the specific array:

    NSArray *loadedRemoves =  [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]];
    NSMutableArray *mutableLoadedRemoves = [[NSMutableArray alloc] initWithArray:loadedRemoves];
    [mutableLoadedRemoves removeObjectsAtIndexes:indexes];

    // …and now save the altered array with key @"remove" again
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:mutableLoadedArray] forKey:@"remove"];
    NSArray *archivedArrayR = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"remove"]] ;
    NSLog(@"array TESTING:: %@", archivedArrayR);

    //
    
    [[NSUserDefaults standardUserDefaults] setObject: [NSKeyedArchiver archivedDataWithRootObject:_tableContents] forKey:@"annotationKey"];
    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"annotationKey"]] ;
    
    NSLog(@"array:: %@", archivedArray);
    [_tableView reloadData];
}

Now you remove exactly the indexes from your remove-Array as you did with your annotations. The key @"remove" saves the array with your filenames, @"annotationKey" saves the entire table view data.

The argument error occurs whilst using an NSSavePanel. Do you use any save panels?

Yes I do have a save panel, but I discarded the changes that I made in Xcode and it fixed it :) BTW the new code you sent me works perfectly. Thanks a lot for your help :)
 

LinusR

macrumors 6502
Jan 3, 2011
332
515
Yes I do have a save panel, but I discarded the changes that I made in Xcode and it fixed it :) BTW the new code you sent me works perfectly. Thanks a lot for your help :)

You're welcome, hope your project develops well :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.