dimanche 23 mars 2014

Implementing NSCoding protocol


Vote count:

0




I have asked a similar questions...

While I have made some progress, I still cant seem make it work.


Here is what I got uptill now:


customWriteableObj.h:



@interface customWriteableObj : NSObject <NSCoding>
@property int integer;
@property NSString * string;
@property BOOL boo;
@end




customWriteableObj.m:



\#import "customWriteableObj.h"

@implementation customWriteableObj
-(id)init
{
_integer = 117;
_string = @"aString";
_boo = YES;
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
_integer = [aDecoder decodeIntForKey:@"integer"];
_boo = [aDecoder decodeBoolForKey:@"boo"];
_string = [aDecoder decodeObjectForKey:@"string"];
return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.string forKey:@"string"];
[aCoder encodeBool:self.boo forKey:@"boo"];
[aCoder encodeInteger:self.integer forKey:@"integer"];
}
@end




main:



customWriteableObj * x = [[customWriteableObj alloc]init];
NSMutableData *dat = [[NSMutableData alloc] init];
NSKeyedArchiver* arch1 = [[NSKeyedArchiver alloc]initForWritingWithMutableData:dat];
arch1.outputFormat = NSPropertyListXMLFormat_v1_0;
[arch1 encodeObject:x forKey:@"tester1"];
[arch1 finishEncoding];

[dat writeToFile:@"text.xml";
customWriteableObj * y = [[customWriteableObj alloc]init];
y = [NSKeyedUnarchiver unarchiveObjectWithFile:@"text.xml";




Output in XML:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://ift.tt/vvUEPL">
<plist version="1.0">
<dict>
<key>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<dict>
<key>$class</key>
<dict>
<key>CF$UID</key>
<integer>3</integer>
</dict>
<key>boo</key>
<true/>
<key>integer</key>
<integer>117</integer>
<key>string</key>
<dict>
<key>CF$UID</key>
<integer>2</integer>
</dict>
</dict>
<string>aString</string>
<dict>
<key>$classes</key>
<array>
<string>customWriteableObj</string>
<string>NSObject</string>
</array>
<key>$classname</key>
<string>customWriteableObj</string>
</dict>
</array>
<key>$top</key>
<dict>
<key>tester1</key>
<dict>
<key>CF$UID</key>
<integer>1</integer>
</dict>
</dict>
<key>$version</key>
<integer>100000</integer>
</dict>
</plist>


which doesnt seem to be correct, since I cant see any data here, plus, when I try to read it back I get a nil!



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire