objective-c-runtime

Handling the return value of object_getIvar(id object, Ivar ivar)

孤街浪徒 提交于 2019-12-24 07:16:08
问题 1) object_getIvar(id object, Ivar ivar) returns an 'id' if the Ivar is an object eg. if the variabe is an NSString, presumably the id = NSString which contains the value. Is that correct? Or what do I need to do to access the value of the Ivar. 2) if the Ivar is a float/int etc. what will get returned and how do I convert it into something I can use (a float or int is fine as I can use NSNumber numberWithXXX to convert it to an object). 回答1: 1) Correct. As stated by the docs: Return Value The

Get all existing pointers to an object

荒凉一梦 提交于 2019-12-24 01:56:31
问题 Is it possible to get a list of pointer to a pointers to an objective c object. something like id **pointers(id object, int *out_count) Pretty crazy, huh? =) 回答1: Unfortunately, no. If such a thing were generally possible, then writing a precise garbage collector would be rather simple: int count; pointers(obj, &count); if (count == 0) { free(obj); } Since the objective-c garbage collector has to chase pointers from roots, control the allocator, and scan the stack conservatively to achieve

Calling a block though runtime, anything similar to NSInvocation?

空扰寡人 提交于 2019-12-24 01:45:24
问题 I have block of unknown type (as id ) and array of arguments that need to passed into that block. Arguments may be objects or numbers/structs boxed as NSNumber/NSValue. Block may also return an object, number or struct. This is a library code, and types of arguments are not known beforehand. Assuming that I can can dynamically read signature from the block descriptor, is there a way to construct something like an NSInvocation to call a block? 回答1: Surprisingly this works: CGAffineTransform (

Swift 2.0 replicate OBJC_ASSOCIATION_RETAIN

ぐ巨炮叔叔 提交于 2019-12-23 11:47:08
问题 I'm extending some classes in Swift 2.0 to work with ReactiveCocoa 3.0 (swift-2.0 branch), but have run into some trouble. I've followed Colin Eberhardt's tutorial, and have copy pasted some of his UIKit extension logic over to my OS X app. It all compiles fine, apart from this property: UInt(OBJC_ASSOCIATION_RETAIN) , which gives me the following compiler error. use of unresolved identifier How can I access this property? I've tried to import ObjectiveC and #import <objc/runtime.h> in the

Swift 2.0 replicate OBJC_ASSOCIATION_RETAIN

徘徊边缘 提交于 2019-12-23 11:47:01
问题 I'm extending some classes in Swift 2.0 to work with ReactiveCocoa 3.0 (swift-2.0 branch), but have run into some trouble. I've followed Colin Eberhardt's tutorial, and have copy pasted some of his UIKit extension logic over to my OS X app. It all compiles fine, apart from this property: UInt(OBJC_ASSOCIATION_RETAIN) , which gives me the following compiler error. use of unresolved identifier How can I access this property? I've tried to import ObjectiveC and #import <objc/runtime.h> in the

Call super without “super” keyword

别说谁变了你拦得住时间么 提交于 2019-12-22 09:50:14
问题 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

Accessing static variables that are simulating class variables from unit tests

*爱你&永不变心* 提交于 2019-12-22 08:48:08
问题 Is there an Objective-C runtime library function ( unlikely ) or set of functions capable of inspecting static (quasi-class level) variables in Objective-C? I know I can utilize a class accessor method but I'd like to be able to test without writing my code "for the test framework". Or , is there a obscure plain C technique for external access to static vars? Note this information is for unit testing purposes—it needn't be suitable for production use. I'm conscious that this'd go against the

Determining if an Objective-C method is variadic during runtime

断了今生、忘了曾经 提交于 2019-12-22 07:53:30
问题 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? 回答1: 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

Determining if an Objective-C method is variadic during runtime

Deadly 提交于 2019-12-22 07:52:22
问题 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? 回答1: 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

Is dispatch_once overkill inside of +[NSObject initialize]?

故事扮演 提交于 2019-12-22 07:03:33
问题 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