Objective-C Runtime provides the class_addIvar C function :
BOOL class_addIvar(Class cls, const char *name, size_t size,
uint8_t alignment, const char *types)
size
和 alignment
应该怎么设置?
我正在添加类型为 UITextPosition *
的实例变量,但范围内没有 UITextPosition
对象。对于 size
,我可以只做 sizeof(self)
,其中 self
是 UITextField
的子类吗?即,我可以假设 UITextPosition
对象与 UITextField
对象的大小相同吗?
如何获得对齐
?
最佳答案
关于这个东西的文档不是很有用,这确实反射(reflect)出通常你不应该使用它。但是,您可以要求对齐:
char *texPosEncoding = @encode(UITextPosition);
NSUInteger textPosSize, textPosAlign;
NSGetSizeAndAlignment(textPosEncoding, &textPosSize, &textPosAlign);
class_addIvar(yourClass, "yourIvarName", textPosSize, textPosAlign, textPosEncoding);
关于 Objective-C 运行时 : What to put for size & alignment for class_addIvar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942345/