objective-c-runtime

Creating an IMP from an Objective-C block

旧街凉风 提交于 2019-11-28 19:09:07
问题 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. 回答1: 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

What is the underlying mechanism for ivar synthesis in the modern Objective C runtime

孤者浪人 提交于 2019-11-28 18:20:16
One of the features of the modern (64 bit OS X and iPhone OS) Objective C runtime is the ability for properties to dynamically synthesize ivars without explicitly declaring them in the class: @interface MyClass : NSObject { // NSString *name; unnecessary on modern runtimes } @property (retain) NSStrng *name; @end @implementation MyClass @synthesize name; @end In quite a bit of my code I use custom getter implementations in order to initialize the properties: - (NSString *) name { if (!name) { name = @"Louis"; } return name; } The above is incompatible with synthesized ivars since it needs to

Avoid extra static variables for associated objects keys

别来无恙 提交于 2019-11-28 17:34:29
When using associated objects, an Objective-C runtime feature available starting from iOS 4 and OSX 10.6, it's necessary to define a key for storing and retrieving the object at runtime. The typical usage is defining the key like follows static char const * const ObjectTagKey = "ObjectTag"; and then use is to store the object objc_setAssociatedObject(self, ObjectTagKey, newObjectTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC); and retrieve it objc_getAssociatedObject(self, ObjectTagKey); (example taken by http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/ ) Is

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

左心房为你撑大大i 提交于 2019-11-28 09:20:44
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, the compiler only creates any absent accessor methods. There is no direct interaction with the dealloc

How do I get the Objective-C class of an ivar?

对着背影说爱祢 提交于 2019-11-28 08:24:52
问题 I have a bunch of simple NSManagedObject s I create in a unit test. They just have a single name attribute of type NSString * . I always give my NSManagedObject the same entityName and Class name. I want to avoid having to write the following code 30 times to set up a unit test: @interface FooTest : GHTestCase { Foo *foo; } @end @implementation FooTest - (void) setUp { [super setUp]; foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:managedObjectContext]

Why shouldn't you use objc_msgSend() in Objective C?

谁说胖子不能爱 提交于 2019-11-28 07:27:52
The Objective C Runtime Guide from Apple, states that you should never use objc_msgSend() in your own code, and recommends using methodForSelector: instead. However, it doesn't provide any reason for this. What are the dangers of calling objc_msgSend() in your code? Reason #1: Bad style - it's redundant and unreadable. The compiler automatically generates calls to objc_msgSend() (or some variant thereof) when it encounters Objective-C messaging expressions. If you know the class and the selector to be sent at compile-time, there's no reason to write id obj = objc_msgSend(objc_msgSend([NSObject

swift: Equivalent objective-c runtime class

吃可爱长大的小学妹 提交于 2019-11-28 05:46:37
What is equivalent swift code for below Objective-C code. I couldn't find swift topic with runtime concept. #import <objc/runtime.h> Class class = [self class]; Trying to get class object of self ? Update: Tried with below code, got error as 'UIViewController.type' doesn't conform to protocol 'AnyObject' var klass: AnyClass = object_getClass(self) Note: Found this post, but wouldn't helped. newacct First, it's hard to translate that code to Swift without knowing what you used that class object for in Objective-C. In Objective-C, class objects are objects, and the type Class can hold a pointer

Get all methods of an Objective-C class or instance

大兔子大兔子 提交于 2019-11-28 03:34:33
In Objective-C I can test whether a given class or instance responds to certain selectors. But how can query a class or instance for all its methods or properties of a class (e.g. a list of all methods or properties)? Ben Gottlieb You'll want to use the Objective C runtime methods, see here: https://developer.apple.com/reference/objectivec/objective_c_runtime You can do this and it is extremely well documented at https://developer.apple.com/library/mac/documentation/cocoa/Reference/ObjCRuntimeRef/index.html To fetch all the instance or class methods of a class, you may use class_copyMethodList

Intercept Objective-C delegate messages within a subclass

人走茶凉 提交于 2019-11-28 03:04:41
I have a subclass of UIScrollView in which I need to internally respond to scrolling behaviour. However, the viewcontroller will still need to listen to scrolling delegate callbacks, so I can't outright steal the delegate within my component. Is there a way to keep the property named "delegate" and just listen to messages sent along it, or else somehow internally hijack the delegate property and forward messages outward after running some code? Matt Williamson Yes, but you'll have to override every delegate method in the docs . Basically, make a second delegate property and implement the

Objective-C class -> string like: [NSArray className] -> @“NSArray”

青春壹個敷衍的年華 提交于 2019-11-28 02:46:44
I am trying to get a string name of a class from the class object itself. // For instance [NSArray className]; // @"NSArray" I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless work. So how can I get a string from a class object, and not an instance? NSString *name = NSStringFromClass ([NSArray class]); You can even go back the other way: Class arrayClass = NSClassFromString (name); id anInstance = [[arrayClass alloc] init]; Here's a different way to do it with slightly less typing: NSString *name = [NSArray description];