【发布时间】:2011-03-30 15:03:24
【问题描述】:
我正在尝试创建一个 plist 文件,向其中写入数据,然后使用以下代码从中读取数据,但不知何故应用程序崩溃了。我正在尝试存储一个字符串值。谁能帮我解决这个问题,
//PLIST FILE START..
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
//And write data:
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//here add elements to data file and write data to file
//NSString *value = rowValue;
[data setObject:[NSString stringWithFormat:@"%@", rowValue] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];
//Read Data..
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//load from savedStock example int value
NSString *test;
test = [savedStock objectForKey:@"value"];
[savedStock release];
NSLog(@"PLIST VALUE:%@",test);
//PLIST FILE END..
我得到的错误,
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFString stringValue]:无法识别的选择器发送到实例 0x4b25af0” *** 第一次抛出调用堆栈: ( 0 核心基础 0x00dd9be9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x00f2e5c2 objc_exception_throw + 47 2核心基础0x00ddb6fb-[NSObject(NSObject)不识别选择器:]+187 3 核心基础 0x00d4b366 ___forwarding___ + 966 4 核心基础 0x00d4af22 _CF_forwarding_prep_0 + 50 5 木槌边缘 0x00003e1e -[JobViewController tableView:didSelectRowAtIndexPath:] + 832 6 UIKit 0x00358794-[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140 7 UIKit 0x0034ed50-[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219 8 基础 0x000617f6 __NSFireDelayedPerform + 441 9 核心基础 0x00dbafe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 10 核心基础 0x00dbc594 __CFRunLoopDoTimer + 1220 11 核心基础 0x00d18cc9 __CFRunLoopRun + 1817 12 核心基础 0x00d18240 CFRunLoopRunSpecific + 208 13 核心基础 0x00d18161 CFRunLoopRunInMode + 97 14 图形服务 0x0170e268 GSEventRunModal + 217 15 图形服务 0x0170e32d GSEventRun + 115 16 UIKit 0x002f142e UIApplicationMain + 1160 17 木槌边缘 0x00001de4 主 + 102 18 木槌边缘 0x00001d75 开始 + 53 19 ??? 0x00000001 0x0 + 1 ) 抛出 'NSException' 的实例后调用终止【问题讨论】:
-
“应用程序以某种方式崩溃”的表述根本没有真正的帮助。您可能想尝试使用调试器并找出该应用程序究竟在什么时候崩溃了。
-
它在哪一行崩溃?有什么错误?
-
对不起,我已经编辑了我的代码以添加错误。