objective-c-runtime

Any gotchas with objc_setAssociatedObject and objc_getAssociatedObject?

自古美人都是妖i 提交于 2019-11-29 14:43:25
问题 I’m looking into ways to add a property (an integer in this case) to all UIView instances, whether they are subclassed or not. Is using objc_setAssociatedObject() and objc_getAssociatedObject() within a category the appropriate, Apple-endorsed way to do this? I have heard some concerns that this constitutes a “runtime hack,” and can lead to problems that are difficult to track down and debug. Has anyone else seen this type of problem? Is there a better way to add an integer property to all

How do I get the Objective-C class of an ivar?

假装没事ソ 提交于 2019-11-29 14:16:30
I have a bunch of simple NSManagedObject s I create in a unit test. They just have a single name attribute of type NSString * . I always give my NSManagedObject the same entityName and Class name. I want to avoid having to write the following code 30 times to set up a unit test: @interface FooTest : GHTestCase { Foo *foo; } @end @implementation FooTest - (void) setUp { [super setUp]; foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:managedObjectContext]; foo.name = @"foo"; } @end Since foo is an ivar, I would think I should be able to write a macro to

Associated objects in Swift, does global key actually produce specific instances?

眉间皱痕 提交于 2019-11-29 07:35:37
To have an associated object in Swift, you simply use a memory address as a handle, and then use the objc calls. The usual example code you can google everywhere is: var keyA:UInt8 = 0 var keyB:UInt8 = 0 extension UIViewController { var aoAA: String? { get { return objc_getAssociatedObject(self, &keyA) as? String } set { objc_setAssociatedObject(self, &keyA, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } } var aoBB: String? { get { return objc_getAssociatedObject(self, &keyB) as? String } set { objc_setAssociatedObject(self, &keyB, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION

object_getIvar fails to read the value of BOOL iVar

守給你的承諾、 提交于 2019-11-29 05:23:20
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. 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 information, but ARC refuses to accept the function. Accessing ivars directly can be done using ARC. You can

My isa-swizzling breaks KVO

这一生的挚爱 提交于 2019-11-29 04:47:00
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(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] () Here is implementation of

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

本秂侑毒 提交于 2019-11-29 04:23:24
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 with "EXC_BAD_ACCESS" and without a stack trace. I used this first because the documentation for objc

Asterisk usage in Objective-C

你。 提交于 2019-11-29 02:38:46
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) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];) Think of an Objective-C object as a glorified struct for a moment. NSArray array; in a local

Error compiling with ARC when runtime programming dynamic method

自闭症网瘾萝莉.ら 提交于 2019-11-29 02:38:36
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 returned value (following method name convention). But how to this issue without an ugly performSelector

Using objc_setAssociatedObject with weak references

只谈情不闲聊 提交于 2019-11-28 21:57:48
问题 I know that OBJC_ASSOCIATION_ASSIGN exists, but does it zero the reference if the target object is dealloced? Or is it like the old days where that reference needs to get nil-ed or we risk a bad access later on? 回答1: As ultramiraculous demonstrated, OBJC_ASSOCIATION_ASSIGN does not do zeroing weak reference and you risk to access a deallocated object. But it’s quite easy to implement yourself. You just need a simple class to wrap an object with a weak reference: @interface WeakObjectContainer

Convert a string (“MyExampleClass”) into a class name (MyExampleClass)

匆匆过客 提交于 2019-11-28 21:00:59
问题 I want to convert a string to a class name. Imagine that I have a string, which changes, containing a class name, for example, the string "MyExampleClass" . Now, I want to create an object of the class MyExampleClass . I have to get the class name from the string. I want to do something like the following. (Consider the code just as a sketch.) NSString *classNameStr = "MyExampleClass"; id theClass = [UIClass classFromString:classNameStr]; theClass *myObject = [[theClass alloc] init]; What is