2011-08-21 60 views
-2

我在学习Objective C。我被给了一个代码来解决我已经修复但不完全确定发生了什么。有些人可以解释下面的代码在做什么。这是什么目标C代码在做什么?

//in header file there is this line 
@property (retain) NSMutableArray *anArray; 

    // In implementation file in a method 
    self.anArray = [NSMutableArray array]; 
    //This assigns a large value to index . What is this value. Does NSIteger needs initialization I think default is 0 
    NSInteger _nextIndex = (NSInteger)[self.anArray]; 

回答

3

你的类型转换指针指向self.anArrayNSInteger。换句话说,_nextIndex包含存储self.anArray的地址。

7

该代码无效,将无法解析[self.anArray]

方括号用于调用方法,但不存在调用的方法。看起来你想要做的是NSInteger _nextIndex = (NSInteger)[self.anArray count];它将分配给_nextIndex数组中元素的数量,这是下一个索引的位置。

数组是列表项,从0开始。因此,如果列表中没有任何内容,count方法将返回0,这是第一个位置。如果列表中有100个项目,则它们将使用从099的索引,然后count将返回100,下一个项目位置将是100

+0

谢谢好解释投票up –