使用获取的请求sorting描述符
我有我的sorting描述符的问题。 我正在获取一个足球俱乐部的排名。 这是我的web服务如何给数据返回。
"ranking": [ { "type": "competitie", "position": "1", "name": "Club Brugge", "gamesPlayed": "10", "gamesWon": "6", "gamesTied": "4", "gamesLost": "0", "goalsPos": "25", "goalsNeg": "12", "goalsDiff": 13, "points": "22" }
这是我的提取请求。
- (void)getKlassement // attaches an NSFetchRequest to this UITableViewController { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Klassement"]; request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES]]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.genkDatabase.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; }
现在排名如下。 1,11,12,13,14,2,3,4,5 …我认为问题是我的web服务中的位置是一个string。 而我的sorting描述符正在把它看作一个string。 但实际上它应该把它看作一个整数。 有谁知道我怎么能做到这一点?
提前致谢。
即使“位置”是作为一个string从您的Web服务提供,最好的解决scheme是将其存储为核心数据模型中的数字(“整数32”或类似的)。 该对象的相应属性将是NSNumber
,它将被正确sorting。
如果你真的不能这样做,你可以作为一个解决方法尝试使用localizedStandardCompare
作为比较函数。 我认为这种string包含数字根据他们的数字值。
[NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES selector:@selector(localizedStandardCompare:)]
- 如何在使用CoreData时与Watch OS 2共享数据以显示在WKInterfaceTable中
- 如何限制NSFetchRequest的结果数量?
- 如何获取只是对象的ID,但也包括CoreData的行数据?
- 无法用types参数列表(Int64,String)调用setValue
- TableView节更新依赖于CoreData的瞬态属性更改
- :发送到释放实例的消息
- NSPredicate'ALL或ANY操作符的左侧必须是NSArray或NSSet'
- 按属性IN数组筛选核心数据结果
- Swift CoreData:错误:无法调用NSManagedObject类NSManagedObject上的指定初始值设定项