In meinem Xcode-Projekt habe ich die folgenden Klassen:Sortieren NSArray mit benutzerdefinierten Objekte
Adresse
@interface LDAddress : NSObject{
NSString *street;
NSString *zip;
NSString *city;
float latitude;
float longitude;
}
@property (nonatomic, retain) NSString *street;
@property (nonatomic, retain) NSString *zip;
@property (nonatomic, retain) NSString *city;
@property (readwrite, assign, nonatomic) float latitude;
@property (readwrite, assign, nonatomic) float longitude;
@end
Ort
@interface LDLocation : NSObject{
int locationId;
NSString *name;
LDAddress *address;
}
@property (readwrite, assign, nonatomic) int locationId;
@property (nonatomic, retain) LDAddress *address;
@property (nonatomic, retain) NSString *name;
@end
In einer Unterklasse von UITableViewController gibt es ein NSArray, das viele unsortierte Objekte von LDLocations enthält. Nun möchte ich die NSArray-Objekte aufsteigend nach der Eigenschaft city einer LDAddress sortieren.
Wie kann ich das Array mit NSSortDescriptor sortieren? Ich habe Folgendes versucht, aber die App wird beim Sortieren des Arrays abgemeldet.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];
haben Sie versucht, nur @ "Stadt" als Schlüssel? –