嗨,我正在尝试从我创建的类中保存一个对象。它被称为shot并包含我想保存的5个变量。这是.h文件--- 它切断了NSCoding和NSMutableCopying Protocals以及我的导入,但它们就在那里。
#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];
}
}
我已将保存和加载方法设置为按钮,但我的数据仍然无法保存。请帮忙!!!
答案 0 :(得分:2)
我终于解决了这个问题。除上述帮助外,.archive不是文件类型。你无法保存.archive,你只能保存.arch或.plist这是我的主要问题。
答案 1 :(得分:1)
首先,你像筛子一样泄漏记忆。很多alloc/init
&amp; mutableCopy
,没有release
。我建议你阅读memory management guide。
接下来,您的方法名称不遵循Cocoa约定。这些:
-(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 *shotObject = [[Shot alloc]init];
while (shotObject = [enumOne nextObject]) {
Shot *shotShot = [[Shot alloc]init];
shotShot = [shotObject mutableCopy];
不需要[[Shot alloc]init]
个调用(这两个都是泄漏)。
最后,您的编码方法实现不正确。 As the documentation states,您需要根据需要致电super
。
具体来说,您的encodeWithCoder:
必须调用super的实现,initWithCoder:
应该调用super initWithCoder:
,而不是init
。