objective-c – NSInvocation nil参数

objective-c – NSInvocation nil参数,第1张

概述我如何(或者甚至可以)将nil参数传递给NSInvocation对象? 我试着这样做: NSMethodSignature* signature = [AClass instanceMethodSignatureForSelector:@selector(aMethod:theOtherArg:)];NSInvocation* invocation = [NSInvocation invocat 我如何(或者甚至可以)将nil参数传递给NSInvocation对象?

我试着这样做:

NSMethodSignature* signature = [AClass instanceMethodSignatureForSelector:@selector(aMethod:theOtherArg:)];NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];[invocation setTarget: aTargetobj];[invocation setSelector: @selector(aMethod:theOtherArg:)];/* I've trIEd both this way */AnObj* arg1 = nil;AnotherObj* arg2 = nil;[invocation setArgument: &arg1 atIndex:2];[invocation setArgument: &arg2 atIndex:3];/* and this way *///[invocation setArgument: nil atIndex:2];//[invocation setArgument: nil atIndex:3];NSInvocationoperation* operation = [[NSInvocationoperation alloc] initWithInvocation:invocation];//opQueue is an NSOperationQueue object[opQueue addOperation:operation];

第一种方法会因此消息而崩溃:

Thread 0 Crashed:0   libSystem.B.dylib              0x927c1f10 strlen + 161   com.apple.CoreFoundation       0x92dd1654 __NSI3 + 5002   com.apple.CoreFoundation       0x92dd1994 -[NSInvocation retainArguments] + 1323   com.apple.Foundation           0x96a50c5e -[NSInvocationoperation initWithInvocation:] + 78

第二种方法会因此消息而崩溃:

Error: Exiting due to caught object *** -[NSInvocation setArgument:atIndex:]: NulL address argument

在此先感谢您的帮助!

解决方法 是的,你可以传递零参数.更确切地说,您可以传递内容为nil的有效指针.

我无法重现您的特定问题.这是我用来测试它的代码.程序中可能还有其他东西导致错误.

#import <Foundation/Foundation.h>@interface Person : NSObject {    Nsstring *name;    NSUInteger age;}- (voID)setname:(Nsstring *)personname age:(NSNumber *)personAge;@end@implementation Person- (voID)setname:(Nsstring *)personname age:(NSNumber *)personAge {    NSLog(@"setname:%@ age:%@",personname,personAge);    name = [personname copy];    age = [personAge unsignedIntegerValue];}@endint main() {    NSautoreleasePool *pool = [NSautoreleasePool new];                Person *person = [Person new];    SEL sel = @selector(setname:age:);    NSMethodSignature *sig = [Person instanceMethodSignatureForSelector:sel];    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];    [inv setTarget:person];    [inv setSelector:sel];    Nsstring *name = nil;    NSNumber *age = nil;    [inv setArgument:&name atIndex:2];    [inv setArgument:&age atIndex:3];    // [inv retainArguments];    // [inv invoke];    NSInvocationoperation *op = [[[NSInvocationoperation alloc] initWithInvocation:inv] autorelease];    NSOperationQueue *queue = [NSOperationQueue new];    [queue addOperation:op];    [pool release];    return 0;}

我还测试了它,没有使用NSInvocationoperation并直接发送-retainArguments,因为这似乎是触发你的错误.

对于记录,您的第二种方法将无法工作,因为 – [NSInvocation setArgument:atIndex:]需要一个指向将被复制的缓冲区的有效内存地址.对于对象参数,缓冲区必须包含对象的地址.对象的地址可以为零,但缓冲区的地址必须有效.

总结

以上是内存溢出为你收集整理的objective-c – NSInvocation nil参数全部内容,希望文章能够帮你解决objective-c – NSInvocation nil参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1025313.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存