zhsoft88
V2EX  ›  iOS

swift tips - objc 带有 instancetype 的静态方法在 swift 中该如何写

  •  
  •   zhsoft88 · Jul 20, 2015 · 5063 views
    This topic created in 3986 days ago, the information mentioned may be changed or developed.

    objc的类:

    @interface A : NSObject
    + (instancetype)test;
    @end

    @implementation A
    + (instancetype)test {
    return [[self alloc] init];
    }
    @end

    在swift中这样写:

    class A : NSObject {

    class func test() -> Self {
    return self.init()
    }

    required init() {
    print("init")
    }
    }

    用Self替代instancetype,并且在静态方法中要引用required的init构造方法。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3957 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 05:21 · PVG 13:21 · LAX 22:21 · JFK 01:21
    ♥ Do have faith in what you're doing.