objective-c-runtime

How do I lookup a string constant at runtime in Objective-C?

不羁岁月 提交于 2019-11-26 12:32:09
问题 My company develops an advertising SDK that mediates other ad networks. At runtime, it checks if the other ad networks are present by using NSClassFromString , and sends those classes messages if they\'re present. This works fine for Objective-C objects, but how can I load a string constant at runtime? In this case, I want to check the version of an SDK that is only available through a string constant ( extern NSString* VungleSDKVersion; ) 回答1: You can use CFBundleGetDataPointerForName to

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

寵の児 提交于 2019-11-26 09:16:18
问题 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

Get property name as a string

随声附和 提交于 2019-11-26 08:10:53
问题 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\" 回答1: 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

What exactly is super in Objective-C?

橙三吉。 提交于 2019-11-26 05:27:03
问题 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? 回答1: 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,

Swift native base class or NSObject

我们两清 提交于 2019-11-26 04:32:22
问题 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

How can I add properties to an object at runtime?

巧了我就是萌 提交于 2019-11-26 04:07:46
问题 Is it possible to add properties to an Objective C object at runtime? 回答1: 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.

How to write iOS app purely in C

最后都变了- 提交于 2019-11-25 20:03:11
I read here Learn C Before Objective-C? Usually I then replace some Obj-C code with pure C code (after all you can mix them as much as you like, the content of an Obj-C method can be entirely, pure C code) Is this true? Is it possible to build an iPhone app purely in the C programming language? Richard J. Ross III Damn, it took me a while but I got it: main.c: #include <CoreFoundation/CoreFoundation.h> #include <objc/runtime.h> #include <objc/message.h> // This is a hack. Because we are writing in C, we cannot out and include // <UIKit/UIKit.h>, as that uses Objective-C constructs. // however,