此代码在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()")
}
}这个游乐场的动机是一个错误,当试图覆盖一个方法时,编译器抱怨说“不可用”
class MySFSafariViewController: SFSafariViewController {
override init() {
}
}当跳到目标C的时候。
@interface SFSafariViewController : UIViewController
/*! @abstract The view controller's delegate */
@property (nonatomic, weak, nullable) id<SFSafariViewControllerDelegate> delegate;
****- (instancetype)init NS_UNAVAILABLE;****发布于 2016-02-28 23:38:08
在Swift中,私有/内部/公共的含义与其他一些语言不同。
如果在项目中将类作为两个单独的文件来处理,那么这是非常清楚的。
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游乐场中,它们都位于一个文件中,因此该方法对B类可见。
发布于 2016-02-28 23:15:21
类A的myUnavailableMethod是私有的,因此不能重写它。通过删除internal关键字将方法声明更改为private。
https://stackoverflow.com/questions/35689477
复制相似问题