Amazon.com Widgets 19.8
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 01, 2014, 07:22:00 PM
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
| |-+  Program Examples
| | |-+  Chapter 19
| | | |-+  19.8
Pages: [1]   Go Down
Print
Author Topic: 19.8  (Read 2792 times)
mdeh
Full Member
***
Posts: 166






« on: March 05, 2009, 03:29:14 PM »

Code: (Objective-C)
#import "Foo.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *myFoo = [ [ Foo alloc] init];
Foo *myFoo2;

[myFoo setStrVal:@"This is the string"];
[myFoo setIntVal: 12345];
[myFoo setFloatVal: 98.6];

[NSKeyedArchiver archiveRootObject: myFoo toFile: @"foo.arch"];

myFoo2 = [ NSKeyedUnarchiver unarchiveObjectWithFile: @"foo.arch"];

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

[myFoo release];
[pool drain];
    return 0;
}
Logged
elTuga
Newbie
*
Posts: 3






« Reply #1 on: August 20, 2010, 07:00:17 PM »

mine is not working...help anyone?


"Foo.h"

#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


"Foo.m"

#import "Foo.h"
#import <Foundation/Foundation.h>

@implementation Foo

@synthesize strVal, intVal, floatVal;

- (void) encodeWithCoder: (NSCoder *) encoder
{
   [encoder encodeObject:strVal forKey: @"FooStrVal"];
   [encoder encodeInt:intVal forKey: @"FooIntVal"];
   [encoder encodeFloat:floatVal forKey: @"FooFloatVal"];
}

- (id) initWithCoder: (NSCoder *) decoder
{
   strVal = [[decoder decodeObjectForKey: @"FooStrVal"] retain];
   intVal = [[decoder decodeIntForKey: @"FooIntVal"] retain];
   floatVal = [[decoder decodeFloatForKey: @"FooFloatVal"]retain];
   return self;
}

@end


"Test Program"

#import <Foundation/Foundation.h>
#import "Foo.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   Foo * myFoo1 = [[Foo alloc] init];
   Foo * myFoo2;
   
   myFoo1.strVal = @"This is the string";
   myFoo1.intVal = 12345;
   myFoo1.floatVal = 98.6;
   
   [NSKeyedArchiver archiveRootObject:myFoo1 toFile: @"myfoo1.arch"];
   
   NSLog (@"myFoo1:");
   NSLog (@"%@, %@, %@", myFoo1.strVal, myFoo1.intVal, myFoo1.floatVal);
   
   
   
   myFoo2 = [NSKeyedUnarchiver unarchiveObjectWithFile: @"myfoo1.arch"];
   
   
   NSLog (@"myFoo2:");
   NSLog (@"%@, %@, %@", myFoo2.strVal, myFoo2.intVal, myFoo2.floatVal);
   
   [myFoo1 release];
   

    [pool drain];
    return 0;
}

if "Foo.m" i get the following errors:

On the line
   intVal = [[decoder decodeIntForKey: @"FooIntVal"] retain];
i get the error "invalid receiver type 'int'"

On the line
   floatVal = [[decoder decodeFloatForKey: @"FooFloatVal"]retain];
i get the error "invalid receiver type 'float'"

Could anyone pls help me with this?

Thanks

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #2 on: August 20, 2010, 08:38:46 PM »

You can't retain basic data types like floats or ints--only objects.

Cheers,

Steve
Logged
elTuga
Newbie
*
Posts: 3






« Reply #3 on: August 21, 2010, 06:19:13 AM »

That's it! Thank you so much for the quick reply!! But now i get and execution error: “EXC_BAD_ACCESS”. Any idea why?
Logged
elTuga
Newbie
*
Posts: 3






« Reply #4 on: August 21, 2010, 06:31:51 AM »

I got it. It was the '@"%@, %@, %@"' sequence in NSLog! Tried the '@"%@\n%i\n%g"' instead and it worked.

Shouldn't the %@ display any kind of variable?

Many thanks
« Last Edit: August 21, 2010, 09:57:42 AM by elTuga » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #5 on: August 28, 2010, 09:22:02 PM »

%@ can only be used to display objects, not basic data types like int's, float's, double's, etc.

Cheers,

Steve
Logged
mrmonkey
Newbie
*
Posts: 2






« Reply #6 on: November 24, 2010, 01:47:25 PM »

I'm not exactly sure why you need to retain the strVal when you encode it.  

When I tried altering the property of it to "retain", it did not work as I expected.  Why is that?  Does the "retain" only deal when it's being set?
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.