gpt4 book ai didi

objective-c - 遍历NSMutableArray

转载 作者:行者123 更新时间:2023-12-01 01:30:48 24 4
gpt4 key购买 nike

我有一个NSMutableArray,可以加载不同的对象(类)。

现在,我需要遍历数组并进入类进行进一步的操作。

我正在尝试这种方法...

for (id obj in allPointsArray)   
{
///// this is where i need to bring the obj into a class to work with
NSInteger loc_x = obj.x_coord;
NSInteger loc_y = obj.y_coord;
}


但我无法真正将类从数组中取出并放入可用的对象中。

x_coord和y_coord在数组中存储的所有对象之间是公用的。

谢谢大家的帮助

最佳答案

如果数组中的对象属于不同的类,您是否尝试做不同的事情?您可以执行以下操作:

for (id obj in myArray) {
// Generic things that you do to objects of *any* class go here.

if ([obj isKindOfClass:[NSString class]]) {
// NSString-specific code.
} else if ([obj isKindOfClass:[NSNumber class]]) {
// NSNumber-specific code.
}
}

关于objective-c - 遍历NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com