我卡住了,这让我发疯了。我有一个返回NSManagedObject的函数:
let getaccount = AccountModel.getFirst(globals.managedObjectContext)
我必须将其强制转换为生成的代表Account
我会这样做:
dump("--------------------")
dump(getaccount)
dump("--------------------")
if let acc = getaccount as? Account {
print("OK")
} else {
print("not convertable ")
}
dump("*********************")
输出 FROM MY UNITTEST 如下所示:
- --------------------
▿ Ticketstream.Account
▿ Some: Coredataexampleapp.Account #0
▿ super: <Coredataexampleapp.Account: 0x138d85f90> (entity: Account; id: 0x1398a9fe0 <x-coredata://06F96EF3-96A6-4C32-A6A2-F16BCB8318EA/Account/p1> ; data: {
active = 0;
avatar = (
);
config = (
);
createdAt = "2015-07-31 23:34:58 +0000";
displayName = "My Account (1)";
host = localhost;
lastUpdatedDate = "2015-07-31 23:34:58 +0000";
password = nil;
port = 8080;
schema = HTTPS;
username = "";
})
- NSObject: <Coredataexampleapp.Account: 0x138d85f90> (entity: Account; id: 0x1398a9fe0 <x-coredata://06F96EF3-96A6-4C32-A6A2-F16BCB8318EA/Account/p1> ; data: {
active = 0;
avatar = (
);
config = (
);
createdAt = "2015-07-31 23:34:58 +0000";
displayName = "My Account (1)";
host = localhost;
lastUpdatedDate = "2015-07-31 23:34:58 +0000";
password = nil;
port = 8080;
schema = HTTPS;
username = "";
})
- --------------------
not convertable
- *********************
如何将 NSManagedObject 投射到帐户?
我需要这个,因为除了以下我不能获得第一个帐户:
class func getFirst(context: NSManagedObjectContext) -> NSManagedObject? {
let request = NSFetchRequest(entityName: accountEntityName)
request.fetchLimit = 1
let list: NSArray = try! context.executeFetchRequest(request)
return list.firstObject as? NSManagedObject
}
我已经尝试过使用NSManagedObject,AnyObject和Account ...我如何以正确的方式获得第一个帐户?
更新的: 在正常运行模式下:一切都很好!我会得到:
- --------------------
OK
- *********************
但不是我的测试:-(
感谢您的帮助,我迷失了。
答案 0 :(得分:0)
依次选择所有项目文件,然后查看右侧的文件检查器。确保所有文件都在其目标成员资格中选中了“测试”项目。
答案 1 :(得分:0)