objective-c-runtime

Swift : Alternative to .classForCoder()

倖福魔咒の 提交于 2020-01-03 09:09:26
问题 Given the following code: return TyphoonDefinition.withClass(AppDelegate.classForCoder()) { (definition) in definition.injectProperty("assembly") }) . . . it is necessary to use .classForCoder() , as .class() is struck out. This is certainly unintuitive compared to simply .class() . Is there an alternative that provides better readability? 回答1: To obtain a reference to the class Object, simply use 'ClassName.self' Example: return TyphoonDefinition.withClass(AppDelegate.self) { (definition) in

Objective-C - Is there a way for an Object to execute a method IMP directly as if it were its own?

夙愿已清 提交于 2020-01-03 03:54:17
问题 Presume I have an Object, an instance of MyClass. In Objective-C one can ask the Object to "perform" a selector by either sending it a message or using NSObject's "perform". This selector has to be defined at compile time as part of the Class definition, more precisely as an Instance method of that class OR with the help of the Obj-C Runtime, have the method added to the (entire) MyClass at runtime with class_addMethod. My question is as follows: Would it be possible to send an object the IMP

Copy a method IMP for multiple method swizzles

时光总嘲笑我的痴心妄想 提交于 2020-01-02 05:18:11
问题 I have a class set up that ideally will read the methods of any class passed in and then map all of them to on single selector at runtime before forwarding them off to their original selector. This does work right now, but I can only do it for one method at a time. The issue seems to be that once I swizzle the first method, my IMP to catch and forward the method has now been swapped with that other methods IMP. Any further attempts at this screw up because they use newly swapped IMP to

Understanding uniqueness of selectors in Objective-C

徘徊边缘 提交于 2019-12-30 10:58:14
问题 I am having problem understanding part of the function of "selectors", as described in Apple's guide. I've bolded the parts where I am getting confused: In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same

Why do Objective-C objects have to be dynamically allocated?

霸气de小男生 提交于 2019-12-30 01:53:04
问题 Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks. 回答1: the primary reason: not knowing how much stack size to reserve. existing conventions and uses also make lifting the restriction quite difficult. dynamic messaging does not matter in this case, as setting the right 'vtable' at initialization is trivial. in c++, the size of a stack object is always known (and if it's wrong, you know

-rewrite-objc and Objective-C in clang

我只是一个虾纸丫 提交于 2019-12-29 09:37:15
问题 Recently, I have one problem. The clang can translate Objective-C to c++ use -rewrite-objc. So I think, the first step. clang compile Objective-C to C++. And then compile only can use c++ compiler. Is it do like this? clang first translate Objective-C to C++ with runTime, and then compile to the machine code? 回答1: -rewrite-objc exists to translate ObjC to C++ so it can be compiled in the Visual Studio. It is still Objective-C semantics and you still need the objective-c runtime. It is not

Proper way of method swizzling in objective-C

橙三吉。 提交于 2019-12-28 11:59:25
问题 Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster post: http://nshipster.com/method-swizzling/ In the post the author has some method swizzling sample code. I am looking for someone to better explain to me what the author is doing.. In particular I am confused on the didAddMethod logic. Why is the author not just directly swapping/exchanging

Proper way of method swizzling in objective-C

微笑、不失礼 提交于 2019-12-28 11:59:09
问题 Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster post: http://nshipster.com/method-swizzling/ In the post the author has some method swizzling sample code. I am looking for someone to better explain to me what the author is doing.. In particular I am confused on the didAddMethod logic. Why is the author not just directly swapping/exchanging

Changed +load method order in Xcode 7

社会主义新天地 提交于 2019-12-28 02:56:06
问题 I found out that Xcode 7 (Version 7.0 (7A220)) changed the order in which +load methods for classes and categories are called during unit tests. If a category belonging to the test target implements a +load method, it is now called at the end, when instances of the class might've already been created and used. I have an AppDelegate , which implements +load method. The AppDelegate.m file also contains AppDelegate (MainModule) category. Additionally, there is a unit test file

intercept all objective c method calls

旧时模样 提交于 2019-12-24 19:01:48
问题 I wish to insert hooks whenever a method is called in my iOS app. So lets say there is a selector X, I wish to log "Method X starting" before the method executes, and then log "Method X ended" after the execution. I am aware of an approach where I can swizzle the implementation of sel X with the one which has the hook before and after the call to "itself", so that I can be notified when the method has executed. But, this will only work if i know the method in advance. I wish to insert hooks