简体   繁体   中英

read a list from a file and sort them using xcode NSMutableArray arrayWithContentsOfFile

i am trying to read from a plist file more than 10000 recored but whenever i run it crashes here is the code i am using the problem it can not read from plist

-(IBAction)clicked:(id)sender{

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

then i using sort program.....

but the above is not working here is also he plist file

<?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;


 }

Check that:

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

does not return a nil value for path . (You can add an NSLog trace for that).

If this does not fix the problem, please attach the output on the console and the exact line where it crashes.

EDIT:

Replace the statement:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile: path];

with:

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

or remove the line:

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

and you should be fine.

The way your code is currently written, you are releasing an autoreleasead array, which is not correct. Thus, either you add a retain, or you remove the release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM