简体   繁体   English

NSArray对象和Casting

[英]NSArray of objects and Casting

I have a class A with a property NSString *name . 我有一个带有属性NSString *name A类。 If have an NSArray and add many A objects into this array, is casting necessary each time I retrieve an object? 如果有一个NSArray并在这个数组中添加了许多A对象,那么每次检索对象时都需要进行强制转换吗? ie

NSString* n = (NSString*)[arr objectAtIndex:1];

Or is there a another way to do it kind of like in java where you have ArrayList<A> arr ? 或者是否有另一种方法可以在java中使用ArrayList<A> arr

NSArray do not store information about the types of objects contained in them. NSArray不存储有关其中包含的对象类型的信息。 If you know for sure the types of objects in your array, you can perform a cast, either implicitly or explicitly: 如果您确定数组中的对象类型,则可以隐式或显式执行强制转换:

NSString *n = [arr objectAtIndex:1];  // implicit type conversion (coercion) from id to NSString*
NSString *n = (NSString *)[arr objectAtIndex:1];  // explicit cast

There's no difference in runtime cost between implicit and explicit casts, it's just a matter of style. 隐式和显式转换之间的运行时成本没有区别,只是风格问题。 If you get the type wrong, then what is very likely going to happen is that you'll get the dreaded unrecognized selector sent to instance 0x12345678 exception. 如果你输入的类型不对,那么很可能会发生的事情是你会将可怕的unrecognized selector sent to instance 0x12345678异常。

If you have a heterogeneous array of different types of objects, you need to use the isKindOfClass: method to test the class of the object: 如果您有不同类型对象的异构数组,则需要使用isKindOfClass:方法来测试对象的类:

id obj = [arr objectAtIndex:1];
if ([obj isKindOfClass:[NSString class] ])
{
    // It's an NSString, do something with it...
    NSString *str = obj;
    ...
}

NSArray 's objectAtIndex: returns a variable of type id . NSArrayobjectAtIndex:返回id类型的变量。 This would be roughly equivalent to returning an Object in Java—"we can't guarantee much about this variable's type." 这将是大致相当于返回一个Object在Java的“我们不能保证许多关于这个变量的类型。” In Objective-C however, you can send messages to id variables without the compiler complaining. 但是,在Objective-C中,您可以在没有编译器抱怨的情况下向id变量发送消息。 So, because you know that the array only contains A instances, you're good to send it the name message. 因此,因为知道该数组只包含A实例,所以您最好向其发送name消息。 For example, the following will compile without warning: 例如,以下内容将在没有警告的情况下编译:

NSString *name = [[array objectAtIndex:0] name];

However, if you want to use dot notation (eg [array objectAtIndex:0].name ), you'll need to cast the id to an A * as shown here: 但是,如果要使用点表示法(例如[array objectAtIndex:0].name ),则需要将id转换为A * ,如下所示:

NSString *name = ((A *)[array objectAtIndex:0]).name;

This is the way to do it. 这是做到这一点的方法。 You can actually do the above operation without casting the return since you're reading it into a NSString anyway. 实际上,您可以在不转换返回的情况下执行上述操作,因为无论如何您都要将其读入NSString。

this is the way: 这是方式:

NSMutableArray  *objectsArray = [[NSMutableArray  alloc] init];

A *a= [[A alloc] init];

[objectsArray addObject:a];

[a release];

You don't need to worry about the proprety type 您不必担心属性类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM