objective-c-runtime

object_setClass to bigger class

倾然丶 夕夏残阳落幕 提交于 2019-12-03 11:50:33
I am changing the class of some objects using object_setClass(id object, Class cls) . I am changing the class to a subclass of the original class. Then I set some properties that are only defined on the subclass, and things seem to work fine. I was a bit surprised that this worked, because object_setClass , as far as I understand, doesn't reallocate the object, it only changes the isa pointer. If the subclass instances are considerably larger (meaning having many more ivars) than the original class instances, I don't see how the object can work as expected. Does this work only because there is

How to decipher “objc_method_description” from protocol method description list?

余生颓废 提交于 2019-12-03 08:48:17
I have some Swift 3 code to decode an iOS Objective-C protocol (which has a Swift counterpart). After concluding Swift 3 reflection was not developed enough to accomplish what I needed, I stumbled on the objc runtime method protocol_copyMethodDescriptionList() , which returns an array of the following C structs: struct objc_method_description SEL name; char *types; }; The code fetches a list of protocol selector names, but not sure what's being returned in the type field. I'm confused about how to properly decode objc_method_description.type values. What I'm getting in the the type fields are

What are the digits in an ObjC method type encoding string?

馋奶兔 提交于 2019-12-03 08:11:10
问题 I'm reading Apple's article about Objective-C runtime type encoding strings and some methods have numbers in their type strings. What do the numbers in v12@0:4@8 mean? 回答1: This looks like an encoding of a setter method like this: - (void) setSomething:(id) anObject To break it down: v means void return type 12 means the size of the argument frame (12 bytes) @0 means that there is an Objective-C object type at byte offset 0 of the argument frame (this is the implicit self object in each

Objective-C runtime: What does declaring a variable of type Class (objc_class) conforming to a protocol mean?

混江龙づ霸主 提交于 2019-12-03 06:54:22
Class bah = [NSString class]; id object = [bah new]; Compiles with absolutely no issues. Class<NSSecureCoding> bah = [NSString class]; id object = [bah new]; Returns the error "No known class method for selector 'new'". Why does the first instance understand that it can call the +new method defined on NSObject but the second instance does not? According to Apple's documentation: Protocols can’t be used to type class objects. Only instances can be statically typed to a protocol, just as only instances can be statically typed to a class. (However, at runtime, both classes and instances respond

Objective-C: preferred way to retrieve the superclass of a Class instance

丶灬走出姿态 提交于 2019-12-03 05:37:52
I am wondering which of the two following methods is the correct or preferred one to retrieve the superclass of a Class variable: Class getSuperclass(Class cls) { return [cls superclass]; } Class getSuperclass(Class cls) { return class_getSuperclass(cls); } Well, the docs on class_getSuperclass() say this: You should usually use NSObject‘s superclass method instead of this function So, I'd go with door #1. The accepted answer is technically correct (yes, that's what the docs say), and yet it's an incorrect answer. [* superclass] only exists for objects that are subclasses of NSObject. Yes,

Why can't we use C-strings as SELs?

ⅰ亾dé卋堺 提交于 2019-12-03 03:12:51
So, I've been messing around with the objc-runtime again (surprise surprise), and I found an interesting block of code here : const char *sel_getName(SEL sel) { #if SUPPORT_IGNORED_SELECTOR_CONSTANT if ((uintptr_t)sel == kIgnore) return "<ignored selector>"; #endif return sel ? (const char *)sel : "<null selector>"; } So, what this tells me is that a SEL is equivalent to a C-string, in every mannerism. Doing a hex dump of the first 16 bytes of SEL that contains @selector(addObject:) gives the following: 61 64 64 4F 62 6A 65 63 74 3A 00 00 00 00 00 00 Which is equal to the C-string addObject: .

How does the Objective-C runtime retrieve the list of classes and methods?

混江龙づ霸主 提交于 2019-12-03 02:48:28
If I get the following Objective-C source file: // test.m #import <objc/Object.h> @interface MySuperClass: Object { } -(void) myMessage1; @end @implementation MySuperClass -(void) myMessage1 { } @end @interface MyClass: MySuperClass { } -(void) myMessage2; @end @implementation MyClass -(void) myMessage2 { } @end int main() { return 0; } and try to generate an assembly file from it with clang -fobjc-nonfragile-abi -fnext-runtime -S test.m , I get the following assembly code: .file "test.m" .text .align 16, 0x90 .type _2D__5B_MySuperClass_20_myMessage1_5D_,@function _2D__5B_MySuperClass_20

why does this code give EXC_BAD_ACCESS (using IMP)

≡放荡痞女 提交于 2019-12-02 21:15:41
This code gives me EXC_BAD_ACCESS, why? NSMutableDictionary *d = [[NSMutableDictionary alloc] init]; IMP imp= [d methodForSelector:@selector(setObject:forKey:) ]; imp(d, @selector( setObject:forKey:), @"obj", @"key"); I'm just starting using IMP, firs try.. no luck. Not sure why I get the error, also.. in the past, when I got EXC_BAD_ACCESS, the message was printed at the console, this time the error line is highlighted. Some notes: ARC is enabled, XCode 4.3.2, the project uses Objective-C++ as de default language/compiler,this code is at the very beginning of the project thanks guys You need

What does the “unrecognized selector sent to instance” error mean?

旧城冷巷雨未停 提交于 2019-12-02 15:32:48
问题 I am getting a crash in my app due to the following error: -[NSCFString count]: unrecognized selector sent to instance 0x612b060 Can anybody tell me what does it mean and how can i find the line in my code with reference 0x612b060 回答1: You are calling count method on an object (probably a collection e.g array, dictionary, or set) which is released or has not been initialized yet. 回答2: You are sending message "count" on NSCFString, means, calling "count" method on NSString datatype. To find

retainCount in blocks show extrange behavior

不打扰是莪最后的温柔 提交于 2019-12-02 08:14:09
I got the this code in a class: - (void)cancel { if (_cancelBlock) _cancelBlock(); } - (void)overrideCancelWithBlock:(void(^)(void))cancelBlock { [_cancelBlock release]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[cancelBlock retainCount]=%lu (before)", [cancelBlock retainCount]); _cancelBlock = [[cancelBlock copy] retain]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[_cancelBlock retainCount]=%lu (after)", [_cancelBlock retainCount]); } - (void)dealloc { NSLog(@"AsyncOperation-dealloc-[_cancelBlock retainCount]=%lu (before)", [_cancelBlock retainCount]); [_cancelBlock release]; NSLog(@