我正在努力让这个工作。我有一个函数,那就是假设要磁带到coreData并抓住
若是:
1 - >它需要输出routineNames作为NSArray
2 - >然后将其输出为NSManagedObject。
var routineNames:AnyObject!
func GetRoutines(type: String) -> AnyObject {
routineNames = []
var request = NSFetchRequest(entityName: "Routines")
let sort = NSSortDescriptor(key: "name", ascending: false)
let sortDescriptors = [sort]
request.sortDescriptors = sortDescriptors
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
if results?.count > 0 {
if type == "AllRoutines" {
for result: AnyObject in results! {
if result.valueForKey("name") != nil {
routineNames.append(result.valueForKey("name") as String)
//println(routineNames)
}
}
} else {
for result: AnyObject in results! {
if result.valueForKey("name") as String == type {
routineNames = result
}
}
}
}
return routineNames
}
答案 0 :(得分:0)
我设法做到了这样:
func GetRoutines(type: String) -> AnyObject {
routineNames = []
var request = NSFetchRequest(entityName: "Routines")
let sort = NSSortDescriptor(key: "name", ascending: false)
let sortDescriptors = [sort]
request.sortDescriptors = sortDescriptors
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
if results?.count > 0 {
if type == "AllRoutines" {
for result: AnyObject in results! {
if result.valueForKey("name") != nil {
routineNames.append(result.valueForKey("name") as String)
//println(routineNames)
}
}
} else {
for result: AnyObject in results! {
if result.valueForKey("name") as String == type {
println(result)
return result
}
}
}
}
return results!
}