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

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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

macrumors member
Original poster
Dec 25, 2008
80
0
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]];
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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."
 

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

jlobe

macrumors newbie
Feb 2, 2010
9
0
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

chaithrika

macrumors newbie
Nov 24, 2008
7
0
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.
 

calistra

macrumors newbie
Dec 14, 2010
12
0
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.