如何将Struct包装到NSObject

如何将Struct包装到NSObject

本文介绍了如何将Struct包装到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?

谢谢。

推荐答案

嗯,试着在

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 中拉出结构:

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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:34