简体   繁体   English

迅捷2.1,xcode 7.2。 关于为何在Playground中起作用但在应用程序中不起作用的简单NEWBIE

[英]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.

相关问题 我正在使用XCode 7.2。 我的IOS应用程序仅通过https协议连接到Web服务。为什么? - I am using XCode 7.2. My IOS application only connects to webservices with https Protocol..Why is that? 在Swift 2.1 / Xcode 7.2中将PFFile加载到UIImageView中 - Load PFFile Into UIImageView in Swift 2.1/Xcode 7.2 为什么 += 在 swift 操场上有效,但在 swift 应用程序中无效? - Why += works in a swift playground but not in the swift app? Xcode Project和Playground关于Swift错误处理的区别 - Difference between Xcode Project and Playground about Swift error handling Swift 2,Xcode 7.2&#39;NSInternalInconsistencyException - Swift 2, Xcode 7.2 'NSInternalInconsistencyException 为什么我的Swift代码在操场上可以工作,而在Xcode项目中却不能工作? - Why does my Swift code work in the playground but not in the Xcode project? 为什么在Swift操场上进行简单的加法操作时会出错? - Why I am getting error for simple addition operation in Swift playground? Xcode Swift Playground中的多个视图 - Multiple Views In Xcode Swift Playground 我们可以将xcode项目(xcodeproj)转换为Swift Playground(.playground)格式吗? - Can we convert xcode project (xcodeproj) to swift playground (.playground) format? Xcode Playground使用简单的UITableViewController代码崩溃 - Xcode Playground crashes with a simple UITableViewController code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM