Quick extension "Method definition not found"

I am writing a Swift extension for my ObjC class. Although my code compiles and works fine, I get a bunch of Xcode warnings (one for each Swift):

"Method definition for 'foo_method:' not found" "Method definition for 'bar_method:' not found" "Method definition for 'baz_method:' not found" 

He is dead just to play an Xcode message. I did this demo project with four lines of custom code:

Objective-C (subclass of NSView)

 // Subclass_of_NSView.h #import <Cocoa/Cocoa.h @interface Subclass_of_NSView : NSView @end // Subclass_of_NSView.m @implementation Subclass_of_NSView - (instancetype)initWithFrame:(NSRect)frame //______________^ WARNING: Method definition for resizeSubviewsWithOldSize: not found { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } @end 

Swift (extends a subclass of Obj-C)

 // Extension_of_Subclass.swift import Foundation extension Subclass_of_NSView { override func resizeSubviewsWithOldSize( old_bounds_size:NSSize ) { } } 

Bridging header

 // Demo_Project-Bridging-Header.h #import "Subclass_of_NSView.h" 

I assume the warnings will disappear if I, too:

a) create a bunch of dummy methods in the .m file of my ObjC class.

b) in my Swift extension, extend the superclass class of the ObjC class.

I do not like any of these solutions.

Is there a better way to make the compiler happy?

+6
source share
3 answers

I'm not sure what is going on here, I think the problem is with the way NSView connects to the fast language.

If you intend to override these methods, make sure that you call the super implementation, since NSView will probably NSView without them. However, I’m not sure that even if it’s safe, you can still break NSView - perhaps the reason the errors occur is due to some strange / non-standard setting that Apple does.

If I were you, I just agreed to the compiler warnings and reported an Apple issue through http://bugreport.apple.com . Hopefully it will be fixed in a future beta of Xcode 6.

+2
source

This is an old question, but I still see this problem with Xcode 7.3.1.

The reason I don't see the public function defined in Objctive-C is because I did not include the entire class in the parameters in the bridge header . See the following example for more details:

I have a class defined in Objective-C, for example:

foo.h

 @interface Foo : NSObject - (void)function1; - (void)function2:(Bar *)para; // here Bar is just another object @end 

Foo.m

 @implementation Foo // ... @end 

And in my bridge header there is #import "Foo.h" , but not #import "Bar.h" . As a result, I can call function1 from my quick extension, but not function2 .

Foo.swift

 extension Foo { func function3() { function1(); // Okay function2(nil); // Method definition not found } } 

So, adding #import "Bar.h" to the bridge header solves the problem.

+2
source

I had similar problems (although I could not reproduce the problem from the code you provided). I don’t think this has anything to do with how NSView or any other class connects to Swift. I think this is mainly due to visibility issues. The Objective-C class and the Swift extension cannot see inside each other.

Thus, for example, if the Objective-C class has a foo method, the Swift extension cannot call foo() because it cannot see it. The workaround is to declare foo in the Objective-Ch (header) file, which is then imported into the bridge header.

But this trick brings you still; in the end, you simply reach the point where the inability of the two codebases becomes overwhelming, and it is easiest to write the whole class in only one language or the other.

0
source

Source: https://habr.com/ru/post/971098/


All Articles