2011-01-02 17 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、no releases。 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; 

getPathはちょうどpathでなければなりません。 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を呼び出す必要があります。

関連する問題