2012-10-16 3 views
0

나중에 사용할 수 있도록 개체를 저장하기 위해 NSData를 사용하고 있습니다. 그것은 꽤 많은 NSStrings를 가지고 있으며 객체에서 꺼내야합니다.
어떤 이유로 NSStrings 중 some 만 저장되고 다른 일부는 0이됩니다! 나는 그것이 내 문자열로 초기화되어야한다는 것을 잊어 버렸음에 틀림 없다. 그러나 매우 이상한 이유로 일부 문자열이 데이터를 잃어 버렸다. theImportantString은 변수가 값을 얻은 것처럼 보이기 때문에 관련 값을 가져올 수 없지만 Unarchive에서 돌아온 후에는 @ ""와 같습니다!일부 NSCoding 변수가 검색되지 않습니다.

// 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 

MyController.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]; 
} 

그리고 버튼 HA에 수돗물 ppens : (값이 더 이상 설정할 수 없습니다) 대리자 컨트롤러에서

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

:

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

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

하지만 declarati에 : /////////////////

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


} 
+0

을 그냥 (문자열) 객체 자체를 사용할 수 있습니다 때'initWithString'를 사용하는 이유는 무엇입니까? 또한 식별자'ID/DIST'와'NAME/DISTRIBUTOR'의 사용은 혼란 스럽습니다. – trojanfoe

답변

1

당신은 유형의 불일치를 것 같다 당신이

// in CMyData.h 
NSString *DIST; 

이에이 있어야한다 :

// in - (id)initWithCoder:(NSCoder *)decoder 
self.DIST = [NSString stringWithFormat:@"%d", [decoder decodeIntForKey:@"DIST"]]; 
+0

난 그냥 개체를 잘못 입력 한 코드를 편집 ... – Ted

+0

만약 당신이 nsstring 인코딩 및 개체를 디코딩이 잘 작동합니다. –

+0

self.DIST는 다음과 같아야합니다 : self.DIST = [[NSString alloc] initWithString : [decoder decodeObjectForKey : @ "DIST"]]; ? – Ted

관련 문제