NSArray 第三方子类保存原始值

发布于 2024-12-24 17:44:55 字数 507 浏览 0 评论 0原文

更新:随着新的添加(下标和数字),这个问题已经过时了。


我最近看到了一些子类 NSArray (或任何集合类)的代码来保存原始值。

这个想法不是写:

myArray = [NSArray arrayWithObject:[NSNumber numberWithInt:42]];
[[myArray objectAtIndex:0] intValue];

你可以写:

myArray = [NSPrimitiveObjectArray arrayWithObject:42];
[myArray objectAtIndex:0];

我再也找不到这个代码了。有人也会看到它并记住网址吗?

我也很感谢使用过它或类似代码的人的反馈以及他们的想法。当我看到这段代码时没有保存链接的原因是我有一种使用这种语言进行黑客攻击的感觉,从长远来看可能会带来问题。

Update: With new additions (subscripting and numerals), this question is out-of date.


I have seen recently some code for a class subclassing NSArray (or any collection class) to hold primitive values.

The idea was instead of writing:

myArray = [NSArray arrayWithObject:[NSNumber numberWithInt:42]];
[[myArray objectAtIndex:0] intValue];

you could write:

myArray = [NSPrimitiveObjectArray arrayWithObject:42];
[myArray objectAtIndex:0];

I can't find this code anymore. Would someone have seen it as well, and remember the url?

I would also appreciate feedback from people who have used it -or similar code- and what they think about it. The reason I didn't save the link when seeing this code was that I got a feeling of hacking with the language that might bring problems in the long term.

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

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

发布评论

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

评论(1

活雷疯 2024-12-31 17:44:55

如果我这样做,我可能只会在 NSArray 和/或 NSMutableArray 上编写一个类别。像这样的事情:

@interface NSMutableArray (PrimitiveAccessors)

- (void)addInteger:(NSInteger)value;
- (NSInteger)integerAtIndex:(NSUInteger)index;
- (void)addFloat:(float)value;
- (float)floatAtIndex:(NSUInteger)index;

// etc...

@end

@implementation NSMutableArray (PrimitiveAccessors)

- (void)addInteger:(NSInteger)value;
{
    [self addObject:[NSNumber numberWithInteger:value]];
}

- (NSInteger)integerAtIndex:(NSUInteger)index;
{
    id obj = [self objectAtIndex:index];
    if (![obj respondsToSelector:@selector(integerValue)]) return 0;
    return [obj integerValue];
}

- (void)addFloat:(float)value;
{
    [self addObject:[NSNumber numberWithFloat:value]];
}

- (float)floatAtIndex:(NSUInteger)index;
{
    id obj = [self objectAtIndex:index];
    if (![obj respondsToSelector:@selector(floatValue)]) return 0;
    return [obj floatValue];
}

// etc...

@end

不过,实际上,这似乎是比其价值更多的工作。将基元包装在 NSNumber 中并将它们拉出来并不是那么难......

If I were doing this, I'd probably just write a category on NSArray and/or NSMutableArray. Something like this:

@interface NSMutableArray (PrimitiveAccessors)

- (void)addInteger:(NSInteger)value;
- (NSInteger)integerAtIndex:(NSUInteger)index;
- (void)addFloat:(float)value;
- (float)floatAtIndex:(NSUInteger)index;

// etc...

@end

@implementation NSMutableArray (PrimitiveAccessors)

- (void)addInteger:(NSInteger)value;
{
    [self addObject:[NSNumber numberWithInteger:value]];
}

- (NSInteger)integerAtIndex:(NSUInteger)index;
{
    id obj = [self objectAtIndex:index];
    if (![obj respondsToSelector:@selector(integerValue)]) return 0;
    return [obj integerValue];
}

- (void)addFloat:(float)value;
{
    [self addObject:[NSNumber numberWithFloat:value]];
}

- (float)floatAtIndex:(NSUInteger)index;
{
    id obj = [self objectAtIndex:index];
    if (![obj respondsToSelector:@selector(floatValue)]) return 0;
    return [obj floatValue];
}

// etc...

@end

Really though, it sort of seems like more work than it's worth. Wrapping primitives in NSNumber and pulling them back out just isn't that hard...

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