Swift and ObjC interoperability

Hi!


In the ios-charts repo, I've had to jump through crooked hoops to make everything available in ObjC.

Some function interfaces are not exported to ObjC, and you can see an example here:

https://github.com/danielgindi/ios-charts/blob/master/Charts/Classes/Components/ChartLegend.swift

In the functions `extraColorsObjc`, `colorsObjc` etc.


But the weirdest thing is where declaring a variable in the function header as mutable, it prevents the function from being exposed in ObjC for not reason at all!

See `setMinimumScaleX`( where I've had to store that variable in a local variable in order to change it unintuitively) in https://github.com/danielgindi/ios-charts/blob/5eefb8fac453d26b65c47f22ab36d484d7de5854/Charts/Classes/Utils/ChartViewPortHandler.swift


Now is there something else that could be done that I'm missing? Maybe a special specifier to make those expose naturally instead of making those "hacks"?


Thanks!

Daniel

The reason why the properties/methods like extraColors and extraLabels aren't automatically available in Objective-C is because they use arrays of optionals (which are both Generics and Enumerations defined in Swift without Int raw value type).


Also, objC users of your framework may have trouble with using the features which rely on nil being in the array, even with your work-around methods, since obj-c doesn't allow nil objects in an array.



For setMinimumScaleX() you can use the ternary ? operator to avoid the need for an intermediate variable.


public func setMinimumScaleX(xScale: CGFloat)
    {
        _minScaleX = (newValue < 1.0) ? 1.0 : xScale
        limitTransAndScale(matrix: &_touchMatrix, content: _contentRect);
    }


https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID71

Well I could use a ternary operator, but it's not what I want there. I like the code being more spaced - and in the future there might be more handling to do on the incoming variable.

What I want to understand is the root cause: Why would declaring that as `var` be causing it to hide from ObjC?


About the bridging functions that I created for those arrays - it's expecting NSNulls, so there's no nil problem there. But It's a shame that we have to take such measures when in other places in the language the native bridging happens perfectly...

It may not help at the moment, but your func setMinimumScaleX(var xScale: CGFloat) function will be available as an objC method once you update to Swift 2.


In the meantime, for Swift 1.2 you could write a temporary wrapper function to export to objC instead, if you would prefer to have the Swift code working without the temporary variable.

@objc(setMinimumScaleX:) public func setMinimumScaleX(#xScaleObjC: CGFloat)
{
    setMinimumScaleX(xScaleObjC)
}

public func setMinimumScaleX(var xScale: CGFloat)
{
    if (xScale < 1.0) {xScale = 1.0}
     
    _minScaleX = xScale
     
    /* ... */
}



As far as the extraColors and extraLabels properties/methods go, I'm assuming you have read the Using Swift with Cocoa and Objective C book from Apple, and know why they aren't being exported to objective C?

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID136

I don't think automatic bridging of arrays of optionals between Swift and objC is likely to be added in the future, because of Swift's explicitly typed arrays and objC's collection types not accepting nil values and needing the NSNull instance.

Swift and ObjC interoperability
 
 
Q