Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 调试归档程序_Ios_Objective C_Nskeyedarchiver_Persistent Data - Fatal编程技术网

Ios 调试归档程序

Ios 调试归档程序,ios,objective-c,nskeyedarchiver,persistent-data,Ios,Objective C,Nskeyedarchiver,Persistent Data,我无法找出存档和取消存档的问题所在。我正在尝试保存类中的数据。编码器和解码器是: //archiving - (void)encodeWithCoder:(NSCoder *)aCoder { //encode only certain instance variables [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeObject:self.location forKey:@"location"]; } -

我无法找出存档和取消存档的问题所在。我正在尝试保存类中的数据。编码器和解码器是:

//archiving
- (void)encodeWithCoder:(NSCoder *)aCoder
{
//encode only certain instance variables
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.location forKey:@"location"]; 


}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [self init];
if (self) {
    //decode data
    [self setName:[aDecoder decodeObjectForKey:@"name"]];
    [self setLocation:[aDecoder decodeObjectForKey:@"location"]];

}

return self;

}
实例变量是属性,它们是自定义类的值。此类的多个实例填充存储在我的主视图控制器中的NSMutableArray

“我的视图”控制器包含以下方法:

- (NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//only one document in list - get path t o it
NSString *documentDirectory = [documentDirectories objectAtIndex:0];

return [documentDirectory stringByAppendingString:@"file.archive"];
}

- (BOOL)saveChanges
{
//returns success or failure
NSString *path = [self itemArchivePath];

return [NSKeyedArchiver archiveRootObject:customClassArray //This is the array storing multiple instances of the custom class
                                   toFile:path];
}
关于数组的注释不在实际代码中。最后,应用程序代理具有以下代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

BOOL success = [mainViewController saveChanges];
if (success) {
    NSLog(@"Saved Successfully!");
}
else {
    NSLog(@"Unsuccessful");
}

}

不幸的是,每当运行代码时,总是记录“unsuccessful”,我不知道为什么。mainViewController是应用程序委托的实例变量。我已经尝试调试了很长时间,但我找不到问题。感谢您的帮助。谢谢

您试图保存的
路径很可能无效。您应该尝试记录它,并在保存失败时查看该值是否正常。您可能还想通过附加字符串查看使用而不是
stringby:

self.location
self.name
?self.location是由CLLocationManager确定的CLLocation对象。Self.name是一个NSString。您是否检查了您试图写入的
路径是否正常?该路径似乎正常。当我通过模拟器运行应用程序时,方法会在实际设备上记录“保存成功!”,但我一直收到“未成功”。文档目录路径是否可能在实际设备上有所不同?这似乎不太可能,但似乎是可能的-我会尝试在
NSString*path=[self-itemArchivePath]之后记录
path
的值并查看它在设备上是否正确。