Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
655 views
Welcome To Ask or Share your Answers For Others

1 Answer

Hm, try to look at the NSValue at https://developer.apple.com/documentation/foundation/nsvalue

You can use it like

struct aStruct
{
    int a;
    int b;
};
typedef struct aStruct aStruct;

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];

To pull the struct out from NSValue use:

NSValue *anObj = [array firstObject];
aStruct struct;
[anObj getValue:&struct];

I guess later on, you can have a category from NSValue to make that better =D


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...