Objective-C Is it safe to overwrite [NSObject initialize]?

后端 未结 3 753
悲&欢浪女
悲&欢浪女 2020-12-17 02:55

Basically, I have the following code (explained here: Objective-C Constants in Protocol)

// MyProtocol.m
const NSString *MYPROTOCOL_SIZE;
const NSString *MYP         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 03:36

    Why don't you setup those two variables inside the class MyClass instead ? And also, why don't you use accessors ? Instead of having a fake constant variable that comes from nowhere, it's preferable to define an accessor on a class that should actually use it. A simple +(NSString *)myProtocolSize; would do great, even in the protocol.

    Also, overriding methods of a class in a category "works" but isn't reliable and should be avoided at all cost: if the method you're overriding is implemented in a category, the runtime does NOT guarantee the loading order and your implementation might never be added to it.

提交回复
热议问题