获取请求结果不会让我创建索引的字符串

时间:2014-08-05 00:21:43

标签: arrays nsfetchrequest

我在数组中获取了一个获取请求。当我尝试获取第一个索引并将其变为字符串时,应用程序崩溃。知道为什么会这样做吗?

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext
    let frequest = NSFetchRequest(entityName: "Round")

    let sortDesc = NSSortDescriptor(key: "course", ascending: true)

    frequest.sortDescriptors = [sortDesc]

    var fetchedArray:Array = context.executeFetchRequest(frequest, error: nil)


    var myString:String = fetchedArray[0] as String



    println(myString)

1 个答案:

答案 0 :(得分:0)

看起来我必须将我的原语转换为对象。工作原理

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext
    let frequest = NSFetchRequest(entityName: "Round")

    let sortDesc = NSSortDescriptor(key: "course", ascending: true)

    frequest.sortDescriptors = [sortDesc]


    var fetchedArray:Array = context.executeFetchRequest(frequest, error: nil)
    var myString:Array = (fetchedArray as NSArray).valueForKeyPath("course") as NSArray

    self.navigationItem.title = myString[0] as NSString