需要 iphone sdk 的帮助:未正确将 .plist 保存到文档目录
我花了很长时间试图弄清楚出了什么问题,所以我希望她能帮助我。
我的代码:
- (IBAction)fedDog {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dogsFedDays.plist"];
NSMutableArray *dogsFedSave = [[NSMutableArray alloc] arrayWithCapacity: 48];
for (int i = 0; i < 48; i++) {
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:
date[i], @"string",
fed[i], @"Yes",
nil];
[dogsFedSave addObject:myDict];
[myDict release];
}
if (![dogsFedSave writeToFile:path atomically:YES])
NSLog(@"not successful in completing this task");
}
我已将操作连接到按钮,但是当按下按钮时,模拟器会冻结,并且文档目录中不会出现任何文件。
I've been spending way too long trying to figure out what is going wrong, so I hope someone her can help me out.
My Code:
- (IBAction)fedDog {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dogsFedDays.plist"];
NSMutableArray *dogsFedSave = [[NSMutableArray alloc] arrayWithCapacity: 48];
for (int i = 0; i < 48; i++) {
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:
date[i], @"string",
fed[i], @"Yes",
nil];
[dogsFedSave addObject:myDict];
[myDict release];
}
if (![dogsFedSave writeToFile:path atomically:YES])
NSLog(@"not successful in completing this task");
}
I've connected the action to a button, but when the button is pressed, the simulator freezes, and no file appears in the Documents directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模拟器可能由于未处理的异常而冻结。在应用程序运行时检查调试控制台。
应用程序挂起的最可能原因是按下的按钮可能正在调用
fedDog:
方法。尝试将您的方法签名更改为以下之一:It's possible that the simulator freezes because of an unhandled exception. Check the debug console while your app is running.
The most probable reason your application hangs is that the button pressed may be calling
fedDog:
method. Try changing your method signature to this one:对于初学者来说,您的操作方法应该采用单个参数,而不是不采用任何参数。除此之外,您是否在调试器中运行过它?
For starters, your action method should take a single argument as opposed to none. Beyond that, have you run it in the debugger?
您正在尝试发送 arrayWithCapacity 到 NSMutableArray 的实例,但它实际上被声明为
类方法。
所以要么使用
要么
后者会更好,因为它会产生一个自动释放的对象(而且你还是忘记释放 dogsFedSave...)
You are trying to send arrayWithCapacity to an instance of NSMutableArray, but it is actually declared as
and hence a class method.
So either use
or
The latter would be better, since it results in an autoreleased object (and you forgot to release dogsFedSave anyway…)