Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 13, 2010, 09:57 AM   #1
roeik
macrumors member
 
Join Date: Dec 2008
NSMutableDictionary WriteToFile Always Fail

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.
roeik is offline   0 Reply With Quote
Old Jan 13, 2010, 10:07 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
What is filePath? You can't write to most areas on the phone...
robbieduncan is offline   0 Reply With Quote
Old Jan 13, 2010, 02:13 PM   #3
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
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]];
roeik is offline   0 Reply With Quote
Old Jan 13, 2010, 03:04 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by roeik View Post
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]];
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.
robbieduncan is offline   0 Reply With Quote
Old Jan 13, 2010, 03:29 PM   #5
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
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?
roeik is offline   0 Reply With Quote
Old Jan 13, 2010, 03:36 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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."
robbieduncan is offline   0 Reply With Quote
Old Jan 13, 2010, 04:30 PM   #7
roeik
Thread Starter
macrumors member
 
Join Date: Dec 2008
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.
roeik is offline   0 Reply With Quote
Old Jan 13, 2010, 04:32 PM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Jan 13, 2010, 04:33 PM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by roeik View Post
is there another way then I can save a dictionary object with customized object?
Yes. The Property List Programming Guide describes how this might be possible.
__________________
dejo is offline   0 Reply With Quote
Old Feb 2, 2010, 05:31 PM   #10
jlobe
macrumors newbie
 
Join Date: Feb 2010
Location: Cali
<-- 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?
jlobe is offline   0 Reply With Quote
Old Feb 3, 2010, 03:52 AM   #11
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by jlobe View Post
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?
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.
robbieduncan is offline   0 Reply With Quote
Old Feb 5, 2010, 01:23 AM   #12
jlobe
macrumors newbie
 
Join Date: Feb 2010
Location: Cali
got it, thanks
jlobe is offline   0 Reply With Quote
Old Dec 8, 2010, 07:17 AM   #13
chaithrika
macrumors newbie
 
Join Date: Nov 2008
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.
chaithrika is offline   0 Reply With Quote
Old Mar 3, 2011, 06:51 PM   #14
calistra
macrumors newbie
 
Join Date: Dec 2010
Location: Singapore
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.
calistra is offline   0 Reply With Quote
Old Mar 14, 2012, 04:22 PM   #15
Jailpod
macrumors newbie
 
Join Date: Mar 2012
Quote:
Originally Posted by robbieduncan View Post
What is filePath? You can't write to most areas on the phone...
actually, you can write to any place that has the right permissions.

i don't know why it won't work for you...
Jailpod is offline   -1 Reply With Quote
Old Mar 19, 2012, 01:59 PM   #16
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Jailpod View Post
actually, you can write to any place that has the right permissions.

i don't know why it won't work for you...
Look up the concept of application sandboxing and how it is implemented on iOS.
robbieduncan is offline   1 Reply With Quote
Old May 3, 2013, 06:45 PM   #17
Jailpod
macrumors newbie
 
Join Date: Mar 2012
Quote:
Originally Posted by robbieduncan View Post
Look up the concept of application sandboxing and how it is implemented on iOS.
Sorry, I forgot I was jailbroken which changes everything...
Jailpod is offline   0 Reply With Quote
Old May 3, 2013, 07:24 PM   #18
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by Jailpod View Post
Sorry, I forgot I was jailbroken which changes everything...
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.
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Modify a key-value pair of an NSMutableDictionary that is represented by an NSData moonman239 iPhone/iPad Programming 1 May 2, 2014 01:56 PM
Windows Sp3 Fail!+AppleMNT.sys Fail...Help! Liquigladiators Windows, Linux & Others on the Mac 6 Jun 9, 2013 05:37 PM
NSMutableDictionary and a button to send changes Carll iPhone/iPad Programming 12 Jan 12, 2013 08:19 AM
Resolved: Fetch object in NSMutableDictionary inside NSMutable lasash iPhone/iPad Programming 22 Oct 9, 2012 09:10 PM
(NSArray)writeToFile: issue mdeh Mac Programming 3 Jul 15, 2012 06:38 PM

Forum Jump

All times are GMT -5. The time now is 07:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC