performselector

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:

Running performSelector: on an object that returns a double, not id

£可爱£侵袭症+ 提交于 2021-02-07 14:40:40
问题 How can I run an arbitrary selector on an object, the return of which is a double? For example, I have obj A, which has method -(double)blah; How can I do double res = [obj performSelector:@selector(blah)]; ? performSelector returns an id type object, so should I cast from id to NSInteger to double - that will lose precision? Also, I do not want to use the obj's methodSignatureForSelector (meaning, no NSMethodSignature and no NSInvocation ) because it is a huge CPU drain at run-time. 回答1: You

Running performSelector: on an object that returns a double, not id

纵饮孤独 提交于 2021-02-07 14:36:36
问题 How can I run an arbitrary selector on an object, the return of which is a double? For example, I have obj A, which has method -(double)blah; How can I do double res = [obj performSelector:@selector(blah)]; ? performSelector returns an id type object, so should I cast from id to NSInteger to double - that will lose precision? Also, I do not want to use the obj's methodSignatureForSelector (meaning, no NSMethodSignature and no NSInvocation ) because it is a huge CPU drain at run-time. 回答1: You

How to invoke a class method using performSelector() on AnyClass in Swift?

☆樱花仙子☆ 提交于 2020-07-06 10:41:40
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

How to invoke a class method using performSelector() on AnyClass in Swift?

北慕城南 提交于 2020-07-06 10:41:26
问题 In ObjC you could simply invoke a class method using the class method from NSObject . [Machine performSelector:@selector(calculate:) withObject:num]; But how do you do this in Swift 2.2? @objc(Machine) // put it here, so you can simply copy/paste into Playground class Machine: NSObject { static func calculate(param: NSNumber) -> String { if param.integerValue > 5 { return "42" } return "42" // there is only 1 answer to all the questions :D } } if let aClass = NSClassFromString("Machine") {

Using [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] to let the scheduled selectors fire

允我心安 提交于 2020-01-25 01:58:07
问题 In my application at some point I have a bunch of messages scheduled using performSelector . Under some conditions, while handling an UI action, I need to wait for all the currently scheduled selectors to fire. I could place my code in another method and schedule it using performSelector:target:argument:order:modes: with order value high enough to be sure it will fire last, but there are reasons why I think that would make an ugly solution. So I send [[NSRunLoop currentRunLoop] runUntilDate:

respondToSelector / performSelector with parameter from a string in Swift 3

 ̄綄美尐妖づ 提交于 2020-01-05 03:37:34
问题 I have to call method of a class. This method potentially exists or not. I'm looking for a solution from 2 hours. The following works without parameter sayHello() I have the class : class myClass: NSObject { func say(something:String){ print("I say : \(something)") } func sayHello(){ print("Hello") } } 1st step : Create a selector from a string with a parameter let selector = NSSelectorFromString("sayHello" ) let selectorWithParam = NSSelectorFromString("say:" ) 2nd step : Test if the method

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

Objective-C: [NSObject performSelector: onThread…] does not get called if the thread is not the main one

独自空忆成欢 提交于 2019-12-25 09:13:27
问题 Very similar issue is already discussed here. The problem at hand and what I am trying to achieve is to call a function on a given object in the thread it is created at. Here is the complete case: an instance of class A is created in a given NSThread (Thread A) (not the main one). The instance keeps its creating NSThread as a member variable. an instance of class B has one of its member functions executing in another NSThread - Thread B, and wants to call a function of A in A's creation

Update ULabel immediately while downloading files

泄露秘密 提交于 2019-12-25 08:57:43
问题 I am using NSURLConnection to download the files . I have a UILabel in my View which has to display the currently downloading files. The UILabel is getting updated at the starting and the end. Lets say I am download 10 files. I am able to set the Label text before start downloading and after completing the download. I can understand that, the method which I am trying to call is not running in main thread, So I have used the following code to make it run in the main thread , [_myHome