BEMSimpleLineGraph icon indicating copy to clipboard operation
BEMSimpleLineGraph copied to clipboard

Problems with deprecated methods in Swift

Open mbeaty opened this issue 10 years ago • 9 comments

There are multiple versions of the lineGraph:labelOnXAxisForIndex: method. One is not Swift-compatible and deprecated and in the delegate protocol. The other is Swift-compatible and in the data source protocol. If your class conforms to both the delegate and the data source protocol, there is no way to properly use the Swift-compliant version of this method, because of the conflict between the two methods that get bridged. I can only use this method in Swift if I implement it as

func lineGraph(graph: BEMSimpleLineGraphView, labelOnXAxisForIndex index: Int) -> String! {...}

This is not correct and results in an Xcode warning. It should be

func lineGraph(graph: BEMSimpleLineGraphView, labelOnXAxisForIndex index: Int) -> String? {...}

but due to the conflict introduced by conforming to both protocols, it's impossible to implement it that way.

mbeaty avatar Jun 19 '15 23:06 mbeaty

Is there any way to work around this? I tried creating a separate delegate class and xcode stopped complaining and I could implement the labelOnXAxisForIndex method, but my x axis labels still don't show. I'm not sure if the problem is related to this or if I'm doing something wrong. Should this work with a separate delegate?

dbmrq avatar Nov 12 '15 09:11 dbmrq

@danielbmarques A workaround that worked for me was to define the labelOnXAxisForIndex delegate call with a String! return value e.g.

func lineGraph(graph: BEMSimpleLineGraphView, labelOnXAxisForIndex index: Int) -> String! 

While not entirely correct, it does allow the X axis labels to be displayed, at the cost of a couple of warnings in the project.

I think this is actually an Apple/Swift bug, since the non-optional return version is attributed with __unavailable which should not allow it to be visible to the Swift compiler.

slangley avatar Feb 01 '16 20:02 slangley

I'm encountering this as well in my Swift project.

AvdLee avatar Feb 10 '16 16:02 AvdLee

@Boris-Em are you open to a pull request that will remove the deprecated delegate calls?

slangley avatar Feb 10 '16 16:02 slangley

@slangley, Absolutely. Just make sure that you take a quick look at the contributing document.

Boris-Em avatar Feb 10 '16 18:02 Boris-Em

Don't want to be Johnny Raincloud :cloud: here, but just keep in mind that removing methods from the public API is a breaking change and thus requires (according to semantic versioning) upping the first digit in the release number. This isn't really a huge issue, but it would be nice to bundle a few more breaking changes into the same major release (this will save some headache for those using Cocoapods, or who may have deep integration of this project, etc).

So to compromise, if you do a pull request, can you send it to the feature branch rather than master? That will allow us to make a few more changes in the same release before making it final.

Sam-Spencer avatar Feb 11 '16 04:02 Sam-Spencer

@mbeaty @danielbmarques @slangley @AvdLee This issue should be fixed in the latest commit to the feature branch (83e8acd). Hope this helps!

Sam-Spencer avatar Feb 21 '16 21:02 Sam-Spencer

I still have a problem with the method - (nullable NSString *)lineGraph:(nonnull BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index; method in Swift using version 4.1 of the framework through CocoaPods.

Declaring the function as func lineGraph(_ graph: BEMSimpleLineGraphView, labelOnXAxisFor index: Int) -> String? in BESimpleLineGraphDataSource as suggested by Xcode, I get the error

Result of 'lineGraph(_:labelOnXAxisFor:)' has different optionality than required by protocol 'BEMSimpleLineGraphDelegate'

If I use the suggested fix and change the return type to String, I get a warning saying

Result of 'lineGraph(_:labelOnXAxisFor:)' has different optionality than expected by protocol 'BEMSimpleLineGraphDataSource'

@Sam-Spencer Is the fix to this still not implemented in version 4.1, shall I be using the master branch in my pod instead?

davidpasztor avatar Mar 12 '18 20:03 davidpasztor

For those still perusing this old issue, the signature for x axis labels has been changed to:

func lineGraph(_ graph: BEMSimpleLineGraphView, labelOnXAxisFor index: UInt) -> String?

Namely, the data type has changed from Int to UInt for the index.

danejordan avatar May 07 '18 23:05 danejordan