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

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

问题描述

可能的重复:
有没有办法强制在 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;

在 Obj-c 中,我知道 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?

非常感谢.

推荐答案

不,Cocoa/Objective-C 不提供这样的类型化集合.集合中的所有对象都必须从 NSObject 继承(这基本上是除了基元和结构之外的所有内容),但除此之外,理解/管理数组中发生的事情取决于您.NSMutableArray 中的对象在其接口中由泛型类型 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.

从设计的角度来看,Cocoa 中的集合通常确实包含同构类型的对象.数组的名称通常用于指示其中的内容(就像在 Java 中一样),例如bookTitlesArray 或只是 bookTitles(即字符串).此外,从抽象的角度来看,有时使用轻量级类来包装"原始 NSMutableArray 以在接口上强制执行类型检查.例如,一个名为 BookTitleList 的新类提供了添加、查找、删除方法的子集,并将它们传递给内部数组,例如验证.但是 YMMV 取决于您的需要.

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天全站免登陆