NSArray 第三方子类保存原始值
更新:随着新的添加(下标和数字),这个问题已经过时了。
我最近看到了一些子类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我这样做,我可能只会在 NSArray 和/或 NSMutableArray 上编写一个类别。像这样的事情:
不过,实际上,这似乎是比其价值更多的工作。将基元包装在 NSNumber 中并将它们拉出来并不是那么难......
If I were doing this, I'd probably just write a category on NSArray and/or NSMutableArray. Something like this:
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...