Amazon.com Widgets Page 446 won't work.
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 08:24:30 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 19 - Archiving
| | | |-+ Page 446 won't work.
Pages: [1] Go Down
Print
Author Topic: Page 446 won't work. (Read 1230 times)
Pneumatikon
Newbie
*
Posts: 3






on: March 16, 2010, 06:42:41 AM

NSLog(@"%@\n%i\n%g", [myFoo2 strVal], [myFoo2 intVal], [myFoo2 floatVal]);

I've confirmed that strVal will NOT print. intVal and floatVal print just fine. I get the error message "Program received signal: "EXC_BAD_ACCESS""
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: March 16, 2010, 07:42:47 AM

That example does work.  Can you re-check your code or post it here?

Cheers,

Steve Kochan
Logged
Pneumatikon
Newbie
*
Posts: 3






Reply #2 on: March 17, 2010, 06:28:32 PM

Found the problem while replying: I forgot to retain strVal after decoding.

#import <Foundation/Foundation.h>

@interface Foo : NSObject <NSCoding>
{
   NSString *strVal;
   int intVal;
   float floatVal;
}

@property (copy, nonatomic) NSString *strVal;
@property int intVal;
@property float floatVal;

@end

@implementation Foo

@synthesize strVal, intVal, floatVal;

- (NSString *) stringValue {
   return strVal;
}

- (void) encodeWithCoder:(NSCoder *) coder {
   [coder encodeObject: strVal forKey: @"FoostrVal"];
   [coder encodeInt: intVal forKey: @"FoointVal"];
   [coder encodeFloat: floatVal forKey: @"FoofloatVal"];
}

- (id) initWithCoder:(NSCoder *) decoder {
   strVal = [decoder decodeObjectForKey: @"FoostrVal"];
   intVal = [decoder decodeIntForKey: @"FoointVal"];
   floatVal = [decoder decodeFloatForKey: @"FoofloatVal"];
   
   return self;
}

@end




int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   Foo *myFoo1 = [[Foo alloc] init];
   Foo *myFoo2;
   
   [myFoo1 setStrVal: @"This is the string"];
   [myFoo1 setIntVal: 12345];
   [myFoo1 setFloatVal: 98.6];
   
   [NSKeyedArchiver archiveRootObject: myFoo1 toFile: @"foo.arch"];
   
   myFoo2 = [NSKeyedUnarchiver unarchiveObjectWithFile: @"foo.arch"];
   
   NSLog(@"%@\n%i\n%g", [myFoo2 strVal],[myFoo2 intVal], [myFoo2 floatVal]);
   
   [myFoo1 release];
    [pool drain];
    return 0;

}
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.