2011-01-02 4 views
2

안녕하세요, 내가 만든 클래스에서 개체를 저장하려고합니다. 샷이라고하며 5 개의 변수를 저장하고 있습니다. 다음은 .h 파일입니다. NSCoding과 NSMutableCopying Protocals 및 가져 오기를 자르지 만 거기에 있습니다.NSKeyedArchiver로 데이터를 어떻게 저장합니까?

#import <Foundation/Foundation.h> 


@interface Shot : UIButton <NSCoding, NSMutableCopying> { 

int x; 
int y; 
int location; 
int quarter; 
bool made; 

int miss; 
int make; 
} 

@property()int x; 

@property()int y; 

@property()int location; 

@property()int quarter; 

@property()bool made; 

-(void)set_x:(int)my_x set_y:(int)my_y set_quarter:(int)my_quarter set_made:(bool)my_made set_location:(int)my_location; 

-(void)set_miss_AND_set_make; 

@end 

**Here are the methods I made to save the data in my .m file---** 

-(void)encodeWithCoder:(NSCoder *)aCoder { 

[aCoder encodeInt:x forKey:@"x"]; 
[aCoder encodeInt:y forKey:@"y"]; 
[aCoder encodeInt:location forKey:@"location"]; 
[aCoder encodeInt:quarter forKey:@"quarter"]; 
[aCoder encodeBool:made forKey:@"made"]; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder { 

if (self = [super init]) { 
x = [aDecoder decodeIntForKey:@"x"]; 
y = [aDecoder decodeIntForKey:@"y"]; 
location = [aDecoder decodeIntForKey:@"location"]; 
quarter = [aDecoder decodeIntForKey:@"quarter"]; 
made = [aDecoder decodeBoolForKey:@"made"]; 
} 
return self; 
} 

-(id)mutableCopyWithZone:(NSZone *)zone { 

Shot *newShot = [[Shot allocWithZone:zone]init]; 
[newShot set_x:x set_y:y set_quarter:quarter set_made:made set_location:location]; 
return newShot; 
} 

내가 버튼까지 저장 및로드 방법을 설정 한

-(NSString *)getPath { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentFolder = [paths objectAtIndex:0]; 
NSFileManager *fm = [[NSFileManager alloc]init]; 
if ([fm fileExistsAtPath:documentFolder] == NO) { 
    [fm createDirectoryAtPath:documentFolder withIntermediateDirectories:YES attributes:nil error:nil]; 
} 
return [documentFolder stringByAppendingFormat:@"iStatTrackInfo.archive"]; 
} 

-(void)saveData { 

NSString *path = [self getPath]; 
[NSKeyedArchiver archiveRootObject:shotArray toFile:path]; 
} 

-(void)loadData { 

NSString *path = [self getPath]; 
NSFileManager *fm = [[NSFileManager alloc]init]; 
if ([fm fileExistsAtPath:path] == YES) { 
    shotArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 
    return; 
} 

NSEnumerator *enumOne = [shotArray objectEnumerator]; 
Shot *shotObject = [[Shot alloc]init]; 
while (shotObject = [enumOne nextObject]) { 
    Shot *shotShot = [[Shot alloc]init]; 
    shotShot = [shotObject mutableCopy]; 
    shotShot.frame = CGRectMake(0, 0, 50, 50); 
    shotShot.backgroundColor = [UIColor whiteColor]; 
    [self addSubview:shotShot]; 
} 
} 

이 방법을 사용할 때 저장 내 데이터를 얻을 수없는 것,하지만 내 데이터가 여전히 원 저장하지 마라. 도와주세요!!!

답변

2

나는 마침내 문제를 해결했습니다. 위 도움말 이외에 .archive는 파일 유형이 아닙니다. .archive는 저장할 수 없으며 .arch 또는 .plist 만 저장할 수 있습니다. 이것이 내 주요 문제였습니다.

+0

안녕하세요, 코드 주셔서 감사합니다, 그것은 나를 도왔습니다. – MyCSharpCorner

1

먼저, 당신은 체와 같은 기억을 새고 있습니다. alloc/init & mutableCopy, 아니 release s입니다. memory management guide을 읽을 것을 권합니다.

다음으로 메소드 이름은 코코아 규칙을 따르지 않습니다. 이 :

-(void)set_x:(int)my_x set_y:(int)my_y set_quarter:(int)my_quarter set_made:(bool)my_made set_location:(int)my_location; 

-(void)set_miss_AND_set_make; 

같은 것을해야합니다 또한

-(void) resetMissAndMake; // or just missAndMake or activateMissAndMake 
-(void) setX:(NSInteger)xValue y:(NSInteger)yValue quarter:(NSInteger)quarterValue location:(NSInteger)aLocation; 

getPathpath해야한다. get이라는 접두사가 붙은 메서드는 매우 드물고 매우 특정한 의미를 갖습니다.

이 또한 말도 다음 [[Shot alloc]init] 통화 중 하나에 대한 필요 (사람들은 모두 누출이다)가 없다

Shot *shotObject = [[Shot alloc]init]; 
while (shotObject = [enumOne nextObject]) { 
    Shot *shotShot = [[Shot alloc]init]; 
    shotShot = [shotObject mutableCopy]; 

.

마지막으로 인코딩 방법이 잘못 구현되었습니다. As the documentation states이라면 super 번으로 전화해야합니다.

특히, 당신의 encodeWithCoder:은 같은 슈퍼의 구현을 호출해야하고 initWithCoder: 슈퍼의 initWithCoder:하지 init를 호출해야합니다.