gpt4 book ai didi

objective-c - @selector - 有多个参数?

转载 作者:太空狗 更新时间:2023-10-30 03:10:19 25 4
gpt4 key购买 nike

我今天第一次使用 @selector,但一直无法弄清楚如何执行以下操作?如果您有多个参数,您将如何编写 @selector

没有参数:

-(void)printText {
NSLog(@"Fish");
}

[self performSelector:@selector(printText) withObject:nil afterDelay:0.25];

单个参数:

-(void)printText:(NSString *)myText {
NSLog(@"Text = %@", myText);
}

[self performSelector:@selector(printText:) withObject:@"Cake" afterDelay:0.25];

两个参数:

-(void)printText:(NSString *)myText andMore:(NSString *)extraText {
NSLog(@"Text = %@ and %@", myText, extraText);
}

[self performSelector:@selector(printText:andMore:) withObject:@"Cake" withObject:@"Chips"];

多个参数:(即超过 2 个)

NSInvocation

最佳答案

 - (id)performSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject

来自Documentation :

This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.

所以在你的情况下你会使用:

[self performSelector:@selector(printText:andMore:)
withObject:@"Cake"
withObject:@"More Cake"]

关于objective-c - @selector - 有多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2297613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com