【发布时间】:2015-02-23 07:31:14
【问题描述】:
我想使用从核心数据中检索到的用户名对数组进行排序。我这样检索它:
override func viewDidAppear(animated: Bool) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "User")
let en = NSEntityDescription.entityForName("User", inManagedObjectContext: context)
myList = context.executeFetchRequest(freq, error: nil)!
tv.reloadData()
}
后来我像这样在我的表格视图中设置单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellID:NSString = "cell"
var cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
if let ip = indexPath as Optional {
var data:NSManagedObject = myList[ip.row] as NSManagedObject
cell.textLabel!.text = data.valueForKeyPath("username") as String!
}
我尝试在 viewDidAppear 函数中使用排序函数,如下所示:
var sortedList = myList.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
但这给了我一个错误提示:“找不到成员'localizedCaseInsensitiveCompare'”
任何关于如何进行的建议将不胜感激。
【问题讨论】:
-
为什么不在获取请求中添加一个 sortDescriptor 呢?
标签: ios arrays sorting core-data swift