没有使用objective-c分配给属性swift 3的setter方法 [英] No setter method for assignment to property swift 3 with objective-c
问题描述
我在我的一个 swift 类中使用以下代码:
I am using below code in one of my swift classes:
import UIKit
public class BRUFIMobile: NSObject {
public var mobileNumber : NSString?
public var provider : NSString?
public var aliasName: NSString?
public var aliasName_en : NSString?
@objc public var isDefault: Bool
public override init() {
self.isDefault = false
super.init()
}
}
我的项目包含objective-c 和swift 代码.该代码在 xCode 7 上运行良好,但现在通过更新到 xCode 8,我得到以下错误:
My project contains both objective-c and swift codes. The code works fine on xCode 7 but now by updating to xCode 8 I get below error:
No setter method 'setIsDefault:' for assignment to property
我在我的objective-c 代码中使用属性,如下所示:
I use the attribute in my objective-c code like below:
+ (BRUFIMobile *) convertUFIMobileToBRUFIMobile:(UFIMobile *) ufimobile
{
BRUFIMobile *brUFIMobile = [[BRUFIMobile alloc] init];
brUFIMobile.mobileNumber = ufimobile.number;
brUFIMobile.provider = ufimobile.provider;
brUFIMobile.aliasName = ufimobile.aliasName;
brUFIMobile.aliasName_en = ufimobile.aliasName_en;
brUFIMobile.isDefault = [ufimobile.isDefault boolValue];
return brUFIMobile;
}
现在是否需要手动定义 Seetter?
is it now neccessary to define seetters manually?
推荐答案
Swift 3 beta 对带有前导 is
的属性进行了特殊处理.对于属性isDefault
,Swift 生成isDefault
作为getter 和setDefault:
作为setter(不是setIsDefault:
).
Swift 3 beta makes special treatment for properties with leading is
.
For the property isDefault
, Swift generates isDefault
as getter and setDefault:
as setter (not setIsDefault:
).
在您的 Objective-C 代码中使用 setDefault:
,或将其设为计算属性并手动指定 @objc
名称.
Use setDefault:
in your Objective-C code, or make it a computed property and give @objc
names manually.
(补充)使其成为计算属性"的意思是这样的:
(Addition) "make it a computed property" means something like this:
private var _default: Bool = false
public var isDefault: Bool {
@objc(isDefault) get {
return _default
}
@objc(setIsDefault:) set {
self._default = newValue
}
}
<小时>
(更新)据我测试,这似乎有效:
(Update) As far as I tested, this seems to work:
@objc(isDefault) public var isDefault: Bool
比使其成为计算属性"简单一点.请尝试.
A little bit simpler than "making it a computed property". Please try.
(更新 2)
Apple 工作人员已确认这是一个已知问题,将在未来的测试版中修复:"https://github.com/apple/swift/pull/3254
Apple Staff has confirmed that "This is a known issue that will be fixed in a future beta:" https://github.com/apple/swift/pull/3254
这不是 Core Data 案例.但此修复程序也适用于此问题.(很遗憾,就目前而言,Xcode 8 beta 2 不包含此修复程序.)
This is not a Core Data case. But the fix will work for this issue as well. (It's a pity, as for now, Xcode 8 beta 2 does not include this fix.)
(更新 3)在 Xcode 8 beta 3 Release Notes 中说这个功能被删除了,我已经确认,没有 @objc
属性,Objective-C setter setIsDefault:
可用于Bool
属性 isDefault
.
(Update3)
It is said in Xcode 8 beta 3 Release Notes that this feature is removed and I have confirmed that, with no @objc
attribute, Objective-C setter setIsDefault:
is available for Bool
property isDefault
.
这篇关于没有使用objective-c分配给属性swift 3的setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!