
我有类定义如下:
@interface Wheel:NSObject { int size; float diameter; } @end @interface Car:NSObject { Nsstring *model; Nsstring *make; NSMutableArray *wheels; } @end 如何强制轮阵列只保留轮对象与代码? (绝对不是其他对象)
解决方法 2015年更新这个答案是在2011年初写的,开始:
What we really want is parametric polymorphism so you Could declare,say,
NSMutableArray<Nsstring>; but alas such is not available.
在2015年,苹果显然改变了这一点,在“Objective-C”中引入了“轻量级泛型”,现在你可以声明:
NSMutableArray<Nsstring *> *onlyStrings = [NSMutableArray new];
但所有的都不是什么似乎,注意“轻量级”…然后注意上面的声明的初始化部分不包含任何通用符号。虽然苹果已经引入了参数集合,并将非字符串直接添加到上述数组中,但是只有Strings,如下所示:
[onlyStrings addobject:@666]; // <- Warning: Incompatible pointer types...
将违反所示的警告,类型安全几乎没有皮肤深。考虑方法:
- (voID) push:(ID)obj onto:(NSMutableArray *)array{ [array addobject:obj];} 和代码片段在同一个类的另一个方法中:
NSMutableArray<Nsstring *> *oops = [NSMutableArray new];[self push:@"asda" onto:oops]; // add a string,fine[self push:@42 onto:oops]; // add a number,no warnings...
苹果已经实现的本质上是一个提示系统,以帮助与Swift的自动互 *** 作,它具有类型安全的泛型的风味。然而在Objective-C方面,虽然编译器提供了一些额外的提示,系统是“轻量级的”,类型完整性最终仍然是程序员 – 就像Objective-C方式。
那么你应该使用什么?新的轻量级/伪类泛型,或者为你的代码设计自己的模式?真的没有正确的答案,找出什么是有意义的在你的情况,使用它。
例如:如果你的目标是与Swift互 *** 作,你应该使用轻量级的泛型!然而,如果集合的类型完整性在您的场景中很重要,那么您可以将轻量级泛型与您自己的Objective-C代码相结合,这将强制Swift将在其侧面的类型完整性。
2011年答案的余下部分
另一个选项是NSMutableArray的一个快速子类,你可以在你的单形数组中使用你想要的对象。这个选项不给你静态类型检查(在你以前得到的Obj-C),你得到运行时异常插入错误的类型,正如你得到运行时异常索引超出范围等。
这没有经过彻底测试,并假定关于重写NSMutableArray的文档是正确的…
@interface MonomorphicArray : NSMutableArray{ Class elementClass; NSMutableArray *realArray;}- (ID) initWithClass:(Class)element andCapacity:(NSUInteger)numItems;- (ID) initWithClass:(Class)element;@end 并实现:
@implementation MonomorphicArray- (ID) initWithClass:(Class)element andCapacity:(NSUInteger)numItems{ elementClass = element; realArray = [NSMutableArray arrayWithCapacity:numItems]; return self;}- (ID) initWithClass:(Class)element{ elementClass = element; realArray = [NSMutableArray new]; return self;}// overrIDe primitive NSMutableArray methods and enforce monomorphism- (voID) insertObject:(ID)anObject atIndex:(NSUInteger)index{ if ([anObject isKindOfClass:elementClass]) // allows subclasses,use isMemeberOfClass for exact match { [realArray insertObject:anObject atIndex:index]; } else { NSException* myException = [NSException exceptionWithname:@"InvalIDAddobject" reason:@"Added object has wrong type" userInfo:nil]; @throw myException; }}- (voID) removeObjectAtIndex:(NSUInteger)index{ [realArray removeObjectAtIndex:index];}// overrIDe primitive NSArray methods- (NSUInteger) count{ return [realArray count];}- (ID) objectAtIndex:(NSUInteger)index{ return [realArray objectAtIndex:index];}// block all the other init's (some Could be supported)static ID NotSupported(){ NSException* myException = [NSException exceptionWithname:@"InvalIDInitializer" reason:@"Only initWithClass: and initWithClass:andCapacity: supported" userInfo:nil]; @throw myException;}- (ID)initWithArray:(NSArray *)anArray { return NotSupported(); }- (ID)initWithArray:(NSArray *)array copyItems:(BOol)flag { return NotSupported(); }- (ID)initWithContentsOffile:(Nsstring *)aPath { return NotSupported(); }- (ID)initWithContentsOfURL:(NSURL *)aURL { return NotSupported(); }- (ID)initWithObjects:(ID)firstObj,... { return NotSupported(); }- (ID)initWithObjects:(const ID *)objects count:(NSUInteger)count { return NotSupported(); }@end 用于:
MonomorphicArray *monoString = [[MonomorphicArray alloc] initWithClass:[Nsstring class] andCapacity:3];[monoString addobject:@"A string"];[monoString addobject:[NSNumber numberWithInt:42]]; // will throw[monoString addobject:@"Another string"];总结
以上是内存溢出为你收集整理的objective-c – NSMutableArray – 强制数组仅保存特定对象类型全部内容,希望文章能够帮你解决objective-c – NSMutableArray – 强制数组仅保存特定对象类型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)