objective-c对NSArray的学习

2011-03-23

艰难的开始,学习IOS 开发。 NSARRAY简单的使用

定义数组,遍历数组:

    NSArray *array;
    array = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four",nil];
    
    int i;
    for(i = 0; i < [array count]; i++)
    {
        NSLog(@"index %d has %@.",i,[array objectAtIndex:i]);
    }

切分字符串:

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSString *string = @"oop:ack:bork:greeble:ponies";
    
    NSArray *array = [[[NSArray array] init] autorelease];
//    array = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four",nil];
    
    array = [string componentsSeparatedByString:@":"];
    
    int i;
    for(i = 0; i < [array count]; i++)
    {
        NSLog(@"index %d has %@.",i,[array objectAtIndex:i]);
    }

合并数组:

NSArray  *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];

可变数组:

    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    
    array = [NSMutableArray arrayWithCapacity: 17];
    
    int i;
    
    for(i = 0; i < 4; i++)
    {
        [array addObject:@"aaa"];
        NSLog(@"hello %d  %@",i, [array objectAtIndex:i]);
    }