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

How to make method spit out different things

我正在努力让它发挥作用。我有一个功能,那就是假设磁带进入 coreData 并抓取

  1. 所有例程
  2. 某个套路

如果是:

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
}

我是这样做的:

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