需要 iphone sdk 的帮助:未正确将 .plist 保存到文档目录

发布于 2024-08-16 07:16:09 字数 943 浏览 2 评论 0原文

我花了很长时间试图弄清楚出了什么问题,所以我希望她能帮助我。

我的代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小清晰的声音 2024-08-23 07:16:10

模拟器可能由于未处理的异常而冻结。在应用程序运行时检查调试控制台。
应用程序挂起的最可能原因是按下的按钮可能正在调用 fedDog: 方法。尝试将您的方法签名更改为以下之一:

- (IBAction)fedDog:(id)sender {
    // your code
}

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:

- (IBAction)fedDog:(id)sender {
    // your code
}
乄_柒ぐ汐 2024-08-23 07:16:10

对于初学者来说,您的操作方法应该采用单个参数,而不是不采用任何参数。除此之外,您是否在调试器中运行过它?

For starters, your action method should take a single argument as opposed to none. Beyond that, have you run it in the debugger?

彻夜缠绵 2024-08-23 07:16:10

您正在尝试发送 arrayWithCapacity 到 NSMutableArray 的实例,但它实际上被声明为

+ (id)arrayWithCapacity:(NSUInteger)numItems

类方法。

所以要么使用

NSMutableArray *dogsFedSave = [[NSMutableArray alloc] initWithCapacity:48];

要么

NSMutableArray *dogsFedSave = [NSMutableArray arrayWithCapacity:48];

后者会更好,因为它会产生一个自动释放的对象(而且你还是忘记释放 dogsFedSave...)

You are trying to send arrayWithCapacity to an instance of NSMutableArray, but it is actually declared as

+ (id)arrayWithCapacity:(NSUInteger)numItems

and hence a class method.

So either use

NSMutableArray *dogsFedSave = [[NSMutableArray alloc] initWithCapacity:48];

or

NSMutableArray *dogsFedSave = [NSMutableArray arrayWithCapacity:48];

The latter would be better, since it results in an autoreleased object (and you forgot to release dogsFedSave anyway…)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文