objective-c-runtime

Can't subclass DispatchGroup - “only visible via the Objective-C runtime”?

不打扰是莪最后的温柔 提交于 2019-12-06 15:02:00
It's not possible to subclass DispatchGroup , how to do so? Note: this has finally been fixed in iOS 10+ Example, carry a stateful package with a group, class PushDispatchGroup: DispatchGroup { var sentIds: [(tableName: String, rowId: String)] = [] } thanks to shallowThought for pointing out this is fixed in iOS10. how to inherit from a class which is 'only visible via the Objective-C runtime'? You don't. This is not wrapped around a proper "object" the way you're thinking about it. It's wrapped around a dispatch_group , which is a C struct. This class is not designed to be subclassed and does

Understand Objective-C runtime

邮差的信 提交于 2019-12-06 13:14:09
I have read about how Objective-C runtime works, so please comment if I misunderstood something. Let's say I have class called Person. That class may or not have method getSex. Person *p = [[Person alloc]init]; Here the memory is allocated for Person instance (isa is created also which points to class Person), init is used to initialize Person ivar's [p getSex]; Here objc_msgSend(Person,@selector(getSex) is called on Person class. If Person class not have such method the runtime look for that method in Person root class and so on. When the method is found IMP is called which is a pointer to

Objective C - Get argument types of a method?

寵の児 提交于 2019-12-06 12:04:34
At runtime I need to be able to get the argument types of a method. The following is what gets printed: I have read on other threads that at run-time time objective c treats all objects passed to a method as arguments as id . If this approach doesn't work any other suggestions on a way to read argument types? Log 2014-02-07 15:47:08.962 OCInjection[55727:70b] @ 2014-02-07 15:47:08.964 OCInjection[55727:70b] : Code Class class = NSClassFromString(injectionBinding); unsigned int methodCount; Method *methodList = class_copyMethodList(class, &methodCount); for (int i = 0; i < methodCount; i++) {

In Objective C, how to obtain the metaclass object?

我怕爱的太早我们不能终老 提交于 2019-12-06 11:36:25
In Objective-C , how does one obtain the metaclass object? Both [self class] and [ClassName class] returns the Class object only. objc_getMetaClass Edit: Improved as per Greg's suggestion in the comments. object_getClass([Class class]); object_getClass([self class]) object_getClass([ClassName class]) You'll probably have to use the objective-C runtime function object_getClass to get the class of the class object. A good write up What is a meta-class in Objective-C goes through some detail on meta-classes. However, in some cases the class of the meta-class is the class itself. Do you wish to

Performance of object_setClass() instead of assigning isa pointer

蓝咒 提交于 2019-12-06 10:08:32
I noticed with with the latest update to XCode (4.6), I was given a warning about a couple of lines in JSONKit.m . Specifically, lines that set the class of an object: dictionary->isa = _JKDictionaryClass; These are marked as deprecated with a note that the preferred method was to use object_setClass() : object_setClass(dictionary, _JKDictionaryClass); When I asked why it was preferred to simply silence the warning, the response was: Everything works fine even if new Xcode version complains, I don't want to : 1) test each project where i use JSONKit to check if everything goes fine after

How to dynamically determine Objective-C property type?

此生再无相见时 提交于 2019-12-06 06:05:59
问题 I'm trying to dynamically determine the type of a property in Objective-C. Based on what I have read on this site and elsewhere, I believe I am doing the right thing. However, my code isn't working. The code snippet below demonstrates the problem. Attempting to get the property information for "backgroundColor" and "frame", both of which are valid properties of UIView, fails (class_getProperty() returns NULL): id type = [UIView class]; objc_property_t backgroundColorProperty = class

IMP with unknown number of parameters

偶尔善良 提交于 2019-12-06 04:57:00
问题 Is it possible create an IMP where the number of parameters matches the selector for the instance method being resolved? I could use an 'if' statement and a finite number of parameters (say between 0 and 10), but is it possible to have eg IMP_implementationWithBlock with va_args ? 回答1: You can't create a function at runtime in C; the number of parameters has to be known at compile time. You can use a variadic function to pretend that you have a function with any number of arguments, (I've

Swizzling a method with variable arguments and forward the message - Bad Access

萝らか妹 提交于 2019-12-06 02:26:30
问题 I'm implementing a "Code Injector Class", that through method swizzling can give you the possibility to do something like this: FLCodeInjector *injector = [FLCodeInjector injectorForClass:[self class]]; [injector injectCodeBeforeSelector:@selector(aSelector:) code:^{ NSLog(@"This code should be injected"); }]; aSelector can be a method with variable number of arguments, and variable return type. Arguments / and return type can be objects or primitive type. First, I attach the code of

Call super without “super” keyword

拜拜、爱过 提交于 2019-12-05 21:50:25
I want' to implement "Fix and continue functionality" that was in Xcode 3. CONTEXT : The main idea is: When I need to "Fix something fast", I'm not re-compiling, project. I'm compiling small Attacker class with 'updated' method implementation, loading it into memory and replacing VictimClass's method which have incorrect implementation in runtime. I think that this method will work faster that full project recompilation. When i'm done with fixes i'm just copying source of Attacker class method to Victim class . PROBLEM At the moment, I don't know how correctly call [super ...] in Attacker

Copy a method IMP for multiple method swizzles

最后都变了- 提交于 2019-12-05 17:58:02
I have a class set up that ideally will read the methods of any class passed in and then map all of them to on single selector at runtime before forwarding them off to their original selector. This does work right now, but I can only do it for one method at a time. The issue seems to be that once I swizzle the first method, my IMP to catch and forward the method has now been swapped with that other methods IMP. Any further attempts at this screw up because they use newly swapped IMP to replace the others. 1)So I have MethodA, MethodB, and CustomCatchAllMethod. 2)I swap MethodA with