Re: quick and dirty NSData implosion
Re: quick and dirty NSData implosion
- Subject: Re: quick and dirty NSData implosion
- From: jon <email@hidden>
- Date: Mon, 11 May 2009 14:20:47 -0600
On May 8, 2009, at 6:56 PM, Graham Cox wrote:
You can help yourself out with this type of thing by declaring your
classes properly. If you need it to be NSCoding compliant (as you
do), then ensure it subscribes to the protocol:
@interface BookMark : NSObject <NSCoding>
I think i have the Protocol set up correct now, but i still have
something incorrect, I set up a test case to check it, and instance
"theBookMark" does not return what was put into it in the last line of
code below.
actually "bookMarkCount" returns the correct integer, but the two
strings are left as invalid? just putting in constant strings rather
than reading from a file makes the code smaller to test, but produced
the same result.
here is the class for BookMark, and the code below it that i used to
test it.... "theBookMark" instance has the correct strings in it just
before the last line of code.
Jon.
#import <Cocoa/Cocoa.h>
@interface BookMark : NSObject <NSCoding>
{
int bookMarkCount;
NSString *bookMarkurlString;
NSString *bookMarkTitle;
}
@property(readwrite, assign) int bookMarkCount;
@property(readwrite, assign) NSString *bookMarkurlString;
@property(readwrite, assign) NSString *bookMarkTitle;
@end
#import "BookMark.h"
@implementation BookMark
@synthesize bookMarkCount;
@synthesize bookMarkurlString;
@synthesize bookMarkTitle;
- (id)initWithCoder:(NSCoder *)c
{
[super init];
bookMarkCount = [c decodeIntForKey:@"bookMarkCount"];
bookMarkurlString = [c decodeObjectForKey:@"bookMarkurlString"];
bookMarkTitle = [c decodeObjectForKey:@"bookMarkTitle"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)c
{
[c encodeInt:bookMarkCount forKey:@"bookMarkCount"];
[c encodeObject:bookMarkurlString forKey:@"bookMarkurlString"];
[c encodeObject:bookMarkTitle forKey:@"bookMarkTitle"];
}
@end
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BookMark *theBookMark = [[BookMark alloc] init];
NSMutableDictionary *bookMarkList = [[NSMutableDictionary alloc]
initWithCapacity: 1];
bookMarkCount++;
NSString *theurl = [defaults stringForKey:@"the url"];
NSString *theTitle = [defaults stringForKey:@"the title"];
[theBookMark setBookMarkCount:bookMarkCount];
[theBookMark setBookMarkurlString:theurl];
[theBookMark setBookMarkTitle:theTitle];
NSString *countString = [NSString stringWithFormat:@"%d",
bookMarkCount];
[bookMarkList setObject:theBookMark forKey:countString];
NSData *bookMarkdata = [NSKeyedArchiver
archivedDataWithRootObject:bookMarkList];
[defaults setObject:bookMarkdata forKey:PECBookMarkListKey];
[defaults synchronize];
bookMarkList = [NSKeyedUnarchiver
unarchiveObjectWithData:bookMarkdata];
theBookMark = [bookMarkList objectForKey:countString];
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden