objective-c-runtime

Get all methods of an Objective-C class or instance

ぐ巨炮叔叔 提交于 2019-11-27 00:02:08
问题 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)? 回答1: You'll want to use the Objective C runtime methods, see here: https://developer.apple.com/reference/objectivec/objective_c_runtime 回答2: You can do this and it is extremely well documented at https://developer.apple.com/library/mac/documentation/cocoa/Reference

Intercept Objective-C delegate messages within a subclass

十年热恋 提交于 2019-11-26 23:56:16
问题 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? 回答1: Yes, but you'll have to override every

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

心已入冬 提交于 2019-11-26 23:48:42
问题 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? 回答1: NSString *name = NSStringFromClass ([NSArray class]); You can even go back the other way: Class arrayClass = NSClassFromString (name); id anInstance = [[arrayClass alloc]

What is objc_setAssociatedObject() and in what cases should it be used?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 23:39:53
In a project I have taken on, the original author has opted to use objc_setAssociatedObject() and I'm not 100% clear what it does or why they decided to use it. I decided to look it up and, unfortunately, the docs aren't very descriptive about its purpose. objc_setAssociatedObject Sets an associated value for a given object using a given key and association policy. void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy) Parameters object The source object for the association. key The key for the association. value The value to associate with the key key for

Get property name as a string

﹥>﹥吖頭↗ 提交于 2019-11-26 22:08:43
I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @"crazyObject" You can try this: unsigned int propertyCount = 0; objc_property_t * properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray * propertyNames = [NSMutableArray array]; for (unsigned int i = 0; i < propertyCount; ++i) { objc_property_t property = properties[i]; const char * name = property_getName(property); [propertyNames addObject:

What exactly is super in Objective-C?

﹥>﹥吖頭↗ 提交于 2019-11-26 17:28:12
As far as I know, it's a pointer to the superclass. It's hard-wired with the superclass, and not dynamically figured out at runtime. Would like to know it more in detail... Anyone? super Essentially, it allows you to use the implementations of the current class' superclass. For the gritty details of the Objective-C runtime: [super message] has the following meaning: When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend, objc_msgSend_stret, objc_msgSendSuper, or objc_msgSendSuper_stret. Messages sent to an object’s superclass (using the super

How do I list all fields of an object in Objective-C?

人盡茶涼 提交于 2019-11-26 16:23:50
问题 If I have a class, how can I list all its instance variable names? eg: @interface MyClass : NSObject { int myInt; NSString* myString; NSMutableArray* myArray; } I would like to get "myInt", "myString", and "myArray". Is there some way to perhaps get an array of names that I can iterate over? I've tried searching the Objective-C documentation but couldn't find anything (and I'm not sure what this is called either). 回答1: As mentioned, you can use the Objective-C runtime API to retrieve the

Why doesn&#39;t Objective-C support private methods?

*爱你&永不变心* 提交于 2019-11-26 15:42:42
I've seen a number of strategies for declaring semi-private methods in Objective-C , but there does not seem to be a way to make a truly private method. I accept that. But, why is this so? Every explanation I've essentially says, "you can't do it, but here's a close approximation." There are a number of keywords applied to ivars (members) that control their scope, e.g. @private , @public , @protected . Why can't this be done for methods as well? It seems like something the runtime should be able to support. Is there an underlying philosophy I'm missing? Is this deliberate? bbum The answer is..

Swift native base class or NSObject

放肆的年华 提交于 2019-11-26 15:41:24
I tested out some isa swizzling with Swift, and found that it only works when NSObject is a super-class (directly or further up), or by using the '@objc' decoration. Otherwise it will follow a static- and vtable-dispatch style, like C++. Is it normal to define a Swift class without a Cocoa/NSObject base class? If it is I'm concerned this means foregoing much of the dynamism of Objective-C, such as method interception and run-time introspection. Dynamic run-time behavior sits at the heart of features like property observers, Core Data, Aspect Oriented Programming , Higher Order Messaging ,

How can I add properties to an object at runtime?

天大地大妈咪最大 提交于 2019-11-26 15:01:55
Is it possible to add properties to an Objective C object at runtime? It’s possible to add formal properties to a class via class_addProperty() : BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount) The first two parameters are self-explanatory. The third parameter is an array of property attributes, and each property attribute is a name-value pair which follow Objective-C type encodings for declared properties . Note that the documentation still mentions the comma-separated string for the encoding of property attributes.