我有这样一段代码,我想在消息符号等效内:消息传递符号与圆点记号和值一个NSArray
cell.textLabel.text = self.dataArray[indexPath.row];
我能做的最好的是:
[[cell textLabel] setText: [ what goes here? ]];
我有这样一段代码,我想在消息符号等效内:消息传递符号与圆点记号和值一个NSArray
cell.textLabel.text = self.dataArray[indexPath.row];
我能做的最好的是:
[[cell textLabel] setText: [ what goes here? ]];
完全展开的版本是这样的:
[[cell textLabel] setText:[[self dataArray] objectAtIndex:[indexPath row]]];
然而,这是一个和你之间绝对没有区别最初张贴。它们与编译器相同。
这两种语法这里所使用的有:
这是-foo
或-setFoo:
方法:
cell.textLabel -> [cell textLabel]
cell.textLabel.text = ... [[cell textLabel] setText:...]
这是-objectAtIndex:
和-setObject:atIndex:
种方法(如果它是一个NSArray
),以及objectForKey:
和-setObject:forKey:
方法(如果它是一个NSDictionary
):调用
dataArray[indexPath.row] -> [dataArray objectAtIndex:indexPath.row]
实际的方法是objectAtIndexedSubscript:
......,但他们类似于更常见objectAtIndex:
方法。有关此语法的更多信息,请拨打check out the Clang documentation。
它不是“下标”,它是“消息”符号。 –
@HotLicks使用'[]'语法来索引数组或字典称为下标。但是由于这些都是调用方法,所以它是“发送消息”。 –
非常完整的答案,也有一点深入的解释其他东西的新手,请添加[[单元textLabel] setText:[[self dataArray] objectAtIndex:[indexPath row]]];在顶部,所以我们可以在消息中注明indexPath.row部分。我会将其标记为答案。 – unmircea
[[cell textLabel] setText: [self dataArray][indexPath row]];
或:
[[cell textLabel] setText: [_dataArray[indexPath row]];
我假定对于self.dataArray所述的ivar是_dataArray(默认);
,或者您可以拨打
[[cell textLabel] setText: [[self dataArray] objectAtIndex:[indexPath row]]];
出了什么问题'cell.textLabel.text = self.dataArray [indexPath.row]'?或者,如果失败了,'[[cell textLabel] setText:self.dataArray [indexPath.row]];'? –
我无法读取点符号。当我开始编程时,我学习了Smalltalk。我发现它如此清晰和合乎逻辑。如果我看到点符号,它会让我认为在引擎盖下发生了一些我不明白的事情,这让我感到不安。所以,现在我正在学习Objective-C,当我在各处看到消息符号时,我都会感到更新,我绝对喜欢括号(它们使得执行顺序非常清晰)。我不是在开玩笑! – unmircea
给他自己的。正如我在下面所说的那样,这对新手来说是令人困惑的,因为完全相同的功能通常可以通过多种方式来表达,而在其他情况下,您*不能使用点符号。 Objective-C不是特别“统一”的语言。 –