86692

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?

Answer1:

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.

<hr>

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

(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.

<hr>

(Update2)

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

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

<hr>

(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.

Recommend

  • VBA Class Method Chaining
  • How can I seek key of dictionary by index value?
  • .NET Obfuscation [SmartAssembly]
  • MATLAB Combine matrices of different dimensions, filling values of corresponding indices
  • Interrupting boost thread
  • initializing array of variable size inside a class
  • Bokeh 0.7.1: Dynamically Add Plot to Bokeh-Server Generated Existing Page
  • using a UILocalNotification to play a sound that's not in the app bundle
  • How to set up precision attribute used by @Column annotation?
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • yii2 sorting in related model
  • How to create a data template dependent on an XML Attribute?
  • Mocking a imported function with pytest [duplicate]
  • Can you pass data to a test fixture just like you pass data to test cases?
  • Chrome breakpoint on radio doesn't fire
  • textIndicatorPrecision in dojox.dgauges using dojo
  • Tinymce strips attributes on submit
  • Rails 3, Custom Actions, and HTML request methods
  • how to pass class attribute and value to markdown syntax
  • How to check if DIV element is disabled using jquery
  • Yii2: Using Kartik Depdrop Widget?
  • CoreData basics – to-many relationship array data
  • Client side validation mvc dropdown
  • Should a web service response include empty values?
  • Best practice to eliminate magic numbers within a member function
  • Pass array from parent template to child template
  • Jquery Mobile pageLoading() Method how does it work?
  • Combining two different ActiveRecord collections into one
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Meteor: Do Something On Email Verification Confirmation
  • Initializer list vs. initialization method
  • Spring security and special characters
  • HTML download movie download link
  • Trying to switch camera back to front but getting exception
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache