0

此代码在 Playground 中有效,但是当我在 Xcode 7.2 的项目中定义它时出现编译错误

这是我的游乐场截图 https://goo.gl/yJ4Q75

错误是:方法没有覆盖超类中的任何方法

public class A {
    private func myUnavailableMethod() {
        print("A. private func myUnavailableMethod()")
    }
}

public class B : A {  
    override func myUnavailableMethod() {
        print("B. func myUnavailableMethod()")
    }
}

尝试覆盖方法时,此 Playground 的动机是一个错误,编译器抱怨为“不可用”

class MySFSafariViewController: SFSafariViewController {
    override init() {

    }
}

---- 发现他们如何将方法标记为不可用。

跳转到 Objective C 声明时。

@interface SFSafariViewController : UIViewController

/*! @abstract The view controller's delegate */
@property (nonatomic, weak, nullable) id<SFSafariViewControllerDelegate> delegate;

****- (instancetype)init NS_UNAVAILABLE;****
4

2 回答 2

1

与其他一些语言相比,Swift 中私有/内部/公共的含义是不同的。

IF 并且它是一个 IF,您将您的类作为项目中的两个单独的文件,那么它就很清楚了。

private - scope is visibility is the file that holds the code
internal - scope of visibility is the namespace
public - scope of visibility is full access from anywhere 

在 Xcode Playground 中,它们都在一个文件中,因此该方法对 B 类可见。

于 2016-02-28T23:38:08.797 回答
0

AmyUnavailableMethod类的 是私有的,因此不能被覆盖。internal通过删除private关键字将方法声明更改为。

于 2016-02-28T23:15:21.887 回答