NSArray计数不起作用

And*_*rew 0 iphone xcode uitableview tableview ios

所以我有一个UITableView,我用table中的数据填充tableview .plist.这对我来说一直很好,直到今天,当我试图做同样的事情,我无法得到numberOfRowsInSection,方法工作.这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if...

     else if (segmentOptions == exchange){
         NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];
         return [array count];
     }   

   //Else
    return contentArray.count;
}
Run Code Online (Sandbox Code Playgroud)

因此,在该代码中,每次运行代码时,代码都会崩溃.但是,我使用基本相同的代码来设置tableview文本,并且工作正常:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
     }

     if...

     else if (segmentOptions == exchange) {
           NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:indexPath.section]]];
           cell.textLabel.text =  [array objectAtIndex:indexPath.row];
     }

     return cell;
}
Run Code Online (Sandbox Code Playgroud)

并且该代码工作正常,这就是为什么我很困惑.当我设置返回到特定数字的行数,并运行其余代码时,一切正常.所以我真的迷路了.提前发酵

更新 我用正确的count符号尝试了return [array count];,我仍然得到相同的崩溃和相同的控制台输出.

Mar*_*rra 5

首先,count不是属性,因此您不应该使用点语法来访问它.

我建议您更改代码,以便不像属性那样访问count方法.

第二,

测试你的数组是否为nil,它甚至是一个数组.

第三,

发布实际的完整堆栈跟踪.

  • 这不是"相当普遍......"; 这是错误的.这违背了点语法的意图.消除代码中的每个问题有助于缩小问题范围.Dot语法不应用于非属性.就这么简单. (11认同)
  • 从概念上讲,`-count`是`NSArray`的只读属性,所以你可以说可以使用点语法.</ devil的拥护者> (5认同)
  • 使用属性表示法访问`count`是相当普遍的.它有效,编译器永远不会抱怨. (3认同)
  • 我不确定马库斯关于点符号的评论在这里是否相关; 它肯定不是"错误的".点符号和属性是正交概念,虽然它们通常是齐头并进的,但我认为没有理由不将它们用于早于`@ property`的旧API中的概念属性.底层实现并不重要.如果它是一个没有副作用的概念属性,那么如果这是你喜欢的风格(因为它是我的),请随意使用点符号. (3认同)
  • @NickForge不,这不是财产; 故事结局.如果人们想要像这样使用它们,他们应该提交一个雷达计数转为财产. (2认同)