Objective-C: 调用带有可变参数的选择器
Objective-C: Calling selectors with variable arguments
我遇到了以下问题,我已经尝试了很多。我还阅读了 Whosebug 中的其他问题,例如:
Objective-C: Calling selectors with multiple arguments
和 Cocoa 关于选择器的核心能力,但我正在寻找将参数变量传递给选择器的最佳方法。
-(void) runAllStatusDelegates : (SEL)selector
{
for (NSValue *val in self.statusDelegates)
{
id<StatusDelegate> delegate = val;
if ([delegate respondsToSelector:selector])
{
[delegate performSelector:selector];
}
}
}
该方法负责调用委托内部的方法。该参数是一个选择器。我的问题是选择器可以有 0 - 3 个参数,如下所示。
-(void) handleBluetoothEnabled:(BOOL)aEnabled
{
if (aEnabled)
{
[self.statusDelegate bluetoothEnabled];
if (_storedPenSerialNumber != nil && ![_storedSerialNumber isEqual:kUnknownPenID])
{
[self runAllStatusDelegates: @selector(penConnected : _storedSerialNumber : _storedFirmware:)];
}
}
else
{
[self.statusDelegate bluetoothDisabled];
}
}
-(void) handleChooseDevice:(BluetoothDeviceList*)aDevices
{
NSLog(@"Handle Choose Device");
[self runAllStatusDelegates: @selector(chooseDevice:aDevices:)];
}
-(void) handleDiscoveryStarted
{
NSLog(@"Discovery Started");
[self runAllStatusDelegates: @selector(searchingForBluetoothDevice)];
[self.statusDelegate handleStatus:@"Searching for your digipen"];
}
此实现无效,因为 performSelector 无法识别选择器。
我也尝试用 @selector(penConnected::) withObject:_storedSerialNumber 来实现它,但是我也必须用额外的参数来实现另一种方法,我不想要那个。
我是 objective-c 的新手,所以我不是很熟悉所有的可能性。
我的想法是将一个字符串和一个参数数组传递给 runAllStatusDelegates 并在该方法中构建选择器,但这是最好的方法还是有更方便的方法?
你可以在这种情况下使用 NSInvocation
SEL theSelector = @selector(yourSelector:);
NSMethodSignature *aSignature = [NSMethodSignature instanceMethodSignatureForSelector:theSelector];
NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:aSignature];
[anInvocation setSelector:theSelector];
[anInvocation setTarget:self];
[anInvocation setArgument:&arg1 atIndex:2];
[anInvocation setArgument:&arg2 atIndex:3];
[anInvocation setArgument:&arg3 atIndex:4];
[anInvocation setArgument:&arg4 atIndex:5];
//Add more
请注意,索引 0 和 1 处的参数是为目标和选择器保留的。
更多信息http://www.cocoawithlove.com/2008/03/construct-nsinvocation-for-any-message.html
您可以将参数绑定到选择器
NSDictionary *argInfo=@{@"arg1":arg1,@"arg2":arg2,...};
objc_setAssociatedObject(self,@selector(chooseDevice:aDevices:),argInfo,OBJC_ASSOCIATION_COPY)
[self runAllStatusDelegates: @selector(chooseDevice:aDevices:)];
然后在
-(void) runAllStatusDelegates : (SEL)selector
{
for (NSValue *val in self.statusDelegates)
{
id<StatusDelegate> delegate = val;
if ([delegate respondsToSelector:selector])
{
NSDictionary *argInfo=objc_getAssociatedObject(self, selector);
//call the fun use arginfo
}
}
}
我个人不喜欢 NSInvocation
复杂的签名。它非常适合在队列上排队一个简单的函数调用,并在需要时 运行 它,但对于你的情况,你知道选择器,所以你真的不需要走调用路线。我通常发现调用更有用,如果你实际上不知道你想在编译时调用的选择器,也许它是由你的 API 等决定的
所以我要做的就是将一个块传递到您的 runAllStatusDelegates
方法中,该方法将针对您的所有委托执行:
- (void)performSelector:(SEL)selector againstAllDelegatesWithExecutionBlock:(void (^)(id<StatusDelegate>))blockToExecute
{
for (id<StatusDelegate> delegate in self.statusDelegates)
{
if ([delegate respondsToSelector:selector])
{
blockToExecute(delegate);
}
}
}
然后当你想用一个函数调用你的委托时,它看起来像这样:
[self performSelector:@selector(handleAnswerOfLifeFound)
againstAllDelegatesWithExecutionBlock:^(id<StatusDelegate> delegate){
[delegate handleAnswerOfLifeFound];
}];
我想唯一的缺点可能是您可以更改选择器并将不同的函数传递到块中。我如何解决这个问题是通过实际确保并非所有方法都是可选的,或者如果它们是可选的以在块内进行实际检查,这将清理签名:
- (void)callAllDelegatesWithBlock:(void (^)(id<StatusDelegate>))blockToExecute
{
for (id<StatusDelegate> delegate in self.statusDelegates)
{
blockToExecute(delegate);
}
}
然后是可选方法的实际用法:
[self callAllDelegatesWithBlock^(id<StatusDelegate> delegate){
if([delegate respondsToSelector:@selector(handleAnswerOfLifeFound)]){
[delegate handleAnswerOfLifeFound];
}
}];
仍然容易出错,但至少更整洁了。
我遇到了以下问题,我已经尝试了很多。我还阅读了 Whosebug 中的其他问题,例如: Objective-C: Calling selectors with multiple arguments 和 Cocoa 关于选择器的核心能力,但我正在寻找将参数变量传递给选择器的最佳方法。
-(void) runAllStatusDelegates : (SEL)selector
{
for (NSValue *val in self.statusDelegates)
{
id<StatusDelegate> delegate = val;
if ([delegate respondsToSelector:selector])
{
[delegate performSelector:selector];
}
}
}
该方法负责调用委托内部的方法。该参数是一个选择器。我的问题是选择器可以有 0 - 3 个参数,如下所示。
-(void) handleBluetoothEnabled:(BOOL)aEnabled
{
if (aEnabled)
{
[self.statusDelegate bluetoothEnabled];
if (_storedPenSerialNumber != nil && ![_storedSerialNumber isEqual:kUnknownPenID])
{
[self runAllStatusDelegates: @selector(penConnected : _storedSerialNumber : _storedFirmware:)];
}
}
else
{
[self.statusDelegate bluetoothDisabled];
}
}
-(void) handleChooseDevice:(BluetoothDeviceList*)aDevices
{
NSLog(@"Handle Choose Device");
[self runAllStatusDelegates: @selector(chooseDevice:aDevices:)];
}
-(void) handleDiscoveryStarted
{
NSLog(@"Discovery Started");
[self runAllStatusDelegates: @selector(searchingForBluetoothDevice)];
[self.statusDelegate handleStatus:@"Searching for your digipen"];
}
此实现无效,因为 performSelector 无法识别选择器。
我也尝试用 @selector(penConnected::) withObject:_storedSerialNumber 来实现它,但是我也必须用额外的参数来实现另一种方法,我不想要那个。 我是 objective-c 的新手,所以我不是很熟悉所有的可能性。
我的想法是将一个字符串和一个参数数组传递给 runAllStatusDelegates 并在该方法中构建选择器,但这是最好的方法还是有更方便的方法?
你可以在这种情况下使用 NSInvocation
SEL theSelector = @selector(yourSelector:);
NSMethodSignature *aSignature = [NSMethodSignature instanceMethodSignatureForSelector:theSelector];
NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:aSignature];
[anInvocation setSelector:theSelector];
[anInvocation setTarget:self];
[anInvocation setArgument:&arg1 atIndex:2];
[anInvocation setArgument:&arg2 atIndex:3];
[anInvocation setArgument:&arg3 atIndex:4];
[anInvocation setArgument:&arg4 atIndex:5];
//Add more
请注意,索引 0 和 1 处的参数是为目标和选择器保留的。
更多信息http://www.cocoawithlove.com/2008/03/construct-nsinvocation-for-any-message.html
您可以将参数绑定到选择器
NSDictionary *argInfo=@{@"arg1":arg1,@"arg2":arg2,...};
objc_setAssociatedObject(self,@selector(chooseDevice:aDevices:),argInfo,OBJC_ASSOCIATION_COPY)
[self runAllStatusDelegates: @selector(chooseDevice:aDevices:)];
然后在
-(void) runAllStatusDelegates : (SEL)selector
{
for (NSValue *val in self.statusDelegates)
{
id<StatusDelegate> delegate = val;
if ([delegate respondsToSelector:selector])
{
NSDictionary *argInfo=objc_getAssociatedObject(self, selector);
//call the fun use arginfo
}
}
}
我个人不喜欢 NSInvocation
复杂的签名。它非常适合在队列上排队一个简单的函数调用,并在需要时 运行 它,但对于你的情况,你知道选择器,所以你真的不需要走调用路线。我通常发现调用更有用,如果你实际上不知道你想在编译时调用的选择器,也许它是由你的 API 等决定的
所以我要做的就是将一个块传递到您的 runAllStatusDelegates
方法中,该方法将针对您的所有委托执行:
- (void)performSelector:(SEL)selector againstAllDelegatesWithExecutionBlock:(void (^)(id<StatusDelegate>))blockToExecute
{
for (id<StatusDelegate> delegate in self.statusDelegates)
{
if ([delegate respondsToSelector:selector])
{
blockToExecute(delegate);
}
}
}
然后当你想用一个函数调用你的委托时,它看起来像这样:
[self performSelector:@selector(handleAnswerOfLifeFound)
againstAllDelegatesWithExecutionBlock:^(id<StatusDelegate> delegate){
[delegate handleAnswerOfLifeFound];
}];
我想唯一的缺点可能是您可以更改选择器并将不同的函数传递到块中。我如何解决这个问题是通过实际确保并非所有方法都是可选的,或者如果它们是可选的以在块内进行实际检查,这将清理签名:
- (void)callAllDelegatesWithBlock:(void (^)(id<StatusDelegate>))blockToExecute
{
for (id<StatusDelegate> delegate in self.statusDelegates)
{
blockToExecute(delegate);
}
}
然后是可选方法的实际用法:
[self callAllDelegatesWithBlock^(id<StatusDelegate> delegate){
if([delegate respondsToSelector:@selector(handleAnswerOfLifeFound)]){
[delegate handleAnswerOfLifeFound];
}
}];
仍然容易出错,但至少更整洁了。