objective-c-runtime

How are the digits in ObjC method type encoding calculated?

假装没事ソ 提交于 2019-11-30 08:47:06
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? The numbers were used in the m68K days to denote stack layout. That is, you could literally decode the the

Using objc_getClassList under arc

随声附和 提交于 2019-11-30 04:51:50
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. 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_getClassList(classes, numClasses); for (int i = 0; i < numClasses; i++) { NSLog(@"Class name: %s", class_getName

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

心已入冬 提交于 2019-11-30 04:03:12
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. Getting : myFloat = 2.34f; float myFloatValue; object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue); NSLog(@"%f", myFloatValue); Outputs: 2.340000 Setting : float newValue = 2.34f; unsigned int addr = (unsigned int)&newValue; object_setInstanceVariable(self, "myFloat", *(float**)addr); NSLog(@"

Why can't gcc or clang properly @encode SIMD vector types?

陌路散爱 提交于 2019-11-30 03:52:25
问题 While doing some messing around with vector types and the ObjC runtime, I came across a very perplexing problem. Neither clang or GCC will give the 'proper' type-encoding for any SIMD vector type, as far as I can tell. #import <Foundation/Foundation.h> int main() { typedef int int4 __attribute__((vector_size(16))); typedef float float4 __attribute__((vector_size(16))); NSLog(@"Int4: %s", @encode(int4)); NSLog(@"Float4: %s", @encode(float4)); } When compiled & run with either GCC or clang, I

object_getInstanceVariable works for float, int, bool, but not for double?

瘦欲@ 提交于 2019-11-30 03:51:08
I've got object_getInstanceVariable to work as here however it seems to only work for floats, bools and ints not doubles. I do suspect I'm doing something wrong but I've been going in circles with this. float myFloatValue; float someFloat = 2.123f; object_getInstanceVariable(self, "someFloat", (void*)&myFloatValue); works, and myFloatValue = 2.123 but when I try double myDoubleValue; double someDouble = 2.123f; object_getInstanceVariable(self, "someDouble", (void*)&myDoubleValue); I get myDoubleValue = 0 . If I try to set myDoubleValue before the function eg. double myDoubleValue = 1.2f , the

Using objc_setAssociatedObject with weak references

淺唱寂寞╮ 提交于 2019-11-30 00:13:39
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? 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 : NSObject @property (nonatomic, readonly, weak) id object; @end @implementation WeakObjectContainer -

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

柔情痞子 提交于 2019-11-29 23:56:57
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 the right way to do this? Here's what you'd want: Class theClass = NSClassFromString(classNameStr); id

How to implement an NSRunLoop inside an NSOperation

隐身守侯 提交于 2019-11-29 22:25:54
Im posting this question because I have seen a lot of confusion over this topic and I spent several hours debugging NSOperation subclasses as a result. The problem is that NSOperation doesnt do you much good when you execute Asynchronous methods which are not actually complete until the asynchronous callback completes. If the NSOperation itself is the callback delegate it may not even be sufficient to properly complete the operation due to the callback occurring on a different thread. Lets say you are in the main thread and you create an NSOperation and add it to an NSOperationQueue the code

Creating an IMP from an Objective-C block

淺唱寂寞╮ 提交于 2019-11-29 21:47:38
The IMP type in Objective-C represents a function pointer, as far I as understand. Is there any way to make an IMP from a block pointer? Thanks for your ideas. Since this was written there is now API in iOS and Mac OS X that allows Blocks to be turned into IMPs directly. I wrote up a weblog post describing the API (imp_implementationWithBlock()). A block is effectively a structure that contains a bit of metadata, a reference to the code contained within the block and a copy of the const-copied data captured within the block. Thus, no, there is no way to directly map between an IMP and a Block

-rewrite-objc and Objective-C in clang

感情迁移 提交于 2019-11-29 18:03:20
Recently, I have one problem. The clang can translate Objective-C to c++ use -rewrite-objc. So I think, the first step. clang compile Objective-C to C++. And then compile only can use c++ compiler. Is it do like this? clang first translate Objective-C to C++ with runTime, and then compile to the machine code? -rewrite-objc exists to translate ObjC to C++ so it can be compiled in the Visual Studio. It is still Objective-C semantics and you still need the objective-c runtime. It is not magically converting Objective-C to the C++ OO architecture. This is much more like when Objective-C was