2011-08-21 61 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 –