Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-05-2010, 01:45 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Las Vegas
Posts: 9
JR2760 is on a distinguished road
Default Archiving and encoding objects help

Hello,
I'm trying to archive and save an instance of a (custom) object:

-(void) saveProfile{
ProfileObj *profile = [[ProfileObj alloc] init];
profile = Player[0];

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:profile forKey:kDatakey];
[archiver finishEncoding];
[data writeToFile:[self pathOfFile] atomically:YES];
[profile release];
[archiver release];
[data release];
}

for debugging purposes my ProfileObj currently only has one NSString variable that is being saved, and the code runs fine while that NSString is nil (currently loaded from Player[0]) but when the string has data the app crashes at [archiver encodeObject:profile forKey:kDatakey]; and i cannot figure out why or how to fix it. This is my first time ever trying to save data and any help would be greatly appreciated!


#pragma mark NSCoding
-(void) encodeWithCoder:(NSCoder *)encoder {
[encoder endodeObject:Name forKey: kNameKey];
}
-(id) initWithCoder:(NSCoder *)decoder {
if (self = [super init]){
self.Name = [decoder decodeObjectForKey:kNameKey];
}
return self;
}
#pragma mark -
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone{
ProfileObj *copy = [[[self class] allocWithZone: zone] init];
copy.Name = [[self.Name copyWithZone:zone] autorelease];
return copy;
}

This is some code within ProfileObj.h, the one string i'm trying to save is called Name.
JR2760 is offline   Reply With Quote
Old 09-05-2010, 10:11 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Is the .Name property declared as (nonatomic,retain) ? If it's not set to retain then the string will probably get dealloc'd before you go to use it. Do you always use the .Name property when changing the Name variable?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-05-2010, 01:20 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Las Vegas
Posts: 9
JR2760 is on a distinguished road
Default

thanks for replying

Yes the .Name is declared as (nonatomic, retain) . Currently i do always use .Name to change the variable. Would adding setter and getter methods fix my problem?

the part that's throwing me off is it crashes at [archiver encodeObject:profile forKey:kDatakey] only when .Name contains data.
JR2760 is offline   Reply With Quote
Old 09-05-2010, 04:50 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Las Vegas
Posts: 9
JR2760 is on a distinguished road
Default

I further narrowed down the error, commenting out the [encoder encodeObject:Name forKey: kNameKey]; line inside the encodeWithCoder: method results in no data saved while leaving it results in a crash regardless if .Name has a value or not.

any clue what i'm doing wrong?
JR2760 is offline   Reply With Quote
Old 09-05-2010, 06:08 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by JR2760 View Post
I further narrowed down the error, commenting out the [encoder encodeObject:Name forKey: kNameKey]; line inside the encodeWithCoder: method results in no data saved while leaving it results in a crash regardless if .Name has a value or not.

any clue what i'm doing wrong?
Sounds like the value of Name is bad at the time of encoding. Put a breakpoint at that line in your code and look at the Name instance variable. I'm guessing that you are assigning it without using property syntax, so it's not invoking the (retain) setter, and is getting released.

if you ever have code like this:

Code:
Name = [NSString stringWithFormat: @"blah blah blah %d", somevar];
Then Name won't be retained, and you will probably crash at some future date when you try to reference it.

Code:
self.Name = [NSString stringWithFormat: @"blah blah blah %d", somevar];
would invoke the setter, and thus the new value assigned to Name would be retained.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-05-2010, 07:14 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Las Vegas
Posts: 9
JR2760 is on a distinguished road
Default

thanks for the help,

I used a breakpoint and .Name DOES contain data at the time of encoding, but right after checking that i saw a typo, I put endodeObject: instead of encodeObject: , after fixing that everything works fine. God i feel silly

thanks for the help

Last edited by JR2760; 09-05-2010 at 07:24 PM.
JR2760 is offline   Reply With Quote
Old 09-05-2010, 07:24 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by JR2760 View Post
thanks for the help,

I used a breakpoint and .Name DOES contain data at the time of encoding, but right after checking that i saw a typo, I put endodeObject: instead of encodeObject, after fixing that everything works fine. God i feel silly

thanks for the help
Oops.

You really, really have to check for warnings when you compile. I daresay you got a warning when you first compiled it. The really bad thing about warnings is that they only show up the first time you compile. If you don't check them the first time, the warning isn't repeated until you change the file in which it occurs.


Duncan
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
archive, encode, iphone, object, save

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 339
10 members and 329 guests
bignoggins, carlandrews, flamingliquid, hzwegjxg, ilmman, linkmx, nadav@webtview.com, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0