How to create a Swift object in Objective-C?

后端 未结 3 2032
时光说笑
时光说笑 2021-01-06 05:32

If you define a swift class like this

@objc class Cat {

}

In swift you can just do

var c = Cat()

But how

3条回答
  •  太阳男子
    2021-01-06 06:20

    You can declare +alloc in a dummy category (don't need to implement it):

    @interface Cat (Alloc)
    + (instancetype)alloc;
    @end
    

    and then you can use regular alloc-init on it:

    Cat *cat = [[Cat alloc] init];
    

    all without needing to change the Swift code.

提交回复
热议问题