开发者

NSMutableArray when using arrayWithArray the mutable array becomes the right size, but the object are all out of scope

开发者 https://www.devze.com 2023-02-06 11:13 出处:网络
NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@\"ComicDB\" withPredicate:nil]; [ComicArray retain];
NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@"ComicDB" withPredicate:nil];
    [ComicArray retain];

    arrayOfComics = [NSMutableArray arrayWithArray:ComicArray];
    [[arrayOfComics valueForKey:@"Name"] sortUsingSelector:@selector(caseInsensitiveCompare:)];

    [ComicArray release];

Why are all the object in the arrayOfComics out of scope?

EDIT: I tried doing this:

NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@"ComicDB" withPredicate:pr开发者_C百科edicate];
    arrayOfComics =[[NSMutableArray alloc] init];
    for (int i = 0; i < [ComicArray count]; i++) {
        [arrayOfComics addObject:[ComicArray objectAtIndex:i]];
    }

    [[arrayOfComics valueForKey:@"Name"] sortUsingSelector:@selector(caseInsensitiveCompare:)];

All the objects in arrayOfComics are still out of scope....

EDIT: This works, the objects in arrayOfComicsTest are NOT "out of scope". I am not sure why this works yet when i do arrayOfComics they are out of scope. arrayOfComics is a class variable NSMutableArray * arrayOfComics in the .h. It is not used anywhere until this point.

NSMutableArray * arrayOfComicsTest = [NSMutableArray arrayWithArray:ComicArray];


NSArray *comicArray = [TCSDataBase fetchManagedObjectsForEntity:@"ComicDB" withPredicate:nil];
NSArray *sortedComicNamesArray = [[comicArray valueForKey:@"Name"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

Note that the latter array only has the names of the comics sorted. It does not contain a sorted list of your comics.

BTW, if you want a sorted list for your comics (not just the names) simply create the proper predicate to use in the -fetchManagedObjectsForEntity:withPredicate: method.


Try this....still i m not getting why u r not using executeFetchRequest

NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@"ComicDB" withPredicate:nil];
    [ComicArray retain];

    arrayOfComics = [NSMutableArray arrayWithArray:ComicArray];
    [[arrayOfComics valueForKey:@"Name"] sortedUsingSelector:@selector(caseInsensitiveCompare:)];


You have to retain your array "arrayOfComics". Put a break point in your code and immediately when it is created, all the objects will be available. But if you want to use that array in some other method, all the objects go out of scope as you are not retaining. Else it will have the scope upto the method in which it is assigned.

After correction, your code must look like:

NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@"ComicDB" withPredicate:nil];
[ComicArray retain];

arrayOfComics = [NSMutableArray arrayWithArray:ComicArray];
[arrayOfComics retain];
[[arrayOfComics valueForKey:@"Name"] sortUsingSelector:@selector(caseInsensitiveCompare:)];

[ComicArray release];

Please note that if you want to reassign arrayOfComics, you have to release and then assign.


When you say the objects are "out of scope" I presume you mean you can't see them in the debugger. If this is the case, stop worrying about it as it happens quite a lot, it's just an implementation feature. In particular, core data doesn't always fetch an actual object till it's needed (read about faulting in the Core Data Programming Guide). If you right click on the array in the debugger display and select "print description to console", it'll print the array and all its objects quite happily.

Your code does have a problem though. This line:

[[arrayOfComics valueForKey:@"Name"] sortUsingSelector:@selector(caseInsensitiveCompare:)];

is nonsense. First it gets an array of the name keys of your objects and then it tries to sort it (which may fail unless the array of names happens to be mutable). Then it throws away the result.


At the time I was having a bunch of problems because I did not fully understand the difference between mutable and non mutable arrays, as well as the various return values from array operations.

I eventually fixed this by making a bunch of changes.

Thanks to everyone that provided help.

0

精彩评论

暂无评论...
验证码 换一张
取 消