Whats is this Objective C code doing?
I am learning Objective C . I was given a code to fix that I have fixed but not entirely sure what was going on though. Can some please explain what is following code doing.
//in header file there is this line
@property (retain) NSMutableArray *anArray;
// 开发者_StackOverflowIn 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];
That code is invalid, it will fail to parse [self.anArray]
.
The square brackets are used to call methods, but there is no method you are calling. It seems like what you wan to do is NSInteger _nextIndex = (NSInteger)[self.anArray count];
which will assign to _nextIndex
the number of elements in the array, which is the position of the next index.
An array is a list of items, starting by 0. So, if the list doesn't have anything the count
method will return 0
, which is the first position. If the list has 100 items, they will use indexes from 0
to 99
, then the count
will return 100
and the next item position will be 100
.
You're typecasting the pointer pointing to self.anArray
to an NSInteger
. In other words, _nextIndex
contains the address at which self.anArray
is stored.
精彩评论