nsinvocation

Dynamically call a function in Swift

℡╲_俬逩灬. 提交于 2021-02-19 03:37:05
问题 I’m trying to implement a router pattern in Swift and can’t figure out how to dynamically call a method. For example, there’s an array of arguments: let arguments: [Any] = [100, 0.5, "Test"] And a handler which needs to be called: public class MyClass { public func handler(value1: Int, value2: Double, text: String) { print("int=\(value1), double=\(value2), string=\(text)") } } let myClass = MyClass() The router has an untyped function reference which it will use as a callback: let callback:

iOS中perform+@selector多参数传递

房东的猫 提交于 2020-04-22 02:10:38
iOS中performSelector实现多参数传递 关于performSelector实现多个参数传递,其实有2种方案,第一种是使用 NSInvocation,第二种是封装参数。按照参数传递的原则,例如c++中的线程,我们只能传递一个参数,但我们可以将参数封装进结构体或者class也是一种优秀的方案。 两种方法的比较,第一种使用了Runtime反射机制,效率又说折扣,可读性也不好好,第二种方法,效率较高,可读性也比较好。所以对比而言,推荐第二种将参数封装进结构体或者对象,作为DTO的作用。 第一种方法 NSInvocation : - (id)performSelector:(SEL)selector withObjects:(NSArray *)objects { // 方法签名(方法的描述) NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector]; if (signature == nil) { //可以抛出异常也可以不操作。 } // NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值) NSInvocation *invocation = [NSInvocation

ios 消息跳转处理

折月煮酒 提交于 2020-03-08 19:23:17
一.消息转发流程 当向Objective-C对象发送一个消息,但runtime在当前类及父类中找不到此selector对应的方法时,消息转发(message forwarding)流程开始启动。 动态方法解析(Dynamic Method Resolution或Lazy method resolution) 向当前类(Class)发送 resolveInstanceMethod: (对于类方法则为 resolveClassMethod: )消息,如果返回YES,则系统认为请求的方法已经加入到了,则会重新发送消息。 快速转发路径(Fast forwarding path) 若果当前target实现了 forwardingTargetForSelector: 方法,则调用此方法。如果此方法返回除nil和self的其他对象,则向返回对象重新发送消息。 慢速转发路径(Normal forwarding path) 首先runtime发送 methodSignatureForSelector: 消息查看Selector对应的方法签名,即参数与返回值的类型信息。如果有方法签名返回,runtime则根据方法签名创建描述该消息的 NSInvocation ,向当前对象发送 forwardInvocation: 消息,以创建的NSInvocation对象作为参数

How to pass an array to an objc method that expects var args (eg …')

偶尔善良 提交于 2020-01-04 01:29:14
问题 I have a method in a library that looks like so: - (id)initWithSomeObjects:(NSString *)something, ... NS_REQUIRES_NIL_TERMINATION; I'd really like to call it with an array instead of var args, because the number of objects i'd like to pass in is changeable. Is there some way, using performSelector or NSInvocation or objc_msgSend or whatever, that i can call the var args method, with the arguments coming from an array? 回答1: There is no easy way to do this, because how arguments are passed goes

Is there any alternative for NSInvocation in Swift?

偶尔善良 提交于 2020-01-03 21:12:11
问题 I'm trying to invoke a selector, with multiple (2+) arguments (the number of arguments can be determined). However, the selector is unknown at compile time (generated with NSSelectorFromString, actually). In Objective-C, I could create an invocation and set arguments to it and invoke it. But this is not available in Swift. Is there any way around this? Like: let obj = SomeClass() let selector = NSSelectorFromString("arg1:arg2:arg3:") //selector, arguments known only at runtime //invoke

Using NSUndoManager with prepareWithInvocationTarget, Gestures and Objects

无人久伴 提交于 2019-12-25 08:25:18
问题 I have a Drawing App of sorts. I want to Implement Undo/Redo. Though I'm running into difficulty with storing the Original and New Values for the Undo/Redo. With Gestures I need to store a Few things: transform, Center, for the Properties Dialog, I need to store many more, Color, Font, Font-Size, Outline, Outline Color, the Text It self, etc. I've created a NSMutableDictionary of the Attributes that the user can change in a Single Gesture/Properties Popover. I wanted to use Rob's Answer for

getArgument of NSInvocation of current method always returns null

为君一笑 提交于 2019-12-24 19:14:59
问题 I want to get the name of the arguments of the current function I am in so that I can prepare loading that object from the filesystem if it's not present on the current instance. (for instance if [foo dictTest] is not available I want to load it's prior saved plist version into exactly that ivar) I want to find the file by providing the ivar name that I provided as an argument to the current function. This is the function code: -(NSDictionary*)getCachedDictionary:(NSDictionary*)dict{

Index-based fetching of Objective-C argument values

匆匆过客 提交于 2019-12-24 05:27:08
问题 I want to dynamically create the NSInvocation for the current method with the correct argument values . Typically, one might do this: - (void)messageWithArg:(NSString *)arg arg2:(NSString *)arg2 { NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:_cmd]]; [invocation setTarget:self]; /* * How do we set the argument values here dynamically? */ } Setting the argument values explicitly is trivial and we could do something like this:

Calling a block though runtime, anything similar to NSInvocation?

空扰寡人 提交于 2019-12-24 01:45:24
问题 I have block of unknown type (as id ) and array of arguments that need to passed into that block. Arguments may be objects or numbers/structs boxed as NSNumber/NSValue. Block may also return an object, number or struct. This is a library code, and types of arguments are not known beforehand. Assuming that I can can dynamically read signature from the block descriptor, is there a way to construct something like an NSInvocation to call a block? 回答1: Surprisingly this works: CGAffineTransform (

Passing blocks to asynchronous methods

自古美人都是妖i 提交于 2019-12-23 20:07:26
问题 I'm passing a block to an asynchronous method which executes this block later. My app crashes if I don't copy the block before passing it to someMethod:success:failure: Is there a way to copy the blocks in forwardInvocation: instead of copying it before passing it to someMethod:success:failure: ? The flow is someMethod:success:failure: -> forwardInvocation: -> httpGet:success:failure httpGet:success:failure: executes the success or the failure block depending on the HTTP status code. //