swift_dynamicCastClassSwift中的无条件问题 [英] swift_dynamicCastClassUnconditional issues in swift

查看:190
本文介绍了swift_dynamicCastClassSwift中的无条件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迅速拥有以下NSManagedObject:

import Foundation
import CoreData

public class User: NSManagedObject {

    @NSManaged public var first_name: String
    @NSManaged public var last_name: String
}

我有一个提供以下功能的类:

I have a class which provides the value with a function below:

- (id)getUserAtIndexPath:(NSIndexPath *)indexPath {
    User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];
    return user;
}

当我从Objective-C类调用此函数时,一切正常.我这样叫:

When I call this function from an Objective-C class, everything works fine. I call like so:

User *user = [self.userAccessor getUserAtIndexPath:indexPath];

我可以从那里继续.当我像这样从Swift类调用它时:

And i'm able to continue from there. When I call it from a Swift class like so:

let user: User = self.userAccessor.getUserAtIndexPath(indexPath) as User

我每次都得到swift_dynamicCastClassUnconditional.我做错了什么吗?为什么同一功能在Objective-C中起作用,而不在Swift中起作用?我什至设置了一个断点,并可以在Swift调用上确认返回值是有效变量! Swift只是缺少它的一些原因.

I get the swift_dynamicCastClassUnconditional every time. Is there anything i'm doing wrong? Why is the same function working in Objective-C and not Swift? I even put a break point and can confirm on the Swift call the return is a valid variable! Some how Swift is just missing it.

我尝试阅读以下内容:

http: //jamesonquave.com/blog/understanding-the-fatal-error-cant-unwrap-optional-none-errors-in-swift/

由NSFetchRequest返回的AnyObject数组,错误与快速动态投放失败";在XCTestCase中投放到Swift XCode 6 Beta 4中

还有什么我想念的吗?

推荐答案

我在这里找到了解决方案:

I got the solution here:

Swift:CoreData库中的断点

基本上,我不得不将@objc(User)放在受管理对象代码的正上方.因此,如下所示:

Basically I had to put @objc(User) right above managed object code. So as per below:

@objc(User)
public class User: NSManagedObject {
    @NSManaged public var first_name: String
    @NSManaged public var last_name: String
}

这解决了问题.结案了.

This solved the problem. Case closed.

这篇关于swift_dynamicCastClassSwift中的无条件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆