Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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
Iphone NSMutableArray到plist时应用程序崩溃_Iphone_Objective C_Plist_Exc Bad Access - Fatal编程技术网

Iphone NSMutableArray到plist时应用程序崩溃

Iphone NSMutableArray到plist时应用程序崩溃,iphone,objective-c,plist,exc-bad-access,Iphone,Objective C,Plist,Exc Bad Access,我有一个名为*收藏夹的NSMutableArray。它是一个由NSStrings组成的数组,我想将其写入.plist文件 当我打电话时: favPath = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"]; [favorites writeToFile:favPath automatically:YES]; 由于异常类型EXC\u BAD\u ACCESS(SIGBUS)和0x00000006处的异常代码

我有一个名为
*收藏夹的
NSMutableArray
。它是一个由
NSStrings
组成的数组,我想将其写入
.plist
文件

当我打电话时:

favPath = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"];
[favorites writeToFile:favPath automatically:YES];
由于异常类型
EXC\u BAD\u ACCESS(SIGBUS)
和0x00000006处的异常代码
KERN\u PROTECTION\u故障,它崩溃

我已经包括了crashlog、Favorites.plist和调用该方法的代码片段。非常感谢您的帮助

Crashlog:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000006
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x000034f8 objc_msgSend + 24
1   Foundation                      0x00011ae6 _NSWriteBytesToFileWithExtendedAttributes + 56
2   Foundation                      0x00011aa0 _NSWriteBytesToFile + 20
3   Foundation                      0x00011a60 -[NSData(NSData) writeToFile:atomically:] + 60
4   Foundation                      0x00049124 -[NSArray(NSArray) writeToFile:atomically:] + 148
5   CoderPlus                       0x00005a7a -[HTMLViewController tableView:didSelectRowAtIndexPath:] (HTMLViewController.m:277)
6   UIKit                           0x000c3f98 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 884
7   UIKit                           0x000d4084 -[UITableView _userSelectRowAtIndexPath:] + 196
8   Foundation                      0x00088334 __NSFireDelayedPerform + 360
9   CoreFoundation                  0x00074256 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 10
10  CoreFoundation                  0x00076966 __CFRunLoopDoTimer + 1038
11  CoreFoundation                  0x000773ea __CFRunLoopRun + 1178
12  CoreFoundation                  0x0001e0bc CFRunLoopRunSpecific + 220
13  CoreFoundation                  0x0001dfca CFRunLoopRunInMode + 54
14  GraphicsServices                0x00003f88 GSEventRunModal + 188
15  UIKit                           0x00007b40 -[UIApplication _run] + 564
16  UIKit                           0x00005fb8 UIApplicationMain + 964
17  CoderPlus                       0x0000837a main (main.m:14)
18  CoderPlus                       0x00002c38 start + 32
收藏夹。plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>

</array>
</plist>

这可能不是导致崩溃的原因,但将内容写回bundle是非常糟糕的做法。我甚至不确定这是否有效。如果应用程序包目录为只读,则您的应用程序可能会因上述原因崩溃


可以从应用程序包中读取初始文件,但如果要更改并保留该数据,则必须将其写入
文档
目录。这在这里的堆栈溢出中有很好的介绍。

您不能在iPhone上的应用程序包中写入或修改文件。如果要保存数据,请在应用程序的文档目录中创建文件。您可以按如下方式获取文档目录:

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
然后,只需使用
NSString
stringByAppendingPathComponent:
方法附加文件名即可


查看Apple的低级文件管理编程主题,了解有关从应用程序中保存数据的更多信息。

我已经这样做了,但它仍然不起作用:NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);NSString*documentsDirectoryPath=[paths objectAtIndex:0];favPath=[documentsDirectoryPath stringByAppendingPathComponent:@“Favorites.plist”];收藏夹=[[NSMutableArray alloc]initWithContentsOfFile:favPath];几个问题:文件是否存在?它是使用
NSArray
writeToFile:atomicaly:
方法编写的吗?如果文件是PLIST,则需要将其读入<代码> NSMutable字典< /代码>中,而不是<代码> NSMutableArray < /代码>。如果这是为了存储应用程序首选项,请考虑使用<代码> NSUDER Debug 。我只是想知道您必须将文件实际写入文档目录……我想我以为SDK会帮我解决这个问题。现在它工作得很好,谢谢!很乐意帮忙。如果对应用程序首选项使用
NSUserDefaults
,SDK将为您处理文件位置和I/O,但如果您手动管理文件,则必须自己指定文档目录。
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];