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 に答える