Dynamic Getters and Setters with Objective C

前端 未结 2 1239
天命终不由人
天命终不由人 2021-01-01 06:43

I am in a situation where I want to dynamically generate getters and setters for a class at runtime (in a similar manner to what NSManagedObject does behind the scenes). Fr

2条回答
  •  不思量自难忘°
    2021-01-01 07:36

    Another example is one I wrote, called DynamicStorage, available here:

    https://github.com/davedelong/Demos

    The primary impetus behind it was this question, which was asking how to use an NSMutableDictionary as the backing store for any object ivar. I wrote a class that will generate getters and setters for any @property, respecting things like a custom getter/setter name, the object memory management policy, etc. The neat thing about it is that it's using imp_implementationWithBlock() so that it only has to calculate the appropriate property name once (and then captures and saves it as part of the block).

提交回复
热议问题