objective-c-runtime

object_getIvar fails to read the value of BOOL iVar

ぃ、小莉子 提交于 2019-11-27 22:52:52
问题 object_getIvar(id object, Ivar ivar) reads the values of iVArs properly but fails on a BOOL type iVar and crashes. I need the values of all iVars of a class.Is there any way to resolve it. 回答1: object_getIvar seems to return the actual value (not an id) which is in opposition to what the manual says. If you are using ARC, this will cause an immediate processor fault during attempt to retain the returned value which is not an object. object_getInstanceVariable() returns a pointer to the

My isa-swizzling breaks KVO

坚强是说给别人听的谎言 提交于 2019-11-27 18:56:08
问题 I'm trying to implement isa swizzling because I need some actions to happen in dealloc method of certain object. I'm overriding - (Class)class; method to return original class (as KVO does). Everything works fine, until I try to add observer to swizzled object. It just crashes. 0x00000000 in 0x00000000 () 0x0091d22a in _NSKeyValueRetainedObservationInfoForObject () 0x0092ec88 in -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] () 0x0092d6fd in -[NSObject

How do I return a struct value from a runtime-defined class method under ARC?

雨燕双飞 提交于 2019-11-27 18:25:04
问题 I have a class method returning a CGSize and I'd like to call it via the Objective-C runtime functions because I'm given the class and method names as string values. I'm compiling with ARC flags in XCode 4.2. Method signature: +(CGSize)contentSize:(NSString *)text; The first thing I tried was to invoke it with objc_msgSend like this: Class clazz = NSClassFromString(@"someClassName); SEL method = NSSelectorFromString(@"contentSize:"); id result = objc_msgSend(clazz, method, text); This crashed

Error compiling with ARC when runtime programming dynamic method

大憨熊 提交于 2019-11-27 16:59:14
问题 I am trying to do some runtime programmation on Objective-C. In order to do this I override the resolveClassMethod method. Unfortunately I come up with some compilation error with clang when ARC is active : error: no known class method for selector 'dynamic' Everything works fine if I use gcc or clang without ARC ( -fno-objc-arc option passed), except a warning instead of the error. I am aware that ARC need to know the name of the method called to figure out how to manage the memory with

Asterisk usage in Objective-C

眉间皱痕 提交于 2019-11-27 16:58:30
问题 I had a question regarding the use of asterisks in Objective-C. Just to be clear: I understand what pointers are and everything in procedural C. I was wondering two things though: 1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];) 2) Why do you omit the asterisk when calling method? Thanks! 回答1: 1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray

How do I list all fields of an object in Objective-C?

喜欢而已 提交于 2019-11-27 13:33:33
If I have a class, how can I list all its instance variable names? eg: @interface MyClass : NSObject { int myInt; NSString* myString; NSMutableArray* myArray; } I would like to get "myInt", "myString", and "myArray". Is there some way to perhaps get an array of names that I can iterate over? I've tried searching the Objective-C documentation but couldn't find anything (and I'm not sure what this is called either). As mentioned, you can use the Objective-C runtime API to retrieve the instance variable names: unsigned int varCount; Ivar *vars = class_copyIvarList([MyClass class], &varCount); for

Avoid extra static variables for associated objects keys

白昼怎懂夜的黑 提交于 2019-11-27 10:49:45
问题 When using associated objects, an Objective-C runtime feature available starting from iOS 4 and OSX 10.6, it's necessary to define a key for storing and retrieving the object at runtime. The typical usage is defining the key like follows static char const * const ObjectTagKey = "ObjectTag"; and then use is to store the object objc_setAssociatedObject(self, ObjectTagKey, newObjectTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC); and retrieve it objc_getAssociatedObject(self, ObjectTagKey); (example

Changed +load method order in Xcode 7

天涯浪子 提交于 2019-11-27 08:38:50
I found out that Xcode 7 (Version 7.0 (7A220)) changed the order in which +load methods for classes and categories are called during unit tests. If a category belonging to the test target implements a +load method, it is now called at the end, when instances of the class might've already been created and used. I have an AppDelegate , which implements +load method. The AppDelegate.m file also contains AppDelegate (MainModule) category. Additionally, there is a unit test file LoadMethodTestTests.m , which contains another category – AppDelegate (UnitTest) . Both categories also implement +load

How do I lookup a string constant at runtime in Objective-C?

丶灬走出姿态 提交于 2019-11-27 02:16:08
My company develops an advertising SDK that mediates other ad networks. At runtime, it checks if the other ad networks are present by using NSClassFromString , and sends those classes messages if they're present. This works fine for Objective-C objects, but how can I load a string constant at runtime? In this case, I want to check the version of an SDK that is only available through a string constant ( extern NSString* VungleSDKVersion; ) You can use CFBundleGetDataPointerForName to lookup a constant's value at runtime NSString *lookupStringConstant(NSString *constantName) { void ** dataPtr =

Why shouldn't you use objc_msgSend() in Objective C?

懵懂的女人 提交于 2019-11-27 01:49:11
问题 The Objective C Runtime Guide from Apple, states that you should never use objc_msgSend() in your own code, and recommends using methodForSelector: instead. However, it doesn't provide any reason for this. What are the dangers of calling objc_msgSend() in your code? 回答1: Reason #1: Bad style - it's redundant and unreadable. The compiler automatically generates calls to objc_msgSend() (or some variant thereof) when it encounters Objective-C messaging expressions. If you know the class and the