微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 为什么添加“动态”修复我的错误访问问题?

我有一个奇怪的问题,出现在iOS 8 Beta 5(此问题没有发生在以前的版本)。

我试图创建一个空项目,并尝试复制的问题,但我不能这样做,所以我不知道问题在哪里。

我看到的是,尝试访问自定义NSManagedobject子类的方法会导致奇怪的EXC_BAD_ACCESS错误

例如:

var titleWithComma: String {
       return "\(self.title),"
  }

这种方法,在许多其他的,导致此问题时调用。但是,在导致问题消失之前添加动态关键字:

dynamic var titleWithComma: String {
       return "\(self.title),"
  }

我知道我没有提供足够的信息,因为我真的不知道如何指出实际问题,但任何人都可以解释什么是可能发生的,为什么添加动态可能解决这个问题?

从Swift语言参考(语言参考>声明>声明修饰符)

Apply this modifier to any member of a class that can be represented
by Objective-C. When you mark a member declaration with the dynamic
modifier,access to that member is always dynamically dispatched using
the Objective-C runtime. Access to that member is never inlined or
devirtualized by the compiler.

Because declarations marked with the dynamic modifier are dispatched
using the Objective-C runtime,they’re implicitly marked with the objc
attribute.

这意味着您的属性/方法可以通过Objective-C代码或类访问。通常,它发生在你分类一个Swift类的Objective-C基类。

原文地址:https://www.jb51.cc/swift/320828.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐