首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用NSCoder

如何使用NSCoder
EN

Stack Overflow用户
提问于 2009-11-09 23:04:21
回答 4查看 6K关注 0票数 5

我正在开发iphone应用程序。

我使用NSCoder。

MyApplication.h

代码语言:javascript
复制
#define ITEMS_KEY @"items"
#define CATEGORIES_KEY @"categories"


#import <UIKit/UIKit.h>


@interface MyApplicationData : NSObject <NSCoding, NSCopying> {
    NSMutableArray* items;
    NSMutableArray* categories;
}

@property (nonatomic ,retain) NSMutableArray* items;
@property (nonatomic, retain) NSMutableArray* categories;


@end

Myapplication.m

代码语言:javascript
复制
#import "MyApplicationData.h"


@implementation MyApplicationData

@synthesize items;
@synthesize categories;

#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder{
  [aCoder encodeObject:items forKey:ITEMS_KEY];
  [aCoder encodeObject:categories forKey:CATEGORIES_KEY];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
  if(self = [super init]){
    self.items = [aDecoder decodeObjectForKey:ITEMS_KEY];
    self.categories = [aDecoder decodeObjectForKey:CATEGORIES_KEY];
  }
  return self;
}

#pragma mark -
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone{
  MyApplicationData* copy = [[[self class]allocWithZone:zone]init];
  items = [self.items copy];
  categories = [self.categories copy];
  return copy;
}

@end

但是警告。

代码语言:javascript
复制
'NSCoder' may not respond to '-decodeDataObjectForKey'

如何使用NSCoder?

EN

回答 4

Stack Overflow用户

发布于 2009-11-09 23:08:20

使用-decodeObjectForKey:read the documentation

票数 7
EN

Stack Overflow用户

发布于 2009-11-09 23:33:13

这是来自我的LogEntry对象的NSCoding协议方法,你可以忽略switch语句和模式细节,这些都来自我写的一个基类,它允许我保持对数据格式变化的正常跟踪。

请注意,除了使用decodeObjectForKey:您还需要确保保留/复制给定值,因为它们在收到时会自动释放。

代码语言:javascript
复制
- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];

    if (self != nil) {
        switch ([schemaVersion intValue]) {
            case 2:
                filepath = [[coder decodeObjectForKey:@"filepath"] copy];
                identifier = [coder decodeInt64ForKey:@"identifier"];
                level = [coder decodeIntForKey:@"level"];
                lineNumber = [[coder decodeObjectForKey:@"lineNumber"] retain];
                message = [[coder decodeObjectForKey:@"message"] retain];
                timestamp = [[coder decodeObjectForKey:@"timestamp"] retain];
                break;              
            default:
                [self release], self = nil;
                break;
        }
    }

    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:filepath forKey:@"filepath"];
    [coder encodeInt64:identifier forKey:@"identifier"];
    [coder encodeInt:level forKey:@"level"];
    [coder encodeObject:lineNumber forKey:@"lineNumber"];
    [coder encodeObject:message forKey:@"message"];
    [coder encodeObject:timestamp forKey:@"timestamp"];

    [super encodeWithCoder:coder];
}
票数 3
EN

Stack Overflow用户

发布于 2009-11-09 23:07:18

我认为你应该使用-decodeObjectForKey:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1701582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档