关于OC基础之数组和字符串的操作;
这里我也是初学一下,就是经常用到的方法,我实地测试了下,给大家分享下哦
#import <Foundation/Foundation.h>
intmain (int argc, constchar *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *p= [[NSMutableArray alloc] initWithObjects:@"3",@"5",@"4",@"1",nil];
NSMutableArray *pp = [[NSMutableArray alloc] initWithObjects:@"hello",@"hello5",@"hello4",@"heloo1",nil];
int a =[[p objectAtIndex:1] intValue];
NSLog(@"a = %d,leg = %d",a,[pcount]);
int len =[p count];
NSLog(@"len = %d",len);
int i =0;
int j =0;
//下面是OC的冒泡排序
for (i =0; i<len; i++)
{
for (j=i+1; j<len; j++)
{
inta = [[p objectAtIndex:i] intValue];
intb = [[p objectAtIndex:j] intValue];
if(a > b)
{
[p replaceObjectAtIndex:iwithObject:[NSString stringWithFormat:@"%d",b]];
[p replaceObjectAtIndex:jwithObject:[NSString stringWithFormat:@"%d",a]];
}
}
}
int bb =0;
NSMutableArray *mArr = [NSMutableArray array];
for(bb =0; bb<4; bb++)
{
[mArr addObject:[pobjectAtIndex:bb]];
NSLog(@"%@",[pobjectAtIndex:bb]);
}
int ss =0;
for (ss=0;ss<4;ss++) {
NSLog(@"2遍历array:%zd-->%@",ss,[mArrobjectAtIndex:ss]);
NSString *stddss =[mArr objectAtIndex:ss];
NSMutableString *str11 = [[NSMutableString alloc] initWithString:nil];
[str11 appendString:stddss];
NSLog(@"%@",str11);
}
NSLog(@"---------------");
int aa =0;
NSMutableArray *temp = [NSMutableArray array];
NSString *str =@"";
NSMutableString *str1 = [[NSMutableString alloc] initWithString:nil];
for (aa =0; aa<4; aa++)
{
NSLog(@"helliiii");
NSString *stdd = [pobjectAtIndex:aa];
[str1 appendString:stdd];
NSLog(@"%@",[ppobjectAtIndex:aa]);
}
NSLog(@"%@",str1);
[pool drain];
return 0;
}
对字符串和数组的操作还有很多方法,大家可以尝试下,我觉的上面的在做项目的话是比较常用的,就写一下,