如何让方法吐出不同的东西

时间:2015-03-12 21:42:49

标签: ios swift core-data methods nsmanagedobject

我正在努力让这个工作。我有一个函数,那就是假设要磁带到coreData并抓住

  1. 所有例程
  2. 某个例程
  3. 若是:

    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
    }
    

1 个答案:

答案 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!
}