6

I have One Initializer method in my Swift file like below:

public init(frame: CGRect, type: NVActivityIndicatorType? = nil, color: UIColor? = nil, padding: CGFloat? = nil) {
        self.type = type ?? NVActivityIndicatorView.DEFAULT_TYPE
        self.color = color ?? NVActivityIndicatorView.DEFAULT_COLOR
        self.padding = padding ?? NVActivityIndicatorView.DEFAULT_PADDING
        super.init(frame: frame)
        isHidden = true
}

I want to call this method from my Objective-C file but it's throwing error while compiling.

Error:

/Users/Desktop/old data/ChatScreenViewController.m:396:92: No visible @interface for 'NVActivityIndicatorView' declares the selector 'initWithFrame:type:color:padding:'

Obj-C calling code:

NVActivityIndicatorView *objNVActivityIndicatorView = [[NVActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) type:NVActivityIndicatorTypeLineScalePulseOut color:[UIColor blueColor] padding:10]

What I tried:

Added @objc in my Swift class

@objc public final class NVActivityIndicatorView: UIView

Still can't able to access the above method.

My Swift file: NVActivityIndicatorView.swift

What's going wrong?

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
  • please share an error. – KDeogharkar Sep 25 '17 at 07:26
  • added more detail please check my updated answer. – CodeChanger Sep 25 '17 at 07:27
  • If I recall correctly, the cause of this issue would be using the default argument value in your init. Please remove that default values and give it a shot. It should work. – iamyogish Sep 25 '17 at 07:31
  • yes @iamyogish I have tried same by removing default values but still same issue no luck at all. – CodeChanger Sep 25 '17 at 09:25
  • Check your generated header file (the name should be something like `MyApp-Swift.h`. See how (and if) the method is exposed to Objective-C in that header. This may give us some clues as to what's going on. Also, if you haven't already, make sure your Objective-C sources are including that header. – Charles Srstka Sep 28 '17 at 06:23
  • https://stackoverflow.com/questions/24078043/call-swift-function-from-objective-c-class check this – Yuyutsu Sep 28 '17 at 06:59

3 Answers3

8

To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.

And also this answer.

This apple doc will also help you understand.

So the options for you is to either inherit the class of yours from some Objective C class (In this case compiler adds @objc) or add @objc yourself to your class.

You also need to add to your Objective C class-

#import "<#ProjectName#>-Swift.h"

Real solution to this specific question

Please look at this question, you will get your solution yourself. :)

That question says that optional parameters can't be exposed from Swift to Objective C as all parmeters become _Nonnull.

So I think you will need to create another initializer without optionals and default parameters.

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
3
  1. Try to add @objc also to the function:

    @objc public init(frame: CGRect, type: NVActivityIndicatorType? = nil, color: UIColor? = nil, padding: CGFloat? = nil) {

  2. You need to create a Bridging Header. See https://medium.com/ios-os-x-development/swift-and-objective-c-interoperability-2add8e6d6887

Witterquick
  • 6,048
  • 3
  • 26
  • 50
  • its not allowed due to this reason `Method cannot be marked @objc because the type of the parameter 4 cannot be represented in Objective-C` – CodeChanger Sep 28 '17 at 06:32
  • So try to change parameter 4 to non-optional and to remove it's default value. See if that helps. – Witterquick Sep 28 '17 at 06:39
0

Try to Add @objc to Swift class in Build Phases -> Compile Source and call the method again. I hope this will work.

Nupur Gupta
  • 305
  • 1
  • 12