Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-17-2010, 09:30 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 129
stephen is on a distinguished road
Default Sorting an Array error ?

Hello,

I'm trying to sort an array, code is as follows:

Code:
NSArray *sortedArray =[listArray sortedArrayUsingFunction:intSort context:nil];


NSInteger intSort(id num1, id num2, void *dummy)
{
    int v1 = [[num1 objectForKey:@"waypoint_order"] intValue];
    int v2 = [[num2 objectForKey:@"waypoint_order"] intValue];

    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}
But its crashing on line int v1 = [[num1 objectForKey:@"waypoint_order"] intValue]; with '-[NSManagedObject objectForKey:]: unrecognized selector sent to instance 0x7a6b7f0'.

What am I doing wrong, I must be leaving out some functionality.

Regards, Stephen
stephen is offline   Reply With Quote
Old 09-17-2010, 10:37 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

from the looks if the error, it looks like you are assuming that num1 and num2 are dictionaries, when in fact they are NSManagedObjects

Its hard to tell you more without knowing how you populated your listArray.
smithdale87 is offline   Reply With Quote
Old 09-17-2010, 10:41 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 129
stephen is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
from the looks if the error, it looks like you are assuming that num1 and num2 are dictionaries, when in fact they are NSManagedObjects

Its hard to tell you more without knowing how you populated your listArray.
listArray was populate by a NSFetchRequest:
Code:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
	
		NSEntityDescription *entity = [NSEntityDescription entityForName:@"WayPoint" inManagedObjectContext:managedObjectContext];
		[request setEntity:entity];
		
		NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id contains[cd] %@", sharedMapID];
		[request setPredicate:predicate];
	
		NSError *error = nil;
		NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
	
		// Check for errors from the FetchRequest
		if (nil == results || nil != error)
			NSLog(@"Error getting results : %@", error);
	
		listArray = [results mutableCopy];
		[request release];
stephen is offline   Reply With Quote
Old 09-18-2010, 11:07 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 129
stephen is on a distinguished road
Default

*Bump*
stephen is offline   Reply With Quote
Old 09-20-2010, 05:13 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 129
stephen is on a distinguished road
Default

*bump*
stephen is offline   Reply With Quote
Old 09-20-2010, 05:25 AM   #6 (permalink)
Registered Member
 
CambridgeCoders's Avatar
 
Join Date: Jul 2010
Location: Cambridge, UK
Posts: 73
CambridgeCoders is on a distinguished road
Default

Hi,

Personally, I'm very wary of manipulating NSManagedObjects outside of CoreData's control. If I wanted a sorted array in this case, I would use a sort descriptor as part of the fetch request itself to sort the data. Would be neater, probably faster and avoids manually messing with NSManagedObjects.

See:

http://developer.apple.com/library/m.../fetching.html

Best regards,
__________________
Andy, Cambridge Coders Limited, www.cambridgecoders.com
Calcuccino - scientific calculator for the iPhone
CambridgeCoders is offline   Reply With Quote
Old 09-20-2010, 06:59 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 129
stephen is on a distinguished road
Default

Quote:
Originally Posted by CambridgeCoders View Post
Hi,

Personally, I'm very wary of manipulating NSManagedObjects outside of CoreData's control. If I wanted a sorted array in this case, I would use a sort descriptor as part of the fetch request itself to sort the data. Would be neater, probably faster and avoids manually messing with NSManagedObjects.

See:

Loading…

Best regards,
Thanks, I did try that but the values in CoreData are all strings. So this was causing a problem i.e. the results were 1,10,11,12.....2,20,21,22....etc.
Is there a way to sort a string as an integer via the NSFetchRequest.
stephen is offline   Reply With Quote
Old 09-21-2010, 01:15 AM   #8 (permalink)
Registered Member
 
CambridgeCoders's Avatar
 
Join Date: Jul 2010
Location: Cambridge, UK
Posts: 73
CambridgeCoders is on a distinguished road
Default

Hi,

I would do the following:

- Derive a class from NSManagedData to store your entities
- Add a virtual accessor to the class which returns the string values as integers

This is covered well here:

Introduction to Core Data, Part II

(scroll to "Virtual Accessors" part-way down the page).

Obvious question though - if the strings are just integers, can you not store them this way in CoreData itself?

Best regards,
__________________
Andy, Cambridge Coders Limited, www.cambridgecoders.com
Calcuccino - scientific calculator for the iPhone
CambridgeCoders is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 307
16 members and 291 guests
2ndSegment, cayladv57, cgokey, dermotos, djohnson, Domele, Hamad, heshiming, linkmx, markuschow, pungs, Rudy, Sloshmonster, teebee74, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0