objective-c-runtime

What's wrong with using a category on NSObject to provide a default protocol implementation?

人盡茶涼 提交于 2019-12-19 17:42:13
问题 I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling performSelector: is out (and NSInvocation has problems cooperating with ARC) 3: This solution, IMO, pretends to be universal but has all the drawbacks of 1 I eventually came up with this idea: @protocol MyProtocol <NSObject> @optional -(void

What's wrong with using a category on NSObject to provide a default protocol implementation?

非 Y 不嫁゛ 提交于 2019-12-19 17:42:02
问题 I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling performSelector: is out (and NSInvocation has problems cooperating with ARC) 3: This solution, IMO, pretends to be universal but has all the drawbacks of 1 I eventually came up with this idea: @protocol MyProtocol <NSObject> @optional -(void

Is there any way to list all swizzled methods in an iOS app?

荒凉一梦 提交于 2019-12-18 15:46:24
问题 I'm essentially looking for a way to detect when/what third party libraries swizzle. I recently ran into a situation where an ad library used an oddball fork of AFNetworking. AFNetworking swizzles NSURLSessionTask, and the two swizzles didn't play nicely under certain circumstances. I'd really like to be able to detect and sanity check this kind of thing, and ideally even keep a versioned dump of every swizzled method in the app so we have some visibility into who's monkey patching what and

Swift isa pointer remapping or other supported method swizzling

旧巷老猫 提交于 2019-12-18 12:27:42
问题 Do Swift classes have something like an isa pointer that can be remapped? We've seen that Swift uses a more static method dispatch than objective-C, which (unless a class dervices from Foundation/NSObject) prevents the style of swizzling based on remapping method implementations at runtime. I'm wondering how we'll implement method interception-based dynamic features like the observer pattern, notifications, etc? Currently all this stuff is provided by the Objective-C layer, and can be easily

How are the digits in ObjC method type encoding calculated?

Deadly 提交于 2019-12-18 12:23:44
问题 Is is a follow-up to my previous question: What are the digits in an ObjC method type encoding string? Say there is an encoding: v24@0:4:8@12B16@20 How are those numbers calculated? B is a char so it should occupy just 1 byte (not 4 bytes). Does it have something to do with "alignment"? What is the size of void ? Is it correct to calculate the numbers as follows? Ask sizeof on every item and round up the result to multiple of 4? And the first number becomes the sum of all the other ones? 回答1:

Using objc_getClassList under arc

懵懂的女人 提交于 2019-12-18 11:49:09
问题 Has anybody managed to use objc_getClassList under arc, short of turning arc off for the file in question? The fundamental problem is that one of the parameters is a C array of Class pointers. 回答1: This code should work under ARC: int numClasses; Class *classes = NULL; classes = NULL; numClasses = objc_getClassList(NULL, 0); NSLog(@"Number of classes: %d", numClasses); if (numClasses > 0 ) { classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses); numClasses = objc

How do I get the int value from object_getIvar(self, myIntVar) as it returns a pointer

不问归期 提交于 2019-12-18 11:44:17
问题 if the variable in object_getIvar is a basic data type (eg. float, int, bool) how do I get the value as the function returns a pointer (id) according to the documentation. I've tried casting to an int, int* but when I try to get that to NSLog, I get error about an incompatible pointer type. 回答1: Getting : myFloat = 2.34f; float myFloatValue; object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue); NSLog(@"%f", myFloatValue); Outputs: 2.340000 Setting : float newValue = 2.34f;

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

我的未来我决定 提交于 2019-12-18 04:55:06
问题 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?

Using instance variables with Modern Runtime

為{幸葍}努か 提交于 2019-12-18 00:49:25
问题 I have several years of experience in Obj-c and Cocoa, but am just now getting back into it and the advances of Obj-C 2.0 etc. I'm trying to get my head around the modern runtime and declaring properties, etc. One thing that confuses me a bit is the ability in the modern runtime to have the iVars created implicitly. And of course this implies that in your code you should always be using self.property to access the value. However, in init* and dealloc(assuming you're not using GC) methods we

Is there any problem using self.property = nil in dealloc?

ε祈祈猫儿з 提交于 2019-12-17 18:56:21
问题 I know declared property generates accessor method which is someway just syntax sugar. I found quite a lot people use self.property = nil in their dealloc method. 1) In Apple's Memory Management document, p23 It says: The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc. why shouldn't? 2) In apple's Objective-C 2.0 , p74 Declared properties fundamentally take the place of accessor method declarations; when you synthesize a property,