zhsoft88
V2EX  ›  iOS

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

  •  
  •   zhsoft88 · Jul 20, 2015 · 5040 views
    This topic created in 3967 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   ·   3099 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 03:41 · PVG 11:41 · LAX 20:41 · JFK 23:41
    ♥ Do have faith in what you're doing.