从 Plist 数组读取会使模拟器崩溃,但在 Xcode 控制台中不会崩溃

发布于 2024-11-30 08:18:17 字数 1690 浏览 1 评论 0原文

我是一个新手,已经被困在这个问题上几个小时了。

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

寄居人 2024-12-07 08:18:18

您无法释放任何这些变量,因为它们都是使用返回自动释放对象的方法创建的。请阅读内存管理编程指南

另外,您可以将 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]; to NSString *subPath = nil;, otherwise you're leaking a string.

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