How to avoid accidental overriding method or property in Objective-C

前端 未结 4 1454
轮回少年
轮回少年 2021-01-04 05:47

For instance, I subclassed UILabel and added a method or property called -verticalTextAlignment to align text vertically.
And in the future, if

4条回答
  •  遥遥无期
    2021-01-04 06:27

    Question 1a Category

    Use a prefix, or avoid the whole mess and use functions instead.

    Question 1b Subclass Methods

    If a method does something general enough that the superclass may also implement it, I simply choose a method name which is a little more 'wordy' than Apple would conventionally choose -- such as specifying a non standard typename in the method's name. Of course, this only reduces the possibility of collision.

    If you need a higher level of security, you could just test this at execution (so you know when they are introduced) and hope every user stays up to date -- or you could rely on C and/or C++ more heavily (they do not have this problem).

    Question 2

    But is it possible to happen also when updating your iPhone's iOS version?

    Yes. It's not so unusual. When the frameworks are updated (e.g. via software update), they may contain updated frameworks. This code is loaded into the objc runtime, and you always get the version of the installed frameworks' objc implementations.

    It's also a much broader problem on OS X, where you may load code and/or plugins dynamically quite easily.

提交回复
热议问题