gpt4 book ai didi

ios - 带有枚举的 NSInitation?

转载 作者:行者123 更新时间:2023-11-29 00:42:18 25 4
gpt4 key购买 nike

------------ 编辑------------

长话短说:

看来我错误地使用了 NSInvocation 类(缺乏指针知识)。

使用它们的方式是这样的(可行):

NSString *str = @"string";
UIControlState state = UIControlStateNormal;
[invocation setArgument: &str atIndex: 2];
[invocation setArgument: &state atIndex: 3];

---------------- 原始问题--------

是否可以使用枚举参数setArgument:atIndex:

请参阅以下示例(不起作用):

UIButton *btn = ...;
SEL selector = @selector(setTitle:forState:);
NSInovcation *invocation = [NSInovcation invocationWithMethodSignature: [btn methodSignatureForSelector: selector]];
[invocation setSelector: selector];
[invocation setArgument: @"Test" atIndex: 2];

//Nothing happens
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];

//Runtime error (the pointer is NULL)
UIControlState *state = UIControlStateNormal;
[invocation setArgument: state atIndex: 3];

//No errors but no effect
int state2 = 0;
[invocation setArgument: &state atIndex: 3];

//Compile error (casting)
[invocation setArgument: @(UIControlStateNormal) atIndex: 3];
//Runtime EXC_BAD_ACCESS
[invocation setArgument: (void *)@(UIControlStateNormal) atIndex: 3];

...
[invocation invokeWithTarget: btn];

奇怪的是(或不是),它与 performSelector:... 一起工作得很好:

[btn performSelector: selector withObject: @"Testing" withObject:@(UIControlStateNormal)];

我不确定我是否以正确的方式使用 NSInovcation。有人愿意详细说明吗?

最佳答案

试试这个:

NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];

UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];

关于ios - 带有枚举的 NSInitation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39149483/

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