iOS开发这些最基础的东西你确定你都知道吗

一、字符串

1、不可变字符串的创建(NSSString)

//最简单的创建方式
NSString * str1 = @"I love iOS";

//以str1创建str2
NSString * str2 = [[NSString alloc] initWithString:str1];

//以C的字符串创建OC字符串
char * cStr = "I love iOS ha";
NSString * str3 = [[NSString alloc] initWithCString:cStr encoding:NSUTF8StringEncoding];    
NSString * str4 = [[NSString alloc] initWithUTF8String:cStr];

//format格式创建字符串(拼接字符串)
int a = 1;
float b = 3.14;
NSString * str5 = @"I like iOS";
NSString * str6 = [[NSString alloc] initWithFormat:@"%d%f%@",a,b,str6];

//从文件当中读取字符串
NSString * str7 = [[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];

2、字符串的比较

//*****************C字符串的比较
char *p = "jos";
char *q = "ios";

int ret = strcmp(p, q);//ret = p - q
if (ret > 0) {
    NSLog(@"p > q");
} else if (ret < 0) {
    NSLog(@"p < q");
} else {
    NSLog(@"p == q");
}

//*****************比较相等与否
NSString * str1 = @"I love ios";
NSString * str2 = @"I love ios";
if ([str1 isEqualToString:str2]) {
    NSLog(@"相等");
}else{
    NSLog(@"不相等");
}

//*****************OC字符串比较大小
NSString * str3 = @"how do you do";
NSString * str4 = @"how do you do";
NSComparisonResult result = [str3 compare:str4];
if (result == NSOrderedSame) {
    NSLog(@"str3 == str4");
}else if (result == NSOrderedAscending){
    NSLog(@"str3 < str4");
}else if (result == NSOrderedDescending){
    NSLog(@"str3 > str4");
}

//*****************不区分大小写的比较
NSString * str5 = @"how are you";
NSString * str6 = @"HOW ARE YOU";
NSComparisonResult result1 = [str5 caseInsensitiveCompare:str6];
if (result1 == NSOrderedSame) {
    NSLog(@"str5 == str6");
}else if (result1 == NSOrderedAscending){
    NSLog(@"str5 < str6");
}else if (result1 == NSOrderedDescending){
    NSLog(@"str5 > str6");
}

3、字符串的处理

NSString * str = @"I love iOS";

//小写转大写
NSString * str1 = [str uppercaseString];

//大写转小写
NSString * str2 = [str lowercaseString];

//首字母大写
NSString * str3 = [str capitalizedString];

//字符串范围
NSRange range = [str rangeOfString:@"love"];//空格也要算在内
//位置range.location == 2,长度range.length == 4

//提取字符串(第一个字符的下标是0)
NSString * str4 = [str substringFromIndex:3];//截取从第3个字符开始到结尾(即:ove iOS)
NSString * str5 = [str substringToIndex:5];//截取前5个字符(即:I lov)
NSString * str6 = [str substringWithRange:NSMakeRange(2, 4)];//截取从第2个字符起往后数4个字符(即:love)

//判断字符串首尾
if ([str hasPrefix:@"I love"]) {
    NSLog(@"是以I love开头");
}
if ([str hasSuffix:@"iOS"]) {
    NSLog(@"是以iOS结尾");
}

4、可变字符串的创建(NSMutableString)

//方式1
NSString * str = @"I love iOS";
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:str];
//方式2
int i = 3;
NSMutableString * mstr2 = [[NSMutableString alloc] initWithFormat:@"i = %d",i];
//方式3
NSMutableString * mstr3 = [[NSMutableString alloc] initWithUTF8String:"haha"];

5、可变字符串的处理

//字符串的追加1(追加到尾部)
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:@"I love iOS"];
[mStr1 appendString:@" very much"];  
//结果:mStr1 = @"I love iOS very much"

//字符串的追加2(追加到尾部)
NSMutableString * mStr2 = [[NSMutableString alloc] initWithString:@"I love iOS"];
int i = 2;
[mStr2 appendFormat:@" %d", i];  
//结果:mStr2 = @"I love iOS 2�"

//字符串的插入
NSMutableString * mStr3 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr3 insertString:@"yyy" atIndex:3];
//结果:mStr3 = @"abcyyydefg"

//字符串的删除
NSMutableString * mStr4 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr4 deleteCharactersInRange:NSMakeRange(3, 2)];
//结果:mStr4 = @"abcfg"

//字符串的替换
NSMutableString * mStr5 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr5 replaceCharactersInRange:NSMakeRange(0, 2) withString:@"1234"]
//结果:mStr5 = @"1234cdefg"

二、数组

1、不可变数组

#创建数组#
//创建方式1
NSArray *array1 = [[NSArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//创建方式2
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
//创建方式3
NSArray *array3 = @[@"xixi",@"haha",@"hehe"];
//创建方式4
NSArray *array4 = [NSArray arrayWithObjects:@"how",@"are",@"you", nil];
//创建方式5
NSArray *array5 = [NSArray arrayWithArray:array4];

#查找元素位置#
NSArray *array6 = @[@"how", @"are", @"you"];
NSUInteger index = [array6 indexOfObject:@"are"];
//结果:index = 1

#字符串分割到数组里#
NSString *str = @"You@are@the@best";
NSArray *array7 = [str componentsSeparatedByString:@"@"];
//结果:array7 = @[@"You",@"are",@"the",@"best"];

#数组连接成字符串#
NSArray *array8 = @[@"I", @"love", @"you"];
NSString *str = [array8 componentsJoinedByString:@""];
//结果:str = @"Iloveyou"

2、可变数组

#创建数组#
//创建方式1
NSMutableArray * mArray1 = [[NSMutableArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//创建方式2
NSMutableArray * mArray2 = [[NSMutableArray alloc] initWithArray:mar1];
//创建方式3
NSMutableArray * mArray3 = [NSMutableArray arrayWithObjects:@"how",@"are",@"you", nil];
//创建方式4
NSMutableArray * mArray4 = [NSMutableArray arrayWithArray:mar3];

#增删替换#
NSArray * array = @[@"how",@"do",@"you"];
NSMutableArray * mArray5 = [[NSMutableArray alloc] initWithArray:array];

//添加
[mArray5 addObject:@"do"];
//结果:mArray5 = @[@"how", @"do", @"you", @"do"]

//插入
[mArray5 insertObject:@"GG" atIndex:0];
//结果:mArray5 = @[@"GG", @"how", @"do", @"you", @"do"]

//删除
[mArray5 removeObjectAtIndex:0];
//结果:mArray5 = @[@"how", @"do", @"you", @"do"]

[mArray5 removeLastObject];
//结果:mArray5 = @[@"how", @"do", @"you"]

//替换
[mArray5 replaceObjectAtIndex:0 withObject:@"what"];
//结果:mArray5 = @[@"what", @"do", @"you"]

//交换位置
[mArray5 exchangeObjectAtIndex:0 withObjectAtIndex:1]
//结果:mArray5 = @[@"do", @"what", @"you"]

三、字典

1、不可变字典

#创建字典#
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3",  nil];
#键值对的个数#
NSInteger count = [dict count];
#通过键查找值#
NSString *str = [dict objectForKey:@"two”];
NSString *str = dict[@"two”];
#遍历#
for (NSString *str in dict) {
        NSLog(@"%@", str);      //遍历的是字典dict中的键
        NSLog(@"%@", dict[str]);//遍历的是字典dict中的值
}

2、可变字典

#创建可变字典#
NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
#增加键值对#
[mdict setObject:@"four" forKey:@"4"];
#删除键值对#
[mdict5 removeObjectForKey:@“4”];//删除单个键
[mdict5 removeAllObjects];//全部删除
#字典替换#
NSDictionary * dict = @{@“3”:@“xixi", @"2":@"gogo"};
[mdict setDictionary:dict];//把字典mdict全部改为字典dict

你可能感兴趣的:(iOS开发这些最基础的东西你确定你都知道吗)