Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift单元测试执行错误访问(代码=1,地址=0x8)_Ios_Xcode_Swift_Unit Testing_Core Data - Fatal编程技术网

Ios Swift单元测试执行错误访问(代码=1,地址=0x8)

Ios Swift单元测试执行错误访问(代码=1,地址=0x8),ios,xcode,swift,unit-testing,core-data,Ios,Xcode,Swift,Unit Testing,Core Data,我有一个名为Gift的托管对象,它具有以下属性 import Foundation import CoreData class Gift: NSManagedObject { @NSManaged var name: String @NSManaged var price: NSNumber @NSManaged var location: String } 使用单个单元测试,我成功地将一个实体插入到核心数据中(没有错误) 为了测试这是否完全成功,我使用一个fet

我有一个名为Gift的托管对象,它具有以下属性

import Foundation
import CoreData

class Gift: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var price: NSNumber
    @NSManaged var location: String

}
使用单个单元测试,我成功地将一个实体插入到核心数据中(没有错误)

为了测试这是否完全成功,我使用一个fetch请求返回这个数据

        let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: "Gift")
        var requestError: NSError? = nil
        if let gifts = managedObjectContext?.executeFetchRequest(fetchRequest, error: &requestError) {
            let aGift: NSManagedObject = gifts.first as! NSManagedObject
            //println("Gift name: \(aGift.name)")
            let string: String? = aGift.valueForKey("name") as? String
            println("Name: \(string)")
            var bGift: Gift = aGift as! Gift
            println("Name: \(bGift.name)")
            var a = 1
        }
    }
当我运行此测试时,将aGift转换为bGift
var bGift:Gift=aGift as的行失败!礼品
带有错误EXC_BAD_ACCESS(代码=1,地址=0x8)。然而,我确实从aGift中的核心数据中获得了结果,但作为一个NSManagedObject

这只发生在我在单元测试中运行代码时,如果我在应用程序中运行它,它将返回正确的信息并正确地强制转换它


测试时我做错了什么?

在本页找到了答案(问题后的第一个答案)

我无法强制转换的原因是NSManagedObject(Gift)完全限定类名是我的\u PROJECT.Gift,但当测试运行时,它会尝试将其强制转换为我的\u PROJECT\u TESTS.Gift。为了解决这个问题,我从核心数据模型UI中实体的类名中删除了MY_项目,因此它只需阅读Gift。然后,我将managedObjectModel代码重新编写到

lazy var managedObjectModel: NSManagedObjectModel = {
        // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
        let modelURL = NSBundle.mainBundle().URLForResource("GiftReminder", withExtension: "momd")!
        let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)!

        // Check if we are running as test or not
        let environment = NSProcessInfo.processInfo().environment as! [String: AnyObject]
        let isTest = (environment["XCInjectBundle"] as? String)?.pathExtension == "xctest"

        // Create the module name
        let moduleName = (isTest) ? "GiftReminderTests" : "GiftReminder"

        // Create a new managed object model with updated entity class names
        var newEntities = [] as [NSEntityDescription]
        for (_, entity) in enumerate(managedObjectModel.entities) {
            let newEntity = entity.copy() as! NSEntityDescription
            newEntity.managedObjectClassName = "\(moduleName).\(entity.name)"
            newEntities.append(newEntity)
        }
        let newManagedObjectModel = NSManagedObjectModel()
        newManagedObjectModel.entities = newEntities

        return newManagedObjectModel
    }()
这将动态检查我是否正在运行测试,并对类使用正确的预修复程序

lazy var managedObjectModel: NSManagedObjectModel = {
        // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
        let modelURL = NSBundle.mainBundle().URLForResource("GiftReminder", withExtension: "momd")!
        let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)!

        // Check if we are running as test or not
        let environment = NSProcessInfo.processInfo().environment as! [String: AnyObject]
        let isTest = (environment["XCInjectBundle"] as? String)?.pathExtension == "xctest"

        // Create the module name
        let moduleName = (isTest) ? "GiftReminderTests" : "GiftReminder"

        // Create a new managed object model with updated entity class names
        var newEntities = [] as [NSEntityDescription]
        for (_, entity) in enumerate(managedObjectModel.entities) {
            let newEntity = entity.copy() as! NSEntityDescription
            newEntity.managedObjectClassName = "\(moduleName).\(entity.name)"
            newEntities.append(newEntity)
        }
        let newManagedObjectModel = NSManagedObjectModel()
        newManagedObjectModel.entities = newEntities

        return newManagedObjectModel
    }()