objective-c-runtime

description of message that triggered a method invocation

一个人想着一个人 提交于 2019-12-13 03:11:53
问题 Does the objective-c runtime allow getting a description of the message that triggered a method invocation - from within that method itself? For example, if I have a method called: -(Foobar)doMyStuff; . . is it possible from within that method to get something like: //This would return @"doMyStuff" NSString* selectorName = [foobar nameOfMessageThatTriggeredMyInvocation]; 回答1: Yes. Every method has its selector passed in as a hidden argument. It's the second argument, after self , and it is

Get the sender of the message for an NSInvocation

旧巷老猫 提交于 2019-12-13 00:36:17
问题 How do I get the sender of the message wrapped in an `NSInvocation? I'm in an object, and I've just received an invocation in my forwardInvocation: . Is there any way to know who sent me the message in the first place? 回答1: No. Information about the sender is not available (unless you explicitly add an sender:self argument). See also this thread http://www.cocoabuilder.com/archive/cocoa/194746-is-the-sender-of-an-obj-method-implied-in-the-destination-method.html: It isn't explicitly available

Create object of Objective-C class at runtime in Swift, which conforms to Objective-C protocol

跟風遠走 提交于 2019-12-13 00:04:23
问题 I have Objective-C Protocol and Interface implementation as below: @protocol Animal <NSObject> -(void)walk; @end @interface Cat : NSObject<Animal> @end @implementation Cat -(void)walk{} @end @interface Dog : NSObject<Animal> @end @implementation Dog -(void)walk{} @end I am trying to use instance of classes at runtime which implement protocol 'Animal'. This code in in swift : var classesCount = objc_getClassList(nil, 0) let allClasses = UnsafeMutablePointer<AnyClass?>.allocate(capacity: Int

Intercept/Programmatically set IBOutlet properties

六眼飞鱼酱① 提交于 2019-12-12 20:07:49
问题 Question: Is there any way that I could set IBOutlet properties programmatically and in an automated way (i.e. without hard-coding the properties to be set)? Maybe there is some "IBOutlet setting" routine that I could intercept with my own specialized code? Background: The problem leading to the question above originates from the fact that "IBOutleted" size constraints (width and height) are not being set when running the following method (it's a method for replacing a "placeholder" view from

Decode Class from @encoded type string

我怕爱的太早我们不能终老 提交于 2019-12-12 10:47:21
问题 Objective-C's @encode produces C strings to represent any type, including primitives, and classes, like so: NSLog(@"%s", @encode(int)); // i NSLog(@"%s", @encode(float)); // f NSLog(@"%s", @encode(CGRect)); // {CGRect={CGPoint=ff}{CGSize=ff}} NSLog(@"%s", @encode(NSString)); // {NSString=#} NSLog(@"%s", @encode(UIView)); // {UIView=#@@@@fi@@I{?=b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b6b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b3b1b1b1b2b2b1}} So, I can get a

In Objective C, how to obtain the metaclass object?

。_饼干妹妹 提交于 2019-12-12 10:12:38
问题 In Objective-C , how does one obtain the metaclass object? Both [self class] and [ClassName class] returns the Class object only. 回答1: objc_getMetaClass Edit: Improved as per Greg's suggestion in the comments. object_getClass([Class class]); 回答2: object_getClass([self class]) object_getClass([ClassName class]) 回答3: You'll probably have to use the objective-C runtime function object_getClass to get the class of the class object. A good write up What is a meta-class in Objective-C goes through

Convert/compile a string into executable code in Objective-C

你。 提交于 2019-12-10 17:12:29
问题 I want to take a string in ObjC and evaluate it as if it were code. For example (these are made-up functions): NSString *Cmd=@" if (10>5) NSLog(@"Test"); "; MyClass.Run(Cmd); I expect that "Test" appears in the output log. I have searched and tested a lot of code samples and library but still no good results. I Ended Up with these 2 libraries for compilation at runtime: 1- libClang framework and clang_Cursor_Evaluate function . https://www.mikeash.com/pyblog/friday-qa-2014-01-24-introduction

Understand Objective-C runtime

最后都变了- 提交于 2019-12-10 11:22:51
问题 I have read about how Objective-C runtime works, so please comment if I misunderstood something. Let's say I have class called Person. That class may or not have method getSex. Person *p = [[Person alloc]init]; Here the memory is allocated for Person instance (isa is created also which points to class Person), init is used to initialize Person ivar's [p getSex]; Here objc_msgSend(Person,@selector(getSex) is called on Person class. If Person class not have such method the runtime look for that

Performance of object_setClass() instead of assigning isa pointer

只愿长相守 提交于 2019-12-10 11:18:28
问题 I noticed with with the latest update to XCode (4.6), I was given a warning about a couple of lines in JSONKit.m . Specifically, lines that set the class of an object: dictionary->isa = _JKDictionaryClass; These are marked as deprecated with a note that the preferred method was to use object_setClass() : object_setClass(dictionary, _JKDictionaryClass); When I asked why it was preferred to simply silence the warning, the response was: Everything works fine even if new Xcode version complains,

Creat a memory leak with Objective-C and Swift

孤街醉人 提交于 2019-12-10 00:10:38
问题 I had an interview, and I was asked to create a memory leak with Objective-C and Swift.So how to create a memory leak with Objective-C and Swift? 回答1: You just need to create a reference cycle. Obj-C: @interface MyClass : NSObject @property (strong) MyClass *otherObject; @end @implementation MyClass @end //... MyClass *a = [MyClass new]; MyClass *b = [MyClass new]; a.otherObject = b; b.otherObject = a; The same applies to Swift: class MyClass { var otherObject: MyClass? } let a = MyClass()