简体   繁体   English

Swift:executeFetchRequest:无法转换'Any Object'类型的值

[英]Swift: executeFetchRequest : Cannot convert value of type 'Any Object'

I have some problem about executeFetchRequest 我有一些关于executeFetchRequest的问题

import UIKit
import CoreData

class BeaconDB: NSObject {

    var addStatus: BeaconAddStatus!


    enum BeaconAddStatus{
          case DUPLICATE_IN_AD
          case ADDED_SUCCESSFULL
          case ERROR_IN_ADD
     }


    func addNewBeacon(beacon: BeaconData) -> BeaconAddStatus{
          print("ADDNewBeacon")
          print("uuid: %@ \(beacon.uuid)")

    let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context: NSManagedObjectContext = appDelegate.managedObjectContext

    let entityDesc: NSEntityDescription = NSEntityDescription.entityForName("Beacon", inManagedObjectContext: context)!
    let request: NSFetchRequest = NSFetchRequest()
    let predSearch: NSPredicate = NSPredicate(format: "(uuid = %@) AND (major = %@) AND (minor = %@) \(beacon.uuid), \(beacon.major), \(beacon.minor)")

    request.entity = entityDesc
    request.predicate = predSearch

    do {
        let existingBeacon: Beacons = try context.executeFetchRequest(request)
        // success ...
    } catch let error as NSError {
        // failure
        print("Fetch failed: \(error.localizedDescription)")
    }

}

}

Mv Beacons class is NSManagedObject Mv Beacons类是NSManagedObject

  import UIKit

  import CoreData

  class Beacons: NSManagedObject {

      dynamic var major : NSNumber = 0.0
      dynamic var minor : NSNumber = 0.0
      dynamic var name : NSString = ""
      dynamic var uuid : NSString = ""
   }

But it's error from this line 但是这条线的错误

 let existingBeacon: Beacons = try context.executeFetchRequest(request)

Cannot convert value of type ['Any Object'] to specified type 'Beacons' 无法将['Any Object']类型的值转换为指定类型'Beacons'

Help me please Thank you :)) 请帮帮我谢谢:))

Two issues: 两个问题:

  • executeFetchRequest returns always an array of objects. executeFetchRequest始终返回一个对象数组
  • You have to cast the type. 你必须施放类型。

let existingBeacons = try context.executeFetchRequest(request) as! [Beacons]

Cannot convert value of type ... to specified type ... means usually the compiler needs help by casting the type or there is no relation at all. 无法将类型的值...转换为指定的类型...通常意味着编译器需要通过转换类型来提供帮助,或者根本没有任何关系。

PS: It's recommended to name Core Data entities in the singular form to avoid confusion. PS:建议以单数形式命名Core Data实体以避免混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:无法将'[Any]'类型的值转换为指定类型'NSString' - Swift: Cannot convert value of type '[Any]' to specified type 'NSString' 无法转换类型的值-Swift - Cannot convert value of type - Swift Swift“无法将''类型的值转换为'字符串'类型 - Swift "Cannot convert value of type ' ' to type ' String ' 无法转换[Any]类型的值! 在for3循环中在Swift3中键入强制'MySpecificClass' - Cannot convert value of type [Any]! to type 'MySpecificClass' in coercion: in Swift3 while using for in loop Swift 2.2,RealmSwift-无法将“注释”类型的值转换为预期的参数类型Object.Type - Swift 2.2, RealmSwift - Cannot convert value of type 'Note' to expected argument type Object.Type 无法在Swift中将viewController对象转换为viewController类型 - Cannot convert a viewController object to viewController type in Swift swift 2无法转换“ [NSLayoutConstraint]”类型的值错误 - Cannot convert value of type '[NSLayoutConstraint]' error in swift 2 “无法转换类型的值”-Firebase Swift中的错误 - “cannot convert value of type” - error in Firebase Swift Swift 3.0 错误“无法转换类型的值” - Swift 3.0 Error "Cannot convert value of type" Swift3中的“无法转换类型的值”错误 - “Cannot convert value of type” error in Swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM