Swift accessor function for Objective-C class object

I am trying to implement some Swift framework in an Objective-C project. I already created all Bridge-Headers and also wrote @objc in front of the functions and classes in the swift class. So it should do the following, but the example code is listed in Swift:

barView.addBarBackground(startAngle: 90, endAngle: -270, radius: 100, width: 15)

      

I need to execute this code in Obj-c class for barView object. I tried to do this but it doesn't work:

[_barView.addBarBackground startAngle: 90 endAngle: -270 radius: 100 width: 15];

      

What should I do?

EDIT:

I have an IBOutlet connection:

@property (strong) IBOutlet OGCircularBarView *barView;

      

So there is an object named barView. I also have a Swift class with code:

import Cocoa

@objc public class OGCircularBarView: NSView, Sequence {
     //...
     @objc public func addBarBackground(startAngle: CGFloat, endAngle: CGFloat, radius: CGFloat, width: CGFloat, color: NSColor) {
        //some code
     }
     //...
}

      

I need to execute this code (which is specified in Swift) in an Objective-C class:

barView.addBarBackground(startAngle: 90, endAngle: -270, radius: 100, width: 15)

      

How can I rewrite it to work in Objective-C?

+3


source to share


1 answer


Try the following:

[_barView addBarBackgroundStartAngle: 90 endAngle: -270 radius: 100 width: 15];

      

The first problem is that you cannot use dot syntax for methods; what's the brackets for:[object message:withArguments:...]

Secondly, the Swift function name is a bit awkward to translate Obj-C. If you declare a swift function like this:



func addBarBackgroundWithStartAngle(_ startAngle: Type, endAngle: Type...

      

... this will be translated to be easier to read:

[_barView addBarBackgroundWithStartAngle: endAngle: ...]

      

Hope this helps.

+2


source







All Articles