将NSPersistentStoreResult强制转换为数组

很多

嗨,我在下面的代码上遇到了麻烦。具体来说,if let语句将Cast from转换NSPeristentStoreResult为不相关的类型[Item]始终会发出警告。我正在使用Swift 3.01。

看起来这应该很简单。我关注的书是使用Swift的早期版本编写的。感谢您的放纵。

func demo(){

let request = NSFetchRequest<Item>(entityName: "Item")

  do {
     if let items = try CDHelper.shared.context.execute(request) as? [Item] {
        for item in items {
           if let name = item.name {
              print("Fetched Managed Object = '\(name)'")
           }
        }
     }
  } catch {
     print("Error executing a fetch request: \(error)")
  }
 }
浅思

使用fetch()代替execute()

if let items = try CDHelper.shared.context.fetch(request)
...

perform在您的上下文中使用

 CDHelper.shared.context.perform {
      let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()            
      let items = try! fetchRequest.execute() 
      for item in items {
           if let name = item.name {
                print("Fetched Managed Object = '\(name)'")
           }
      }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从“ NSPersistentStoreResult”强制转换为无关类型“ Entity”总是失败

从“ NSPersistentStoreResult”强制转换为不相关的类型“ [entity]”始终失败

将null强制转换为对象数组是否比强制转换为对象花费更多?

将char数组强制转换为struct指针

将Int数组强制转换为Enum标志

将值从数组强制转换为findViewById id

如何将var强制转换为长数组?

将结构强制转换为数组是否合法?

错误无法使用Mulesoft将数组强制转换为对象

将stdClass强制转换为数组-键不可访问

将原始数组转换为装箱数组时如何减少强制转换次数

将TreeMap转换为两个数组,强制转换问题

强制转换数组(将组件转换为JMenuItem)不起作用

将数组强制转换为以一个数组为成员的结构是否安全?

为什么将Object数组强制转换为Generic数组合法?

将数组转换为数组

Typescript通用数组被强制转换为any []?

数组不能被强制转换为整数(TypeError)

如何从char数组强制转换为double

为什么将开放数组参数强制转换为数组类型会导致E2089无效类型转换?

将ArrayList <String>强制转换为String []

将接口强制转换为类?

将界面{}强制转换为类型

将 null 强制转换为类型

将constexpr int []强制转换为void *

将通用T强制转换为Object []

无法将TextInputEditText强制转换为TextInputLayout

无法将SerializableCoder强制转换为BeamRecordCoder

将undefined强制转换为0