200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Object-C 语法 字符串 数组 字典 和常用函数 学习笔记

Object-C 语法 字符串 数组 字典 和常用函数 学习笔记

时间:2023-06-26 18:51:35

相关推荐

Object-C 语法 字符串 数组 字典 和常用函数 学习笔记

字符串

//取子字符串NSString *str1=@"今天的猪肉真贵,200块一斤";NSString *sub1=[str1 substringFromIndex:4];NSLog(@"%@",sub1);//肉真贵,200块一斤//区间NSRange ran=NSMakeRange(3, 2);NSString *sub2=[str1 substringWithRange:ran];NSLog(@"%@",sub2);//猪肉//字符串拼接NSString *str3=@"进能越塔送人头";NSString *str4=@"退可挂机骂队友";NSString *newStr=[str3 stringByAppendingString:str4];NSLog(@"拼接的结果是%@",newStr);//字符串替换NSString *str5=@"iPhone6";NSString *newstr1=[str5 stringByReplacingOccurrencesOfString:@"6" withString:@"7"];NSLog(@"%@",newstr1);//区间替换NSString *newstr2=[str5 stringByReplacingCharactersInRange:NSMakeRange(1, 2) withString:@"66"];NSLog(@"%@",newstr2);//大小写NSString *str6=@"diaozhatian";NSString *newstr3=[str6 uppercaseString];NSLog(@"%@",newstr3);newstr3=[newstr3 lowercaseString];NSLog(@"%@",newstr3);//判断开头和结尾NSString *webStr=@"";//判断开头BOOL r1=[webStr hasPrefix:@"www"];NSLog(@"%d",r1);//1//判断结尾BOOL r2=[webStr hasSuffix:@"com"];NSLog(@"%d",r2);//1//是否包含NSString *str7=@"我赵日天服了!";BOOL r3=[str7 containsString:@"日"];if (r3) {NSLog(@"请文明聊天.");NSString *new3=[str7 stringByReplacingOccurrencesOfString:@"日" withString:@"*"];NSLog(@"%@",new3);}else {NSLog(@"真乖!");}//可变字符串 相当于 C#的 StringBuilderNSMutableString *mStr=[NSMutableString string];//初始化为空// NSMutableString *mStr1=[[NSMutableString alloc] initWithCapacity:1];[mStr appendString:@"1111"];[mStr appendString:@"2222"];[mStr insertString:@"666" atIndex:3];NSLog(@"%@",mStr);[mStr deleteCharactersInRange:NSMakeRange(3, 3)];NSLog(@"%@",mStr);

数组

//不可变数组NSArray *arr1=[NSArray arrayWithObjects:@"孙悟空",@"八戒",@"和尚",@"小白龙", nil];//字面量NSArray *arr2=@[@"孙悟空",@"八戒",@"和尚",@"小白龙"];//数组长度NSInteger count=[arr1 count];NSLog(@"%ld",count);NSLog(@"%ld",[arr2 count]);//数组中获取元素NSString *str=[arr2 objectAtIndex:2];NSLog(@"%@",str);//第一个 最后一个str=[arr2 firstObject];NSLog(@"%@",str);str=[arr2 lastObject];NSLog(@"%@",str);NSInteger indexof=[arr2 indexOfObject:@"小白龙"];NSLog(@"%ld",indexof);//是否包含BOOL r1=[arr1 containsObject:@"小白龙"];NSLog(@"%d",r1);//1//字符串分割成数组NSString *str2=@"我勒,个,去";NSArray *arr3=[str2 componentsSeparatedByString:@","];NSLog(@"%@",arr3);//数组拼接成字符串NSArray *arr4=@[@"123",@"456",@"789",@"147"];// NSArray *arr4=[NSArray arrayWithObjects: @"123",@"456",@"789",@"147",nil];NSLog(@"%@",[arr4 componentsJoinedByString:@""]);//可变数组NSMutableArray *mArr=[NSMutableArray array];[mArr insertObject:@"张三" atIndex:0];[mArr addObject:@"林黛玉"];[mArr addObject:@"李四"];for (int i=0; i<[mArr count]; i++) {NSLog(@"%@",[mArr objectAtIndex:i]);}// [mArr removeObjectAtIndex:0];// [mArr removeObject:@"林黛玉"];// for (int i=0; i<[mArr count]; i++) {// NSLog(@"%@",[mArr objectAtIndex:i]);// }//数组换位[mArr exchangeObjectAtIndex:0 withObjectAtIndex:1];//替换[mArr replaceObjectAtIndex:0 withObject:@"王五"];for (int i=0; i<[mArr count]; i++) {NSLog(@"%@",[mArr objectAtIndex:i]);}

字典

int main(int argc, const char * argv[]) {// insert code here...//不可变字典NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"哈士奇",@"h",@"柯基",@"k",@"金毛",@"j",nil];NSDictionary *dic1=@{@"哈士奇":@"h",@"柯基":@"k",@"金毛":@"j"};//获取字典中的元素NSString *str=[dic1 objectForKey:@"k"];//统计个数NSLog(@"%ld",[dic count]);//获取所有keyNSArray *arr=[dic1 allKeys];//获取所有ValueNSArray *arrValue=[dic allValues];//可变字典NSMutableDictionary *mDic=[NSMutableDictionary dictionary];[mDic setObject:@"琴女" forKey:@"q"];[mDic setObject:@"盲僧" forKey:@"m"];[mDic setObject:@"螳螂" forKey:@"t"];[mDic setObject:@"淘宝" forKey:@"t"];//移除t[mDic removeObjectForKey:@"t"];for (NSString *item in [mDic allValues]) {NSLog(@"%@",item);}return 0;}

集合

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {//不可变集合NSSet *set1=[NSSet setWithObjects:@"aa",@"22",@"dd",@"gg", nil];//集合没有顺序.只能随机读取 而且会自动去除重复NSLog(@"%@",set1);//获取集合中的元素NSLog(@"%@",[set1 anyObject]);//随机元素//可变集合NSMutableSet *mSet=[NSMutableSet set];[mSet addObject:@"111"];[mSet addObject:@"222"];[mSet addObject:@"222"];[mSet addObject:@"333"];[mSet addObject:@"55"];NSLog(@"%ld",[mSet count]);//4 去除了重复//移除[mSet removeObject:@"111"];NSLog(@"%ld",[mSet count]);//计数集合NSCountedSet *cSet=[NSCountedSet set];[cSet addObject:@"222"];[cSet addObject:@"222"];[cSet addObject:@"333"];[cSet addObject:@"333"];[cSet addObject:@"222"];[cSet addObject:@"111"];[cSet addObject:@"222"];NSLog(@"%ld",[cSet countForObject:@"222"]);//4 统计了次数NSLog(@"%ld",[cSet count]);//3 依然去除了重复int a=10;int b=12;//将基本数据类型存入数组//先转换为引用类型 字符串NSString *astr=[NSString stringWithFormat:@"%d",a];NSString *bstr=[NSString stringWithFormat:@"%d",b];//再存入数组中NSArray *array=[NSArray arrayWithObjects:astr,bstr, nil];//或者转换为 NSNumberfloat f1=9.44;NSNumber *n1=[NSNumber numberWithFloat:f1];//存入数组NSArray *array1=@[n1];//从数组中取出NSNumber *resultNumber=array1[0];//读取时转换float resultFloat=[resultNumber floatValue];NSLog(@"%f",resultFloat);return 0;}

视频学习地址:/webcourse/index.html#course_id=169598&term_id=100198033&taid=83340404420&vid=e14148936nh

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。