【问题标题】:swift setting string from array element error从数组元素错误中快速设置字符串
【发布时间】:2014-08-04 20:45:09
【问题描述】:

所以我使用字典从核心数据中获取属性。它抓住它就好了。但是,当我尝试获取单个返回的属性并将其分配给 navigationitem.title 时,它​​会引发一些令人讨厌的错误。我在这里错过了什么吗.. 它实际上并没有在控制台中输出错误消息,而是给了我这个:

Thread 1: EXC_BREAKPOINT(code+exc_I386_BPT.....

在这条线上。

0x10707a5f3:  nopw   %cs:(%rax,%rax)

也是它的说法

0 swift_dynamicCastObjCClassUnconditional

代码:

override func viewDidLoad() {
    super.viewDidLoad()

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



    frequest.propertiesToFetch = NSArray(object: "course")
    frequest.returnsObjectsAsFaults = false
    frequest.returnsDistinctResults = true

    frequest.resultType = NSFetchRequestResultType.DictionaryResultType



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

    //this is the line that throw the error...why won't it let me set the title to the array of the single returned result?
    self.navigationItem.title = fetchedArray[0]

    println(fetchedArray)

}

【问题讨论】:

  • “一些讨厌的错误”?具体是什么错误?该错误的内容可能是一个很好的线索。
  • 请提供您收到的具体错误消息,以便我们为您提供帮助。
  • 请查看更新后的问题,谢谢

标签: arrays core-data swift


【解决方案1】:

如果您没有收到特定的错误消息,这只是猜测...

NSArray 无类型内容(也就是说元素的类型为AnyObject),因此编译器可能认为您不能将AyObject 设置为字符串。所以可能需要这样的东西:

self.navigationItem.title = fetchedArray[0] as String

或者:

self.navigationItem.title = String(fetchedArray[0])

【讨论】:

    【解决方案2】:

    首先,我会检查几件事,看看问题出在获取的数组中还是在导航项中:

    1. fetchedArray[0] 中究竟返回了什么?是字符串吗?零?还有什么?它实际上在索引 0 处有任何东西吗?检查 fetchedArray 是否不为零并且有任何元素后,您可以使用 "\(fetchedArray[0])"
    2. navigationItem 的状态是什么?它实际上是在您尝试设置它的标题属性时实例化的吗?我建议检查它是否为零。你也可以尝试给它分配一个这样的字符串:self.navigationItem.title = "Hello",看看它是否失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2013-07-04
      • 2015-11-09
      • 1970-01-01
      • 2023-03-26
      • 2011-07-27
      相关资源
      最近更新 更多