objective-c-runtime

Are selectors in Objective-C just another way to send a message to an object?

大兔子大兔子 提交于 2019-12-17 18:11:08
问题 Are selectors in Objective-C just another way to send a message to an object? I really don't understand why or how to use them. 回答1: They aren’t another way to send a message to an object, they’re the only way. For example, in [myView setValue:@"foo"] , setValue: is a selector. (Another, less convenient way of writing the same thing is objc_msgSend(myView, @selector(setValue:), @"foo") .) As Ian Henry says, you can use SEL values to choose a selector at runtime instead of compile time. This

swift: Equivalent objective-c runtime class

China☆狼群 提交于 2019-12-17 17:54:31
问题 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. 回答1: First, it's hard to translate that code to Swift without knowing what you used that class object

Class is implemented in both. One of the two will be used

狂风中的少年 提交于 2019-12-17 16:11:51
问题 I have a project that has a dependency (installed via CocoaPods) using SocketRocket and have imported a static library from HeapAnalytics. Apparently the HeapAnalytics library already uses SocketRocket. I get no errors when compiling, but at runtime receive the following: Class SRWebSocket is implemented in both [path] and [path]. One of the two will be used. Which one is undefined. I'm not sure how to handle it because most solutions I've seen require compiling the static library yourself

Why doesn't Objective-C support private methods?

爱⌒轻易说出口 提交于 2019-12-17 03:22:25
问题 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

How to write iOS app purely in C

倾然丶 夕夏残阳落幕 提交于 2019-12-16 22:23:17
问题 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? 回答1: 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

How to write iOS app purely in C

走远了吗. 提交于 2019-12-16 22:22:10
问题 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? 回答1: 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

Objective-C dynamically created methods and compiler warnings

最后都变了- 提交于 2019-12-14 03:49:00
问题 If I generate methods dynamically on runtime and then call them - how can I convince compiler that the class will respond to undeclared (generated) methods and make it not throw warnings? Update in regard to answers When I generate the methods - their name is not known at compile time. To give an example - if I have a view controller MyFooController and it's initiated with method initWithFoo:(Foo*)foo , I'd be able to generate method like pushMyFooControllerWithFoo:(Foo *)foo for

Designated initializer should only invoke a designated initializer on 'super' When using protocols?

余生长醉 提交于 2019-12-14 02:34:52
问题 I have the following structure. I got class B which conforms to protocol A . protocol A defines a designated initializer which is -(instancetype)initWithInt:(int)count . However, when I go an implement the standard -(instancetype)init in class B and make it to use the designated initializer which is also implemented in class B, i am getting the warning "Designated initializer should only invoke a designated initializer on 'super'", while my designated initializer (which IMO is initWithInt )

How to import objc_retainAutoreleasedReturnValue?

折月煮酒 提交于 2019-12-13 06:05:50
问题 I saw usage of this function in Mike Ash's post on ARC and wanted to play around with it but i just can't find where it resides. I haven't found it in objc/runtime and similar files, runtime documentation doesn't mention it, googling also gave nothing to me. What the secret function it is? Where is it? How do i import it? 回答1: objc_retainAutoreleaseReturnValue() and the related methods from the ARC runtime support are defined in /usr/lib/libobjc.A.dylib , but not exposed in the public header

What does Objective-C property get resolved to in runtime?

不羁岁月 提交于 2019-12-13 04:46:37
问题 What does Objective-C property get resolved to in runtime? Will calling [obj valueForKey:@"property"] always yield the same result? e.g. obj.property 回答1: First, note that obj.property is precisely the same as [obj property] . Dot syntax is just syntactic sugar. While there are some small run-time implementation details related to properties that are different than other methods, for the purposes of this discussion, think only in terms of "I have an ivar named _key and a method called -key ."