简体   繁体   English

Swift扩展“未找到方法定义”

[英]Swift extension “Method definition not found”

I'm writing a Swift extension to my ObjC class. 我正在为我的ObjC类写一个Swift扩展。 Although my code compiles and runs perfectly, I'm getting a bunch of Xcode warnings (one per every Swift method): 虽然我的代码编译和运行完美, 但我得到了一堆Xcode警告 (每个Swift方法一个):

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

It's dead simple to reproduce the Xcode message. 重现Xcode消息很简单。 I made this demo project with four lines of non-boilerplate code: 我用四行非样板代码制作了这个演示项目:

Objective-C (subclass of NSView) Objective-C(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 the Obj-C subclass) Swift(扩展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'm guessing the warnings would go away if I either: 我想如果我要么警告就会消失:

a) create a bunch of dummy methods in the .m file of my ObjC class. a)在我的ObjC类的.m文件中创建一堆伪方法。

b) in my Swift extension, extend my ObjC class's superclass. b)在我的Swift扩展中,扩展我的ObjC类的超类。

I don't love either of these solutions. 我不喜欢这些解决方案。

Is there a better way to make the compiler happy? 有没有更好的方法让编译器满意?

I'm not sure what's going on here, I think the problem is something to do with how NSView is bridged to the swift language. 我不确定这里发生了什么,我认为问题与NSView如何与快速语言桥接有关。

If you're going to override those methods, make sure you call the super implementation, since NSView will probably break spectacularly without them. 如果你要覆盖这些方法,请确保调用super实现,因为如果没有它们, NSView可能会突破。 However I'm not sure if even this is safe, you might still break NSView — perhaps the reason the errors occur is because of some weird/non-standard setup Apple is doing. 但是我不确定即使这是安全的,你仍然可能会破坏NSView - 也许是错误发生的原因是因为Apple正在做一些奇怪/非标准的设置。

If I was you, I'd just put up with the compiler warnings, and report the issue to Apple via http://bugreport.apple.com . 如果我是你,我会忍受编译器警告,并通过http://bugreport.apple.com向Apple报告问题。 Hopefully it will be fixed in a future beta release of Xcode 6. 希望它将在未来的Xcode 6测试版中修复。

This is an old question, but I am still seeing this problem with Xcode 7.3.1. 这是一个老问题,但我仍然看到Xcode 7.3.1的这个问题。

The reason why I cannot see the public function that is defined in the Objctive-C is because I didn't include all the class in the parameters in the bridge header . 我无法看到Objctive-C中定义的公共函数的原因是因为我没有在桥头中的参数中包含所有类 See the following example for more details: 有关详细信息,请参阅以下示例:

I have class defined in Objective-C, like such: 我在Objective-C中定义了类,如下所示:

Foo.h foo.h中

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

Foo.m Foo.m

@implementation Foo
// ...
@end

And in my bridge header I have #import "Foo.h" but not #import "Bar.h" . 在我的网桥标题中,我有#import "Foo.h"但不是#import "Bar.h" As a result, I can call function1 from my swift extension but not function2 . 因此,我可以从我的swift扩展中调用function1而不是function2

Foo.swift 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. 因此,将#import "Bar.h"添加到桥接头可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM