
我正在写一篇正在写作的课程中.我想要做的是将调用转发给一个或另一个选择器,具体取决于它是对象还是基本类型.最终目标是我想“封装”基元,以便将它们添加到数组/字典中.为简单起见,这里通常出现的两种类型的值是Nsstrings和枚举.
简而言之,给定一个指针,有没有办法判断它是否是一个对象?
__unsafe_unretained ID argument;[anInvocation getArgument:&argument atIndex:2];// EXC_BAD_ACCESS if primitive (i.e. NSInteger value of 2 ( = 0x00000002) )if (![argument isKindOfClass:[NSObject class]]) { // Box the value ...} 有没有可以运行的测试?现在我的代码是愚蠢地做这个讨厌的伎俩:
// All my enums have at most 10 elements. I'm so bad at code.if ((NSInteger)argument < 10) { // Box the value ...} 提前致谢.
解决方法 您可以从方法签名中获取类型:NSMethodSignature *signature = [invocation methodSignature];const char* argType = [signature getArgumentTypeAtIndex:2];
这些类型列在Type Encodings in the Objective-C Runtime Guide下
您还应该在调用getArgument之前确保知道类型:atIndex ::
This method copIEs the argument stored at index into the storage pointed to by buffer. The size of buffer must be large enough to accommodate the argument value.
__unsafe_unretained ID argument;[anInvocation getArgument:&argument atIndex:2];
如果实际参数的大小大于sizeof(ID),这将在堆栈上写入过去的参数
总结以上是内存溢出为你收集整理的ios – 确定来自NSInvocation getArgument的if(void *)指针是对象还是原始指针全部内容,希望文章能够帮你解决ios – 确定来自NSInvocation getArgument的if(void *)指针是对象还是原始指针所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)