[swift-users] So how do you implement a NSTextStorage subclass in Swift?
Michel Fortin
michel.fortin at michelf.ca
Mon Feb 13 13:59:11 CST 2017
Le 12 févr. 2017 à 18:41, Karl Wagner <razielim at gmail.com> a écrit :
>
> I've seen this before; I considered it something to be resolved in AppKit. Strings in Swift are values, and the framework expects it to be a reference. Since NSTextStorage is itself a reference-type and informs its delegate of changes, those frameworks should probably retain the NSTS itself and pull Strings out as-needed.
Well, this is apparently not an AppKit problem because the `string` method is first defined in `NSAttributedString` in Foundation. `NSTextStorage` only inherits that method.
The cleanest fix I can see at the framework level is expose it as two properties in `NSAttributedString`:
// swift-only getter for the string, can't override this one
@nonobjc
final public var string: String {
get { return backingString as String }
}
// this is the one mapped to `string` in objc, you can override this one
@objc(string)
public var backingString: NSString { get }
This is somewhat analogous to what I'm doing with `method_setImplementation` in my solution to remap the method to a Swift method that has the right signature.
That would be source-breaking, but only for those who override the property. Pretty much all of these overrides are going to be violating the API contract anyway. They probably deserve the error so they can be fixed.
At this point though, I think the topic belong to somewhere else than swift-users. I'm just not sure where. I should probably file a radar against Foundation at the very least.
--
Michel Fortin
https://michelf.ca
More information about the swift-users
mailing list