NSArray 및 NSMutableArray 상세 정보
2777 단어 NSArray
NSArray 사용법:
첫째, 초기화
NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
NSArray *secondArray=[NSArray arrayWithArray:firstArray];
둘째, 원소 개수와 접근 얻기
NSLog(@"the number is %ld",[secondArray count]);
NSLog(@"the value is %@",[secondArray objectAtIndex:2]);
셋째, 데이터 요소 추가
NSArray *thirdArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
넷째, 수조가 문자열로 전환
NSString *str=[firstArray componentsJoinedByString:@".."];
NSLog(@"the number is %@",str);
다섯째, 문자열 포함 여부 판단
NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
NSLog(@"has value %d",[firstArray containsObject:@"two"]);
NSLog(@"has value %ld",[firstArray indexOfObject:@"two"]);
NSLog(@"the last object is %@",[firstArray lastObject]);
NSMutalbeArray 사용법 -
첫째, 기본적인 첨삭과 수정
NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"richard"];
[mutableArr insertObject:@"yang" atIndex:1];
NSLog(@"%@",mutableArr);
[mutableArr removeObject:@"hello"];
[mutableArr removeObjectAtIndex:0];
[mutableArr removeLastObject];
NSLog(@"%@",mutableArr);
둘째, 교체 작업
[mutableArr replaceObjectAtIndex:0 withObject:@"kaixin"];
두루
NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
for(int index=0;index<[mutableArr count];index++)
{
NSLog(@"the val is %@",[mutableArr objectAtIndex:index]);
}
for(NSString *str in mutableArr)
{
NSLog(@"%@",str);
}
for (id str in mutableArr) {
NSLog(@"%@",str);
}