13.Arrays

프로그래밍/iPhone 2010. 6. 14. 19:52


출처 : www.cocoalab.com

배열은 NSArray, NSMutableArray 두개의 클래스로 제공된다.

[NSMutableArray array];
위 코드는 배열을 생성한후 리턴해준다. 위 코드는 기존의 코드와는 다르게 Class 에게 message 를 보내고 있다.
Object-C 에서는 instance 에게 메세지를 보내는 것 뿐만 아니라 Class 에게 메세지를 보내는 것도 가능하다.
method 선언시 "+" 표시가 붙은 메소드는 class 에서 호출할 수 있으며
"-" 표시가 붙은 메소드는 instance 에서 호출할 수 있다.

NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int count = [myArray count];
NSLog(@"There are %d elements in my array", count);

int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
   NSString *element = [myArray objectAtIndex:i];
   NSLog(@"The element at index %d in the array is: %@", i, element);
}
NSString *first = [myArray replaceObjectAtIndex:1 withObject:@"Hello"];
NSLog(@"first element : %@", first);

==>
There are 3 elements in my array
The element at index 0 in the array is: first string
The element at index 1 in the array is: second string
The element at index 2 in the array is: third string
first element : Hello

'프로그래밍 > iPhone' 카테고리의 다른 글

Objetive-C  (0) 2010.06.15
12. Strings  (0) 2010.06.14
: