0

我正在尝试从记录超过 10000 个的 plist 文件中读取,但是每当我运行它时,它就会崩溃,这是我正在使用的代码,它无法从 plist 中读取

-(IBAction)clicked:(id)sender{

 NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
 NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile: path]; 

然后我使用排序程序.....

但以上在这里不起作用也是他的plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>4</string>
    <string>2</string>
    <string>5</string>
    <string>8</string>
    <string>1</string>
</array>
</plist>

@implementation sort_algViewController

-(IBAction)clicked:(id)sender{

 //id temp;
 NSString *path = [[NSBundle mainBundle] pathForResource:
 @"news" ofType:@"plist"];

    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    NSLog(@"%@",path);
        printf("YeS");
    }
 //NSMutableArray *array = [[NSArray arrayWithObjects: @"1", @"9", @"7",@"3", @"5", nil]mutableCopy];
 //myarray = [NSMutableArray arrayWithContentsOfFile: @"bdata.txt" ];
 NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile: path];

//  NSString *array = [NSString stringWithContentsOfFile: path];
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
//  NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSLog(@"%@",array);
    NSLog(@"%@",path);
 //printf("before Array x: ");

 //bubbleSort(array);   // sort the array
 int n = [array count]  ;
 for (int i = 0; i < n-1; i++)
 for (int j = 0; j < n-i-1; j++)
 if ([[array objectAtIndex: j] compare:
 [array objectAtIndex: j+1]] == NSOrderedDescending)

//#define SWAP(arr, x, y) 
     do {   
         id oldX = [array objectAtIndex: (j)];  
         [array replaceObjectAtIndex: (j) withObject: [array objectAtIndex: (j+1)]];
         [array replaceObjectAtIndex: (j+1) withObject: oldX];  
     } while (0);


     printf("array");
     NSString *element;
        NSEnumerator *iterator = [array objectEnumerator];
        while ((element = [iterator nextObject]) != nil)
            printf("%s ", [element UTF8String]); 
        printf("\n");

[array release];    // array needs to be released! 

 [pool release];

//return EXIT_SUCCESS;


 }
4

1 回答 1

0

检查:

NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];

不返回 nil 值path。(您可以为此添加 NSLog 跟踪)。

如果这不能解决问题,请附上控制台上的输出以及它崩溃的确切行。

编辑:

替换语句:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile: path];

和:

NSMutableArray *array = [[NSMutableArray arrayWithContentsOfFile: path] retain];

删除该行:

[array release];    // array needs to be released! 

你应该没事。

您当前编写代码的方式是释放一个 autoreleasead 数组,这是不正确的。因此,要么添加保留,要么删除版本。

于 2012-04-09T18:23:04.957 回答