You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Objective-C报错:NSObject无objectAtIndex:方法,数组遍历问题求助

问题分析与解决方案

错误原因拆解

第一个错误:No visible @interface for 'NSObject' declares the selector 'objectAtIndex:'

循环变量j被声明为NSObject*类型,但NSObject类并没有objectAtIndex:方法——这个方法是NSArray(以及其子类NSMutableArray)的成员方法。编译器无法识别NSObject实例调用该方法,因此报错。

第二个错误:Assigning to 'CGFloat' (aka 'double') from incompatible type 'id'

[j objectAtIndex:1]返回的是id类型的对象(在你的代码里是NSNumber,比如@90),不能直接赋值给基本数据类型CGFloat,必须将NSNumber对象转换为对应的数值类型。

隐藏的严重问题:数组初始化顺序错误

你先执行了[_elements addObject:_arrins1];,之后才给_arrins1赋值[NSMutableArray array];,这会导致_elements中添加的是未初始化的nil对象,后续遍历访问时会直接崩溃。必须调整初始化顺序,先初始化子数组,再添加到_elements中。

修正后的完整代码

1. 修正数组初始化顺序

// 先初始化子数组
_arrins1 = [NSMutableArray array];
[_arrins1 addObject:ins1.title = @"test1"];
[_arrins1 addObject:ins1.size = @90];
[_arrins1 addObject:ins1.text = @"example"];
[_arrins1 addObject:ins1.ids = @0];

_arrins2 = [NSMutableArray array];
[_arrins2 addObject:ins2.title = @"test2"];
[_arrins2 addObject:ins2.size = @40];
[_arrins2 addObject:ins2.text = @"example"];
[_arrins2 addObject:ins2.ids = @1];

_arrins3 = [NSMutableArray array];
[_arrins3 addObject:ins3.title = @"test3"];
[_arrins3 addObject:ins3.size = @20];
[_arrins3 addObject:ins3.text = @"example"];
[_arrins3 addObject:ins3.ids = @2];

// 再将子数组添加到_elements
_elements = [NSMutableArray array];
[_elements addObject:_arrins1 ];
[_elements addObject:_arrins2 ];
[_elements addObject:_arrins3 ];

2. 修正heightForRowAtIndexPath:方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 0;
    // 将循环变量类型改为NSMutableArray*,明确是数组类型
    for (NSMutableArray *j in _elements) {
        // 取出NSNumber对象,转换为CGFloat
        NSNumber *sizeNum = [j objectAtIndex:1];
        height = sizeNum.floatValue;
        // 注意:这里循环会覆盖height的值,最终height是最后一个子数组的size值
        // 如果需要根据indexPath获取对应行的高度,应该直接取对应索引的元素,而不是遍历全部
        // 比如:
        // NSMutableArray *currentArr = _elements[indexPath.row];
        // height = [currentArr[1] floatValue];
    }

    return height;
}

额外优化建议

不要用数组存储模型属性,建议直接创建自定义模型类(比如InsModel),包含title、size、text、ids属性,然后将模型对象添加到_elements数组中,这样代码可读性和可维护性更强:

// 自定义模型示例
@interface InsModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CGFloat size;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) NSInteger ids;
@end

// 使用模型
InsModel *ins1 = [[InsModel alloc] init];
ins1.title = @"test1";
ins1.size = 90;
ins1.text = @"example";
ins1.ids = 0;

_elements = [NSMutableArray arrayWithObjects:ins1, ins2, ins3, nil];

// heightForRowAtIndexPath中直接取模型属性
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    InsModel *model = _elements[indexPath.row];
    return model.size;
}

内容的提问来源于stack exchange,提问作者Six

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 00:50:16