[英]swift 2.1, xcode 7.2. simple NEWBIE about why it works in Playground but not in application
This code works in Playground, but I get a compile error when I define this in my project in Xcode 7.2 该代码在Playground中有效,但是在Xcode 7.2的项目中定义此代码时出现编译错误
Here is my Playground screenshot https://goo.gl/yJ4Q75 这是我的游乐场屏幕截图https://goo.gl/yJ4Q75
Error is: method does not override any method in the super class 错误是:方法未覆盖超类中的任何方法
public class A {
private func myUnavailableMethod() {
print("A. private func myUnavailableMethod()")
}
}
public class B : A {
override func myUnavailableMethod() {
print("B. func myUnavailableMethod()")
}
}
Motivation to this Playground was an error when trying to override a method, compiler was complaining as "Not available" 尝试覆盖方法时,此游乐场的动机是错误,编译器抱怨为“不可用”
class MySFSafariViewController: SFSafariViewController {
override init() {
}
}
---- FOUND HOW THEY MARKED a method as unavailable. ----发现如何将其标记为不可用的方法。
When jumping to the Objective C declaration. 跳转到目标C声明时。
@interface SFSafariViewController : UIViewController
/*! @abstract The view controller's delegate */
@property (nonatomic, weak, nullable) id<SFSafariViewControllerDelegate> delegate;
****- (instancetype)init NS_UNAVAILABLE;****
The meaning of private/internal/public is different in Swift compared to some other languages. 与其他一些语言相比,Swift中的private / internal / public的含义有所不同。
IF and it is an IF you have your classes as two separate files in the project, then it's pretty clear. 如果是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
In Xcode Playground their are both in one file so the method is visible to class B. 在Xcode Playground中,它们都在一个文件中,因此该方法对B类可见。
The myUnavailableMethod
of class A is private, therefore it can't be overridden. 类A的
myUnavailableMethod
是私有的,因此不能被覆盖。 Change the method declaration to be internal
by removing the private
keyword. 通过删除
private
关键字将方法声明更改为internal
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.