Husein Behboodi Rad Husein Behboodi Rad - 5 months ago 50
iOS Question

No setter method for assignment to property swift 3 with objective-c

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()
}

}


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


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;
}


is it now neccessary to define seetters manually?

Answer

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:).

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
    }
}
Comments