gpt4 book ai didi

Swift 2.2 和 NSProxy。我该如何使用它?

转载 作者:行者123 更新时间:2023-11-30 13:31:59 27 4
gpt4 key购买 nike

NSProxy的接口(interface)是

public class NSProxy : NSObjectProtocol {
public class func alloc() -> Self
public class func `class`() -> AnyClass
public func forwardInvocation(invocation: NSInvocation)
public func dealloc()
public func finalize()
public var description: String { get }
public var debugDescription: String { get }
public class func respondsToSelector(aSelector: Selector) -> Bool
}

所以它没有任何初始化器。但是Swift编译器需要从子类的初始化器中调用Super.init

Super.init isn't called on all paths before returning from initializer

class SimpleDecorator: NSProxy {
let phone: Phone
init(phone: Phone) { // compile error with Super.init
self.phone = phone
}
}

我没有把它放在对 super 的调用中,它不会编译,但是当然,由于 NSProxy 没有 init,所以当我调用 super.init() 时它不会编译。

Swift 中是否可以有 NSProxy 的子类?

最佳答案

NSProxy 是一个抽象类。 Apple 关于 NSProxy 的文档说“一个抽象父类(super class),为充当其他对象或尚不存在的对象的替身的对象定义 API”。

维基百科关于抽象类的文档说:

In a language that supports inheritance, an abstract class, or abstract base class (ABC), is a class that cannot be instantiated because it is either labeled as abstract or it simply specifies abstract methods (or virtual methods).

<小时/>

为抽象类调用 super.init() 是错误的。

关于Swift 2.2 和 NSProxy。我该如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com