如何将 Struct 包装到 NSObject 中

发布于 2024-11-02 07:30:58 字数 128 浏览 6 评论 0原文

这应该是微不足道的......我想,但我找不到如何将 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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄司 2024-11-09 07:30:58

嗯,尝试查看 https://developer.apple 处的 NSValue .com/documentation/foundation/nsvalue

您可以像这样使用它

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

然后将其“包装”到 NSValue 对象,例如:

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

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

对不⑦ 2024-11-09 07:30:58

像这样的东西就是 @Rpranata 正在谈论的类别:)

struct _Pair
{
    short val;
    short count;
};
typedef struct _Pair Pair;

@interface NSValue (Pair)
+ (id)valueWithPair:(Pair)pair;
- (Pair)pairValue;
@end

@implementation NSValue (Pair)
+ (id)valueWithPair:(Pair)pair
{
    return [NSValue value:&pair withObjCType:@encode(Pair)];
}
- (Pair)pairValue
{
    Pair pair; [self getValue:&pair]; return pair;
}
@end

用法:

// Boxing
Pair pair; pair.val = 2; pair.count = 1;
NSValue *value = [NSValue valueWithPair:pair];

// Unboxing
NSValue value = ...
Pair anotherPair = [value pairValue];

Something like like this would be the category @Rpranata is talking about :)

struct _Pair
{
    short val;
    short count;
};
typedef struct _Pair Pair;

@interface NSValue (Pair)
+ (id)valueWithPair:(Pair)pair;
- (Pair)pairValue;
@end

@implementation NSValue (Pair)
+ (id)valueWithPair:(Pair)pair
{
    return [NSValue value:&pair withObjCType:@encode(Pair)];
}
- (Pair)pairValue
{
    Pair pair; [self getValue:&pair]; return pair;
}
@end

Usage:

// Boxing
Pair pair; pair.val = 2; pair.count = 1;
NSValue *value = [NSValue valueWithPair:pair];

// Unboxing
NSValue value = ...
Pair anotherPair = [value pairValue];
无所的.畏惧 2024-11-09 07:30:58

使用 + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;

从文档中:“创建一个包含指定 CoreGraphics 仿射变换结构的新值对象。”

CGAffineTransform fooAffineTransform = CGAffineTransformMakeScale(.5, .5);
[NSValue valueWithCGAffineTransform:fooAffineTransform];

+valueWithCGAffineTransform 自 iOS 2.0 以来就已存在。

Use + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;

From the docs: "Creates a new value object containing the specified CoreGraphics affine transform structure."

CGAffineTransform fooAffineTransform = CGAffineTransformMakeScale(.5, .5);
[NSValue valueWithCGAffineTransform:fooAffineTransform];

+valueWithCGAffineTransform has existed since iOS 2.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文