You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-5475 Can't set property declared optional in protocol
Issue Description:
When dealing with protocols bridged from Objective-C and subtype existentials, Swift generates an error claiming that the instance is a let constant and var should be used. Consider the following code:
test.swift:14:7: error: cannot assign to property: 'b' is a 'let' constant
b.foo = ""
~ ^
test.swift:12:1: note: change 'let' to 'var' to make it mutable
let b: AandP = B()
^~~
var
This can be resolved by making the property in P required (i.e. removing @optional). This is mostly an issue when dealing with UIKit protocols, since many of their protocol requirements are optional.
The text was updated successfully, but these errors were encountered:
This is pretty confusing to me. I'd be grateful to know if there's a workaround that doesn't involve modifying the protocol, since the protocol I'm dealing with in the real world lives in UIKit.
:-( I think the only workaround right now is to define an Objective-C function that does it (it can be static inline). We've put off dealing with this because it comes up fairly rarely.
Darn okay. I've come up with another workaround that's slightly more verbose, but it helps when dealing with UIKit (or other one off cases where something like this is needed). In my real word case, I want to configure the UITextInputTraits keyboard properties the same way for both a UITextView and a UITextField. To accomplish that, I ended up with something like this:
protocolTextInputKeyboardConfiguration: class {
varkeyboardType: UIKeyboardType { getset }
/* copy pattern here for other configuration you want to do */
}
extensionUITextField: TextInputKeyboardConfiguration {}
extensionUITextView: TextInputKeyboardConfiguration {}
// You can now use a subtype existential like this to manage their setup (add as subviews, configure layout, and configure UITextInputTraits properties that you add to this protocol):typealiasKeyboardConfigurableView = UIView & TextInputKeyboardConfiguration
Environment
macOS 10.13
Xcode 9 (App Store version)
Swift 4 (not in Swift 3 compatibility mode)
Additional Detail from JIRA
md5: ec35d24fb9e531b65834eac5a629f857
duplicates:
Issue Description:
When dealing with protocols bridged from Objective-C and subtype existentials, Swift generates an error claiming that the instance is a let constant and var should be used. Consider the following code:
SampleProtocols.h
test.swift
Test-Bridging-Header.h
When building using the following commands:
This generates the following error:
This can be resolved by making the property in
P
required (i.e. removing@optional
). This is mostly an issue when dealing withUIKit
protocols, since many of their protocol requirements are optional.The text was updated successfully, but these errors were encountered: