objective-c-runtime

Change enum values at runtime?

大憨熊 提交于 2021-01-21 04:46:10
问题 Is there a way to assign values to enums during runtime in objective c? I have several enums and want each of the enum to have certain value. The values could be read from a xml file. Is there a way to do this? 回答1: Unfortunatley, @Binyamin is correct, you cannot do this with an enum. For this reason, I usually do the following in my projects: // in .h typedef int MyEnum; struct { MyEnum value1; MyEnum value2; MyEnum value3; } MyEnumValues; // in .m __attribute__((constructor)) static void

swift method_exchangeImplementations not work

▼魔方 西西 提交于 2020-02-28 05:01:05
问题 swift code is below: func swizzleMethod() { let method:Method = class_getInstanceMethod(object_getClass(self), Selector("function1")) self.function1() let swizzledMethod:Method = class_getInstanceMethod(object_getClass(self), Selector("function2")) method_exchangeImplementations(method, swizzledMethod) self.function1() } func function1() { print("function1 log") } func function2() { print("function2 log") } it logs: function1 log function1 log ///// my environment is swift based project,

swift method_exchangeImplementations not work

半城伤御伤魂 提交于 2020-02-28 04:58:26
问题 swift code is below: func swizzleMethod() { let method:Method = class_getInstanceMethod(object_getClass(self), Selector("function1")) self.function1() let swizzledMethod:Method = class_getInstanceMethod(object_getClass(self), Selector("function2")) method_exchangeImplementations(method, swizzledMethod) self.function1() } func function1() { print("function1 log") } func function2() { print("function2 log") } it logs: function1 log function1 log ///// my environment is swift based project,

swift method_exchangeImplementations not work

安稳与你 提交于 2020-02-28 04:58:25
问题 swift code is below: func swizzleMethod() { let method:Method = class_getInstanceMethod(object_getClass(self), Selector("function1")) self.function1() let swizzledMethod:Method = class_getInstanceMethod(object_getClass(self), Selector("function2")) method_exchangeImplementations(method, swizzledMethod) self.function1() } func function1() { print("function1 log") } func function2() { print("function2 log") } it logs: function1 log function1 log ///// my environment is swift based project,

Transform (or copy) an object to a subclass instance in Objective-C

会有一股神秘感。 提交于 2020-01-24 10:11:07
问题 I want to transform an instance of an object into an instance of a subclass of that object class, so that I can use the additional methods and properties of that subclass, in Objective-C. How can I do this in a way that does not require me to hardcode the properties of that object class in a copy method? 回答1: It is not possible to transform an object into an instance of a subclass in Objective-C. However, with the class below you can supply an instance of both the object and the subclass and

Why does the Objective-C compiler need to know method signatures?

拈花ヽ惹草 提交于 2020-01-14 13:12:42
问题 Why does the Objective-C compiler need to know at compile-time the signature of the methods that will be invoked on objects when it could defer that to runtime (i.e., dynamic binding)? For example, if I write [foo someMethod] , why it is necessary for the compiler to know the signature of someMethod ? 回答1: Because of calling conventions at a minimum (with ARC, there are more reasons, but calling conventions have always been a problem). You may have been told that [foo someMethod] is converted

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

Deadly 提交于 2020-01-09 13:03:26
问题 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) {

Objective C: Request for member XXX in something not a structure or union

偶尔善良 提交于 2020-01-05 09:36:39
问题 I hit the error (stated in the subject) when trying to run the following code (snippet). The error is pointing to my 3rd and 4th lines of the code below. id shape[3]; shape[0] = [[Circle alloc]init]; shape[0].fillColor = kRed; shape[0].shapeBounds = bound0; Prior to this set of code I had defined the enum and struct for ShapeColor and ShapeBoundary as below typedef enum { kRed, kBlue, kGreen, kPurple }ShapeColor; typedef struct { int x; int y; int width; int height; }ShapeBoundary; Also, I

AnyClass is NSObjectProtocol… sometimes?

試著忘記壹切 提交于 2020-01-05 04:58:28
问题 Following on this question I got very curious about the described behavior and I did some investigation that left me quite puzzled. The problem Checking the is NSObjectProtocol for the return of NSClassFromString returns true in any case except for the return of NSClassFromString("WKNSURLRequest") . The fact that all the results are true is a bit surprising for me also for PureClass and SwiftObject . import UIKit import WebKit import ObjectiveC class Sigh: NSObject { } class PureClass { } let

How can the Objective-C runtime know whether a weakly referenced object is still alive?

老子叫甜甜 提交于 2020-01-04 06:50:14
问题 With the advent of ARC, some new functions were made available to allow developers to play around with weakly referenced objects. id objc_loadWeak(id *location) is one of them. This function receives one parameter correspoding to a location in memory where a weak object was stored and returns this object if it is still alive or nil if it were deallocated. It seems that when an object obj is stored as weak in a location location with id objc_storeWeak(id *location, id obj) , obj is put in a