NSMutableDictionary WriteToFile Always Fail

Discussion in 'iOS Programming' started by roeik, Jan 13, 2010.

  1. macrumors member

    Joined:
    Dec 25, 2008
    #1
    Hi,

    i am having troubles writing an NSMutableDictionary

    List is
    Code:
    list = [[NSMutableDictionary dictionaryWithCapacity:3500] retain];
    
    // this is how I add custom objects...
    [list setObject:newEntry forKey:key];
    
    
    in the custom object, I adopt the NSCoding protocol lie this:

    in the .h file:
    Code:
    @interface Entry : NSObject <NSCoding> {
    
    in the implementation file:
    Code:
    - (id)initWithCoder:(NSCoder *)decoder {
    	if (self = [super init]) {
    		shape = [[decoder decodeObjectForKey:@"shape"] retain];
    
    
    		sizeIndex = [decoder decodeIntForKey:@"sizeIndex"];
    		price = [decoder decodeFloatForKey:@"price"];retain];		
    	}
    	return self;
    }
    
    - (void)encodeWithCoder:(NSCoder *)encoder {
    	    [encoder encodeObject:shape forKey:@"shape"];
    		[encoder encodeInt:sizeIndex forKey:@"sizeIndex"];
    		[encoder encodeFloat:price forKey:@"price"];
    
    }
    
    I write to file like this:
    Code:
    if ([list writeToFile:filePath atomically:YES]) NSLog(@"success");
    else NSLog(@"failed");
    But it keeps failing. Any idea why?

    Thanks.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What is filePath? You can't write to most areas on the phone...
     
  3. thread starter macrumors member

    Joined:
    Dec 25, 2008
    #3
    I think the file path is fine.

    I trying to save it on the documents directory like this:

    Code:
    			
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *filePath = [NSString stringWithFormat:@"%@/list.plist",[paths objectAtIndex:0]];
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That does look like it should create a valid path but the second line is far from the suggested/correct Cocoa way to do that. Look at the NSString "Working with Paths" methods.
     
  5. thread starter macrumors member

    Joined:
    Dec 25, 2008
    #5
    ok, I also tried to use this sytax:

    Code:
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"raplist.plist"];
    
    but it doesn't work either... any other ideas?
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    As per the NSDictionary documentation you simply can't do what you are trying to do. Implementing NSCoding has no effect on this. I suggest you read the documentation more carefully as the documentation for writeToFile:atomically: says:

    "This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list."
     
  7. thread starter macrumors member

    Joined:
    Dec 25, 2008
    #7
    is there another way then I can save a dictionary object with customized object?

    Worst come to worst I can get rid of my class and store all the variables in an array.

    But just curious of there is other way to do it?

    Thanks.
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Given that you have already found NSCoding I suggest you open the documentation and look at the relevant associated classes that can deal with coding an object for storage in a file. I'm not hear to act as a lookup into the documentation for you. The ability to use the documentation is a key programming skill.
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Yes. The Property List Programming Guide describes how this might be possible.
     
  10. macrumors newbie

    Joined:
    Feb 2, 2010
    Location:
    Cali
    #10
    <-- Same issue

    I'm having the exact same original issue. My understanding (from reading the documentation) was that conforming to NSCoding protocol meant your object could be written to a plist through a mutabledictionary.... no?
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    See my post above: you read the documentation wrong. If you convert all of your NSCoding compliant objects to NSData objects then it should work.
     
  12. macrumors newbie

    Joined:
    Feb 2, 2010
    Location:
    Cali
  13. macrumors newbie

    Joined:
    Nov 24, 2008
    #13
    NSMutableDictionary WriteToFile Always Fail

    You may also check the validity of NSDictionary object at the time of calling writeToFile. This is because, if NSDictionary object pointer is nil, the call writeToFile will simply fail, without crashing the program.
     
  14. macrumors newbie

    Joined:
    Dec 14, 2010
    Location:
    Singapore
    #14
    I found this thread very useful so...

    I was pretty much deep into my project when I decided to try to write my beep object structure to a plist which, of course, failed because we had ints, BOOLs and all kinds of goodies in the objects.

    So what I ended up doing was adding to each object two methods
    Code:
    - (id) initWithDictionary: (NSMutableDictionary *) newDictionary;
    
    - (NSMutableDictionary *) getDictionary;
    
    which handled the transformation between object and dictionary for me.

    This made saving and loading so much easier.
     
  15. macrumors newbie

    Joined:
    Mar 14, 2012
    #15
    actually, you can write to any place that has the right permissions.

    i don't know why it won't work for you...
     
  16. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    Look up the concept of application sandboxing and how it is implemented on iOS.
     
  17. macrumors newbie

    Joined:
    Mar 14, 2012
    #17
    Sorry, I forgot I was jailbroken which changes everything...
     
  18. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #18
    You've now resurrected this thread two springs in a row...

    It went dead in March 2011, you resurrected it in March 2012, it died again, you resurrected it again in May 2013.
     

Share This Page