类中的NSMutableArray类型 [英] Type of class in a NSMutableArray

查看:138
本文介绍了类中的NSMutableArray类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/649483/is-there-any-way-to-enforce-typing-on-nsarray-nsmutablearray-etc\">Is有什么办法可以强制执行的NSArray,NSMutableArray里等打字?

我是Java程序员,我开始上的OBJ-C,Java中我可以创建具有确定类型类的可变数组,就像如下:

I'm Java programmer and I'm starting on Obj-C, in java i can create a mutable array with determined type of class, like as follow:

ArrayList<MyClass> list;

在对象 - 我知道,但NSMutableArray的我不知道,并没有发现如何确定的类的类型进去。

in Obj-c I know the NSMutableArray but i don't know and not found how to determinate the type of class into it.

与它提出这样或其他类的方式,而不其中的NSMutableArray能做到这一点?

Have a way of make this with it or other class without NSMutableArray which can do this?

非常感谢。

推荐答案

没有,可可/ Objective-C中没有提供类型集合这样。集合中的所有对象都必须从 NSObject的继承(这基本上是一切,除了原语和结构),但除此之外,它是由你来了解/管理什么是怎么回事的阵列。由泛型类型在其​​接口psented一个的NSMutableArray 重新$ P $对象 ID

No, Cocoa/Objective-C doesn't offer typed collections like this. All objects in the collection must inherit from NSObject (which is basically everything besides primitives and structs), but beyond that, it's up to you to understand/manage what is going on in the array. Objects in an NSMutableArray are represented in its interface by the generic type id.

从设计的角度来看,在可可收藏一般都含有均匀类型的对象。阵列的名称通常用来指示什么里面(正如在Java中),例如 bookTitlesArray 或只是 bookTitles (即字符串)。此外,从抽象的角度来看,有时轻便类用于包装原始的的NSMutableArray 执行类型的接口检查。正如,一个名为新类 BookTitleList 其提供的添加,查找的一个子集,删除方法和例如通过后他们通过一个内部数组验证。但情况因人而异根据您的需要。

From a design standpoint, collections in Cocoa typically do contain homogeneously-typed objects. The name of the array is often used to indicate what's inside it (just as in Java), e.g. bookTitlesArray or just bookTitles (i.e. strings). Additionally, from an abstraction standpoint, sometimes lightweight classes are used to "wrap" a raw NSMutableArray to enforce type checking at the interface. As in, a new class called BookTitleList which offered a subset of the add, lookup, remove methods and passed them through to an internal array after e.g. validation. But YMMV depending on your needs.

这篇关于类中的NSMutableArray类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆