Adding the same category to multiple classes

后端 未结 6 608
陌清茗
陌清茗 2021-02-07 03:25

I have an Objective-C category that I\'d like to add to multiple classes without duplicating the code contained in the category. I simply want to add the same methods to multipl

6条回答
  •  太阳男子
    2021-02-07 04:14

    It's a bit of a misnomer to say that providing a category on nsmanagedobject "has the unintended consequence of adding the category's methods to all NSManagedObject subclasses.". The category code is just linked when you include it in a file in which you are using it: you aren't modifying nsmanagedobject.

    That said, if the code needs to be aware of its object, you could create a protocol to which those classes conform, and then use conformsToProtocol in your code to do the testing. That's probably a better generic approach than testing for specific class types.

提交回复
热议问题