从 Plist 数组读取会使模拟器崩溃,但在 Xcode 控制台中不会崩溃
我是一个新手,已经被困在这个问题上几个小时了。
我正在尝试使用 PickerView 选定的整数来: - p1:选择数组plist的路径 - p2:在加载数组的索引处选择对象
我正在测试 2 个 plist root = 数组文件的路径切换,当选择器在模拟器中进行选择时,它崩溃,但控制台 NSLog 为正确的 plist 和对象输出正确的值在指数值。
谁能告诉我哪里出错了?我不明白为什么模拟器崩溃,我检查了 XIB 连接。
#define person1 0
#define person2 1
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ((component ==person1) || (component == person2)) {
int p1;
int p2;
p1=[pickerView selectedRowInComponent:person1];
p2=[pickerView selectedRowInComponent:person2];
NSString* subPath = [[NSString alloc] init];
switch (p1) {
case 0:
subPath = @"data0";
break;
case 1:
subPath = @"data1";
break;
default:
NSLog(@"error");
break;
}
NSString *path = [[NSBundle mainBundle] pathForResource:subPath ofType:@"plist"];
NSArray *p1Array = [NSArray arrayWithContentsOfFile:path];
NSString *resultsMessage = [[NSString alloc] init];
resultsMessage = [p1Array objectAtIndex:p2];
NSLog(@"%@", resultsMessage);
result.text = resultsMessage; //Outlet = UITextView
[p1Array release];
[path release];
[subPath release];
[resultsMessage release];
}
}
I'm a newbie and have been stuck on this for hours.
I am trying to use PickerView selected integers to:
- p1: select path to array plist
- p2: select object at index of loaded array
I am testing path switching for 2 plist root = array files, when the picker makes a selection in the Simulator, it crashes, but the console NSLog outputs the correct value for the right plist and object at index value.
Can anyone please tell me where I am going wrong? I don't understand why the simulator crashes, I have checked the XIB connections.
#define person1 0
#define person2 1
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ((component ==person1) || (component == person2)) {
int p1;
int p2;
p1=[pickerView selectedRowInComponent:person1];
p2=[pickerView selectedRowInComponent:person2];
NSString* subPath = [[NSString alloc] init];
switch (p1) {
case 0:
subPath = @"data0";
break;
case 1:
subPath = @"data1";
break;
default:
NSLog(@"error");
break;
}
NSString *path = [[NSBundle mainBundle] pathForResource:subPath ofType:@"plist"];
NSArray *p1Array = [NSArray arrayWithContentsOfFile:path];
NSString *resultsMessage = [[NSString alloc] init];
resultsMessage = [p1Array objectAtIndex:p2];
NSLog(@"%@", resultsMessage);
result.text = resultsMessage; //Outlet = UITextView
[p1Array release];
[path release];
[subPath release];
[resultsMessage release];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法释放任何这些变量,因为它们都是使用返回自动释放对象的方法创建的。请阅读内存管理编程指南。
另外,您可以将
NSString* subPath = [[NSString alloc] init];
更改为NSString *subPath = nil;
,否则您将泄漏字符串。You can't release any of those variables, because they were all created with methods that return autoreleased objects. Please read the Memory Management Programming Guide.
Also, you can change
NSString* subPath = [[NSString alloc] init];
toNSString *subPath = nil;
, otherwise you're leaking a string.