Using Swift Clusters with Overloaded Methods in Objective-C

My project currently uses the Swift and Objectice-C languages. To make it work, Ive followed the recommendations of the official documentation . And now I can use Swift in Objective-C and vice versa.

But the problem arises when some of the Swift clusters have overloaded methods. Swift supports method overloading, but not Objective-C.

Example:

func random(#lower: Int , upper: Int) -> Int {
    return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}

func random(#lower: CGFloat , upper: CGFloat) -> CGFloat {
    return CGFloat(arc4random() % 1) / 0
}

The automatically generated file "MyNameProductModule-Swift.h" cannot compile because there are methods with the "same" signature.

Questions:

a) Did I miss something important?

b) Is there any solution other than renaming the methods?

c) Framework/Library Swift, , , Framework/Library Objective C ?

!

+4
3

swift2.0 , Objective C:

@objc(randomLoInt:HiInt:)
func random(#lower: Int , upper: Int) -> Int {
    return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}

@objc(randomLoFlt:HiFlt:)
func random(#lower: CGFloat , upper: CGFloat) -> CGFloat {
    return CGFloat(arc4random() % 1) / 0
}

, :

@nonobjc
func random(#lower: Int , upper: Int) -> Int {
    return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}

@nonobjc
func random(#lower: CGFloat , upper: CGFloat) -> CGFloat {
    return CGFloat(arc4random() % 1) / 0
}
+5

a) - ?

,

b) - , ?

, . , , , , Objective-C. , Swift .

c) Framework/Library Swift, , Framework/Library Objective C ?

, , Objective-C, Swift, , API Objective-C. Swift, Objective-C. , API, Objective-C.

+5

, . , , .

Stanford's course was made when an older version of Swift and Xcode was present, and Xcode did not respond to this error. It was a bug in Xcode that allowed to override this method. You just need to write a method with a different name in order to achieve what you want.

https://www.reddit.com/r/swift/comments/34yusl/help_with_function_overloading/

0
source

All Articles