0

我正在使用 NSData 来存储对象以供以后使用。它有很多 NSStrings,我需要把它从一个对象中拉出来。
出于某种原因,只有someNSString 被存储,而其他一些则被清零!我在想这一定是我的代码有问题,我一定忘记初始化一些字符串,但是由于一个非常奇怪的原因,一些字符串丢失了数据!我无法theImportantString得到它的相关值,因为它首先似乎变量得到了它的值,但是在从 回来之后Unarchive,它等于 @""!

// CMyData.h
///////////////////

@interface CMyData : NSObject <NSCoding>
{
    NSString *ID;
    NSString *DIST;
    .  
    .  
}
@property (nonatomic,retain) NSString *ID;
@property (nonatomic,retain) NSString *DIST;

@end

// CMyData.m
////////////////////

#import "CMyData.h"
@implementation CMyData

@synthesize ID;
@synthesize DIST;

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.ID = [decoder decodeObjectForKey:@"ID"];
        self.DIST = [decoder decodeObjectForKey:@"DIST"];
    .  
    .  
 }


- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:ID forKey:@"ID"];
    [encoder encodeObject:DIST forKey:@"DIST"];
    .  
    .  

}

- (void)dealloc {
    [ID release];
    [DIST release];
    [super dealloc];

}

@end

我的控制器.m

-(void) makeObject: (NSDictionary *)dict
{    
    CMyData* myData=[[CMyData alloc] init];
    myData.ID = [[NSString alloc] initWithString:[dict objectForKey:@"NAME"]];
    myData.DIST = [[NSString alloc] initWithString:[dict objectForKey:@"DISTRIBUTOR"]];
    .  
    .  

    myObject = [[[MYObject alloc] init];

    myObject.data = [NSKeyedArchiver archivedDataWithRootObject:myData];
} 

然后点击一个按钮:

- (void) tapOnIcon: (MyObject*)theObject 
{
    CMyData *data = [NSKeyedUnarchiver unarchiveObjectWithData:theObject.data];
    [delegate showData:data];
}

在委托控制器中(无法再设置值):

delegateController.m
/////////////////////////////

-(void) showData:(CMyData*)theData{
     self.theImportantString = [[NSString alloc] initWithString:theData.DIST];
     .
     .
     .


}
4

1 回答 1

1

似乎您的类型不匹配:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [decoder decodeIntForKey:@"DIST"];

但在声明中你有

// in CMyData.h
NSString *DIST;

这应该是:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [NSString stringWithFormat:@"%d", [decoder decodeIntForKey:@"DIST"]];
于 2012-10-16T12:05:41.760 回答