objective-c-runtime

How can one obtain the sizeof a type for which one has an encoding?

这一生的挚爱 提交于 2019-12-05 16:41:48
问题 Given an Objective-C type type , one can obtain the encoding encoding and size size of the type easily: const char *encoding = @encode(type); size_t size = sizeof(type); Put a little differently, we have mappings @encode: type_t -> const char * sizeof: type_t -> size_t This raises two questions: (1) Suppose that rather than having a type, we have only an encoding. It would be nice to obtain a mapping sizeofencodedtype: const char * -> size_t such that for every type_t type we have that

Is dispatch_once overkill inside of +[NSObject initialize]?

ぐ巨炮叔叔 提交于 2019-12-05 12:06:11
If I create a singleton inside of +[NSObject initialize] , do I need to put my code inside a dispatch_once block like so? static NSObject * Bar; @implementation Foo + (void)initialize { if (self == [Foo class]) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Bar = [NSObject new]; }); } } @end EDIT I'm concerned about this because I want to make sure that all threads will see that I've set Bar after +[Foo initialize] is called. The documentation says +[NSObject initialize] is thread-safe, but does that imply it is memory-safe? The answer to your direct question is that you don

Determining if an Objective-C method is variadic during runtime

你离开我真会死。 提交于 2019-12-05 10:35:54
Is there a way to find out -- at runtime -- whether a given method is of variadic type? Something like method_getTypeEncoding() ; that won't tell me whether a method accepts variable number of arguments. Or is there maybe a trick to tell so? Robert's comment is correct. Consider: @interface Boogity @end @implementation Boogity - (void)methodWithOneIntArg:(int)a {;} - (void)variadicMethodWithIDSentinel:(id)a, ... {;} @end Running strings on the resulting binary produces (there was also the stock main() ): strings asdfasdfasdf Boogity methodWithOneIntArg: variadicMethodWithIDSentinel: v20@0:8i16

objc_getProtocol() returns NULL for `NSApplicationDelegate\"?

女生的网名这么多〃 提交于 2019-12-05 08:54:24
I'm trying to use the objc_getProtocol() function to get a reference to the struct representing the NSApplicationDelegate protocol: Protocol *protocol = objc_getProtocol("NSApplicationDelegate"); However, for some reason, this always returns NULL . Other protocols such as NSObject , NSCoding , NSTableViewDelegate , and NSTableViewDataSource work fine. Is there something special about NSApplicationDelegate , or am I doing something wrong? Found the answer in the Apple docs: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html#//apple_ref

Objective-C Address of property expression

有些话、适合烂在心里 提交于 2019-12-05 08:17:59
I need access address of property but have problem. example code is @interface Rectangle : NSObject { SDL_Rect wall; SDL_Rect ground; } @property SDL_Rect wall; @property SDL_Rect ground; @end @implementation Rectangle @synthesize x; @synthesize y; @end @interface Graphics : NSObject { int w; int h; } -(void) drawSurface @end @implementation Graphics -(void) drawSurface { Rectangle *rect = [[Rectangle alloc] init]; SDL_BlitSurface(camera, NULL, background, &rect.wall); } @end &rect.x is Address of property expression requested As the comments suggest, you cannot take the address of a property.

Xcode — finding dead methods in a project

北城余情 提交于 2019-12-05 06:51:37
I am curious if there are any tools that provide partial solutions for this. It is a tricky problem because of performSelector . . . but a tool ought to at least be able to come up with candidates, making the human's job easier. Using static analysis, it is impossible to detect functions/methods that are defined but not used due to dynamic nature of Objective-C. The only reasonable solution is to run a coverage using GCov or similar tool. Even then, you will have to make your program do everything possible in order to make sure that you do not strip out some feature that was just not used

How to call an implementation with variable number of arguments?

末鹿安然 提交于 2019-12-05 06:05:55
To simplify, let's say I have a function like that void myFunc(id _self, SEL _cmd, id first, ...) { } In that method I wanna call the implementation(imp) on the superclass of _self. I can reach that IMP with this code: Class class = object_getClass(_self); Class superclass = class_getSuperClass(class); IMP superimp = class_getMethodImplementation(superclass, _cmd); now, how can I do to call that imp ? Just call it using variable arguments: superImp(self, _cmd, argument1, argument2, argument3, etc...) IMP is already typedef 'd as typedef id (*IMP)(id, SEL, ...); So you can call it with variable

Find out if an Objective-C class overrides a method [duplicate]

心不动则不痛 提交于 2019-12-05 04:58:06
This question already has an answer here: Objective-C detect if class overrides inherited method 2 answers How can I find out, at runtime, if a class overrides a method of its superclass? For example, I want to find out if a class has it's own implementation of isEqual: or hash , instead of relying on a super class. You just need to get a list of the methods, and look for the one you want: #import <objc/runtime.h> BOOL hasMethod(Class cls, SEL sel) { unsigned int methodCount; Method *methods = class_copyMethodList(cls, &methodCount); BOOL result = NO; for (unsigned int i = 0; i < methodCount;

Can Foundation tell me whether an Objective-C method requires a special structure return?

五迷三道 提交于 2019-12-05 02:26:57
Background as I understand it: Objective-C method invocations are basically a C function call with two hidden parameters (the receiver and the selector). The Objective-C runtime contains a function named objc_msgSend() that allows to invoke methods that way. Unfortunately, when a function returns a struct some special treatment may be needed. There are arcane (some might say insane) rules that govern whether the structure is returned like other values or whether it's actually returned by reference in a hidden first argument. For Objective-C there's another function called objc_msgSend_stret()

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

我的梦境 提交于 2019-12-04 13:53:46
问题 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