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?

6
  • please share an error. Commented Sep 25, 2017 at 7:26
  • added more detail please check my updated answer. Commented Sep 25, 2017 at 7: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. Commented Sep 25, 2017 at 7:31
  • yes @iamyogish I have tried same by removing default values but still same issue no luck at all. Commented Sep 25, 2017 at 9: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. Commented Sep 28, 2017 at 6:23

3 Answers 3

8
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect I have created one more method without optional and its worked, Thanks a lot for your research on my issue.
Happy to help bro. Hey lets delete all the comments.
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

2 Comments

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
So try to change parameter 4 to non-optional and to remove it's default value. See if that helps.
0

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

3 Comments

can you show the class where you are defining init method.
I have added my swift file please check it once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.