简体   繁体   English

何时在Swift中继承NSObject

[英]When to subclass NSObject in Swift

I just started reading about Swift. 我刚开始阅读有关Swift的内容。 Used to do objective-c here and there a few years ago and I had a dumb question. 曾经几年前在这里和那里做过Objective-c我有一个愚蠢的问题。 When in Swift should you or shouldn't you subclass NSObject? 在Swift中你应该或不应该将NSObject子类化? Are there any best practices out there about it? 那里有关于它的最佳实践吗?

In a playground, I can do this: 在操场上,我可以这样做:

public class Printer {
    public func SaySomething(s : String) {
        print(s);
    }
}

var pr = Printer()
pr.SaySomething("yo");

That all works as expected. 这一切都按预期工作。 However, I thought I would get practice in swift by creating a Cocoa Touch Framework. 但是,我以为我会通过创建一个Cocoa Touch Framework来快速练习。 So I created Printer.swift, copied and pasted the class you see above. 所以我创建了Printer.swift,复制并粘贴了上面看到的类。 Then in the TestLibraryTests file I tried instantiating a Printer. 然后在TestLibraryTests文件中我尝试实例化一台打印机。 So I did 所以我做了

var p : Printer = Printer()

I got the error, Printer cannot be constructed because it has no accessible initializers. 我收到错误, Printer cannot be constructed because it has no accessible initializers.

So I added init() {} . 所以我添加了init() {}

public class Printer {
    init() {}
    public func SaySomething(s : String) {
        print(s);
    }
}

Still didn't build. 仍然没有建立。 I got the same error. 我得到了同样的错误。 Then once I said Printer was a subclass of NSObject, everything worked. 然后,一旦我说打印机是NSObject的子类,一切都有效。 Why is that? 这是为什么? Is there any time you should not subclass from NSObject at this point? 有没有时间你不应该从NSObject继承子类?

As was stated in the comment from matt, your issue was not one of NSObject. 正如matt的评论中所述,你的问题不是NSObject的问题。 In swift the there are three access levels if you will. 在swift中,如果你愿意,有三个访问级别。 Private, Within Module, and Public. 私有,模块内和公共。 If you do not decorate a property, method, class, etc... the default is Within Module. 如果你没有装饰属性,方法,类等...默认是在模块内。 That means it is accessible to anything in the same target.Since you were running code in the test (which has a different target) you only have access to things which are explicitly Public. 这意味着它可以被同一个目标中的任何内容访问。因为您在测试中运行代码(具有不同的目标),您只能访问明确为Public的内容。

Your class was Public, which is how you were able to import it. 您的课程是公开的,这是您能够导入它的方式。 However your default init, which you get for free is the default. 但是,您免费获得的默认初始化是默认值。 Therefore, outside of your target, the test has no access to the init. 因此,在目标之外,测试无法访问init。 NSObjects init is clearly Public, so when you subclassed from it, you got a Public init. NSObjects init显然是Public,所以当你从它中继承时,你得到了一个Public init。

I'd refer to the NSObject Class Reference to see what it gives you when you inherit from it. 我将参考NSObject类参考 ,看看它从你继承它时给你的东西。 It's my feeling that in a lot of cases, it won't be required, especially if you not interoperating with a lot of Objective C. 我觉得在很多情况下,它不是必需的,特别是如果你没有与很多Objective C进行互操作。

Which then leads to the question, do I need a class, or should I use a struct? 然后导致问题,我需要一个类,或者我应该使用结构?

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

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