Best way of saving NSArray strings to a plist?

Discussion in 'iOS Development' started by Chicken, May 7, 2009.

  1. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Well the title explains it, how can I save NSArray strings to a plist?
  2. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Code:
    NSMutableDictionary *dictionary = [[NSDictionary alloc] init];
    [dictionary setObject:theArray forKey:yourKey];
    [dictionary writeToFile:@"/somepath/blah.plist" atomically:YES];
    [dictionary release];
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Gojohnnyboi's way works. Even with the mismatch in the first line.

    Alternatively, try using [NSDictionary dictionaryWithObjectsAndKeys] to create an autoreleased dictionary. Write it the same way. You'll be reducing the entire thing down to 2 lines.

    Alternatively, try using [[NSDictionary dictionaryWith...] writeToFile:atomically:]; to condense it to one line.
  4. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    NSArray has
    Code:
     - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
    Wonder, Why needed NSDirectory for it?
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    OK so here is my code

    Code:
    NSMutableDictionary *savedItems = [[NSMutableDictionary alloc] init];
    		[savedItems setObject:itemsArray forKey:@"Saved"];
    		[savedItems writeToFile:@"/SavedItems.plist" atomically:YES];
    
    I think that's right, but it's not saving. I think it's something wrong with my plist, so I have attached it.

    Attached Files:

  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    It's because you're saving it to root. You can't write outside of the sandbox from an SDK app. You can only really write to your own Documents directory.
  7. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    To save the images in Cracked Screen, i use this:
    Code:
    	homeDirectoryPath = NSHomeDirectory();
    	unexpandedPath = [homeDirectoryPath stringByAppendingString:@"/Pictures/"];
    	folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];
    	unexpandedImagePath = [folderPath stringByAppendingString:@"/image.png"];
    	imagePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]];
    
    And then to save the image, i first check if the /Pictures folder is present, before i create a new folder (Gives a few errors if you try to create a folder that already that exists). And then finally, i write the image to file.
    Code:
    	if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL]) {
    		[[NSFileManager defaultManager] createDirectoryAtPath:folderPath attributes:nil];
    	}
    	NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(crackedImage)];
    	[imageData writeToFile:imagePath atomically:YES];
    
    Hope this helps you, it should give you an idea of how and where to save a file in your NSHomeDirectory (The .app folder) Although i suggest you use NSDocumentDirectory for an application on the App Store, for backup purposes. (I don't think it backs up the .app folder).

    Credit to Skylar for a lot of help with this code. It works like a charm.
  8. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    OK thanks for the code I'll go and try it now.
    ------------------double post merged------------------
    Also I knew that, but I guessed that when you wrote / in an SDK app it took it as the .app folder.

    Well that was a stupid mistake.
  9. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    Code:
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    that's how apple suggests doing it. this is the equiv:

    Code:
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent@"Documents"];
  10. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    You can't write there either.

Share This Page