如何将 Struct 包装到 NSObject 中 [英] How to wrap a Struct into NSObject
问题描述
这应该是微不足道的......我想,但我找不到如何将 Struct 变量包装到 NSObject
的方法.有没有办法这样做?如果没有,我将如何将结构添加到 NSMutableArray
中?
this is supposed to be trivial… I think, but I can't find a way how to wrap a Struct variable into an NSObject
. Is there a method to do so? If not, how would I go about adding a struct into an NSMutableArray
?
谢谢.
推荐答案
嗯,试着看看 https://developer.apple.com/documentation/foundation/nsvalue
Hm, try to look at the NSValue
at https://developer.apple.com/documentation/foundation/nsvalue
你可以像这样使用
struct aStruct
{
int a;
int b;
};
typedef struct aStruct aStruct;
然后将它包装"到一个 NSValue
对象中,例如:
Then sort of "wrap" it to an NSValue
object like:
aStruct struct; struct.a = 0; struct.b = 0;
NSValue *anObj = [NSValue value:&struct withObjCType:@encode(aStruct)];
NSArray *array = @[anObj];
要从 NSValue
中提取结构体,请使用:
To pull the struct out from NSValue
use:
NSValue *anObj = [array firstObject];
aStruct struct;
[anObj getValue:&struct];
我想稍后,您可以从 NSValue 中获得一个类别以使其更好=D
I guess later on, you can have a category from NSValue to make that better =D
这篇关于如何将 Struct 包装到 NSObject 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!