OC基本对象类型
一、NSString
1.NSString 不可变字符串 里面的内容无法更改
1 //1.如何创建字符串对象 2 NSString *str = @"Jack" ; 3 NSLog(@"----%@",str); 4 5 //创建一个空的字符串对象 创建之后对象里面的内容是不能被改变的 6 //尽量不用 7 NSString *str2 = [[[NSString alloc] init] autorelease]; 8 NSString *str2_ = [NSString string]; 9 NSLog(@"----%@",str2); 10 11 //用已经存在的字符串对象去创建一个新的对象 12 NSString *str3 = [[[NSString alloc] initWithString:str] autorelease]; 13 NSString *str3_ = [NSString stringWithString:str];//工厂方法 14 15 //按照一定的格式拼接字符串 字符串里面的值由一些变量决定 16 int age = 10; 17 NSString *name = @"Rose"; 18 NSString *str4 = [[[NSString alloc] initWithFormat:@"%@ is %d years old!", name, age] autorelease]; 19 NSString *str4_ = [NSString stringWithFormat:@"%@ is %d years old!", name, age]; 20 NSLog(@"%@", str4); 21 22 //length 判断字符串是否为空 视频1G 23 NSLog(@"%lu -- %@ ",str4.length, str4); 24 25 //查找子字符串 26 NSString *des = @"The NSString class and its mutable subclass"; 27 28 //从index -> 结尾 29 NSString *s1 = [des substringFromIndex:4]; 30 //从开头 -> index 不包含index对应的字符 31 NSString *s2 = [des substringToIndex:5]; 32 33 //NSRange range = {4,8};//c语言的方式 34 //某个范围的子字符串 35 NSRange range = NSMakeRange(4, 8); 36 NSString *s3 = [des substringWithRange:range]; 37 NSLog(@"%@", s3); 38 39 //字符串的比较 @"123456" 40 NSString *passwd = @"123"; 41 if ([passwd isEqualToString:@"123456"]) { 42 NSLog(@"the same"); 43 } else{ 44 NSLog(@"not the same"); 45 } 46 47 NSString *addr = @"a"; 48 NSComparisonResult result = [addr compare:@"b"]; 49 if (result == NSOrderedAscending) { 50 NSLog(@" < "); 51 } else if (result == NSOrderedSame){ 52 NSLog(@" = "); 53 } else{ 54 NSLog(@" > "); 55 } 56 57 //两个对象的比较 58 NSString *ob1 = @"Jack"; 59 NSString *ob2 = @"jacken"; 60 61 Person *p1 = [[Person new] autorelease]; 62 Person *p2 = [[Person new] autorelease]; 63 //注意:p1和p2不是一个对象 只是名字相同而已 64 // p1.name = @"Jack"; 65 // p2.name = @"Jack"; 66 67 //当一个对象被添加到一个集合里面(NSArray NSDictionary NSSet) 68 //这个对象的引用计数+1 69 NSArray *pArray = @[p1]; 70 p1.name = @"Jack"; 71 72 NSLog(@"%@", ((Person *)pArray[0]).name); 73 74 if ([p1 isEqual:pArray[0]]) { 75 NSLog(@"是同一个对象"); 76 } else{ 77 NSLog(@"不是同一个对象"); 78 } 79 80 //前缀和后缀 81 NSString *url = @"http:png//f.hiphotos.baidu.com/image/h%3D300/sign=16e69d830f4f7png8f09f0b9cf349300a83/63d0f703918fa0ece5f167da2a9759ee3d6ddb37.png"; 82 if ([url hasSuffix:@".jpg"]){ 83 NSLog(@"是一个图片"); 84 } 85 86 if ([url hasPrefix:@"http:"]) { 87 NSLog(@"是一个网址"); 88 } 89 90 //是否包含某一个字符串 91 //快来看我的视频 t.dfafaf.v 快给我点赞哦 92 if ([url containsString:@"baidu.com"]) { 93 NSLog(@"是百度的资源"); 94 } 95 96 //后去某个字符串在源字符串里面的具体位置 97 NSRange r = [url rangeOfString:@"png"]; 98 if (r.length == 0){ 99 NSLog(@"不存在"); 100 } else{ 101 NSLog(@"存在:%lu %lu", r.location, r.length); 102 } 103 104 //在一个字符串后面追加一个字符串形成新的字符串 105 // @"abc" @"de" 106 // @"abcde" 107 NSString *hStr = @"abc"; 108 NSString *newStr = [hStr stringByAppendingString:@"de"]; 109 NSString *newStr2 = [hStr stringByAppendingFormat:@"my name is %@", name]; 110 NSLog(@"%@ %@", hStr, newStr); 111 112 //字符串的数字 转化为对应的类型doubleValue floatValue 113 NSString *numStr = @"123"; 114 int num = [numStr intValue]; 115 116 NSString *ntr = [NSString stringWithFormat:@"%d", age];
2.NSMutableString 可变字符串 内容可以被更改
1 //NSMutableString 可变字符串 insert delete change 2 NSMutableString *mStr = [[NSMutableString alloc] initWithCapacity:0]; 3 NSMutableString *mStr_ = [NSMutableString stringWithCapacity:0]; 4 NSLog(@"%@", mStr); 5 6 //字符串的追加 7 [mStr appendString:@"Hello"]; 8 NSLog(@"%@", mStr); 9 10 [mStr appendFormat:@" my name is %@", name]; 11 NSLog(@"%@", mStr); 12 13 //字符串的插入 14 [mStr insertString:@"World " atIndex:6]; 15 NSLog(@"%@", mStr); 16 17 //替换 18 NSRange rg = [mStr rangeOfString:@"Rose"]; 19 [mStr replaceCharactersInRange:rg withString:@"Jack"]; 20 NSLog(@"%@", mStr); 21 22 //删除数据 23 [mStr deleteCharactersInRange: NSMakeRange(0, 12)]; 24 NSLog(@"%@", mStr); 25 26 //重新设置内容 27 [mStr setString:@"abc"]; 28 NSLog(@"%@", mStr); 29 30 /* 31 NSCaseInsensitiveSearch = 1, //不区分大小写比较 32 NSLiteralSearch = 2, //逐字节比较 区分大小写 33 NSBackwardsSearch = 4, //从字符串末尾开始搜索 34 */ 35 [mStr replaceOccurrencesOfString:@"ABC" withString:@"one two three" options:NSLiteralSearch range:NSMakeRange(0, 3)]; 36 NSLog(@"%@", mStr);
二、NSArray
1.注意:1⃣️OC的数组只能存放对象类型 不能存放C语言的基本数据类型
2⃣️OC的数组可以存放多种对象类型的数据【@"dfa", @2, pxd, cc, shuzu】
3⃣️不能存NSNull的数据
2.操作和方法
1 #import2 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 //创建 6 NSArray *array = @[@"Jack", @"Tom", @"Merry"]; 7 8 //使用方法 9 NSArray *arr = [[NSArray alloc] initWithArray:array]; 10 NSLog(@"%@", arr); 11 12 //用多个对象初始化一个数组 13 //注意:数组的初始化以nil表示结尾,不能向数组里面插入为nil的值 14 NSArray *arr1 = [[NSArray alloc] initWithObjects:@"Jack", @"Tom", nil]; 15 NSArray *arr_1 = [NSArray arrayWithObjects:@"Tom", @"Jack", nil]; 16 NSLog(@"%@", arr_1); 17 18 //数组元素个数 count 19 NSLog(@"%ld", array.count); 20 21 //访问数组的对象 索引值 22 NSString *name = [arr1 objectAtIndex:0]; 23 24 //获取某个对象在数组里面的索引值 25 NSUInteger index = [arr1 indexOfObject:@"Tom"]; 26 NSLog(@"%lu", index); 27 28 //判断数组里面是否包含某个对象 29 BOOL result = [arr1 containsObject:@"Jack"]; 30 31 //快速获取数组第一个元素 32 [arr1 firstObject];//第一个元素 33 [arr1 lastObject];//最后一个元素 34 35 //可变数组 36 NSMutableArray *ar = [NSMutableArray arrayWithCapacity:0]; 37 38 //向数组里面添加一个元素 39 [ar addObject:@"shanghai"]; 40 NSLog(@"%@", ar); 41 42 //插入元素 43 [ar insertObject:@"bj" atIndex:1]; 44 NSLog(@"%@", ar); 45 46 //交换数据 47 [ar exchangeObjectAtIndex:0 withObjectAtIndex:1]; 48 NSLog(@"%@", ar); 49 50 //删除数据 51 [ar removeObject:@"shanghai"]; 52 [ar removeObject:@"bj"]; 53 [ar removeObjectAtIndex:1]; 54 [ar removeAllObjects]; 55 56 //NSNumber封装C语言的数据类型 int long char double float bool 57 NSNumber *nb = [NSNumber numberWithInt:20]; 58 59 //NSValue封装C语言的结构体和枚举 60 NSRange range = NSMakeRange(0, 20); 61 62 /* 63 {x,y,w,h} 64 65 struct CGRect{ 66 CGPoint orign; 67 CGSize size; 68 }; 69 70 struct CGPoint{ 71 CGFloat x; 72 CGFloat y; 73 }; 74 75 struct CGSize{ 76 CGFloat width; 77 CGFloat height; 78 }; 79 80 */ 81 82 CGRect rect = CGRectMake(100, 100, 200, 150); 83 NSValue *rectValue = [NSValue valueWithRect:rect]; 84 NSValue *structValue = [NSValue valueWithRange:range]; 85 NSArray *numbersArray= @[nb, @20, structValue, rectValue]; 86 87 NSNumber *n1 = [numbersArray objectAtIndex:0]; 88 NSValue *strValue = [numbersArray objectAtIndex:2]; 89 90 int age = [n1 intValue]; 91 CGRect frame = [strValue rectValue]; 92 93 } 94 return 0; 95 }
三、NSDictionary
1.概念:字典里面没有顺序,存储多个拥有不同种类型的数据key -> Value
2.操作和方法
//创建
NSDictionary *dic = @{@"name":@"JianZe",@"age":@"19"};
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"JianZe" forKey:@"name"];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"JianZe", @"age" , @"19", nil];
//NSLog(@"%@ %@ %@", dic, dic1, dic3);
//返回字典的键值对数目 count
NSLog(@"%lu", dic.count);
//根据key取出value
NSString *str = [dic3 objectForKey:@"19"];
NSLog(@"%@", str);
//遍历字典
for (NSString *key in dic3) {
NSLog(@"key = %@, value = %@", key, [dic3 objectForKey:key]);
}
//可变字典
//创建
NSMutableDictionary *dic4 = [NSMutableDictionary dictionary];
NSMutableDictionary *dic5 = [NSMutableDictionary dictionaryWithCapacity:0];
//增加
[dic4 setValue:@"JianZe" forKey:@"name"];
//删除
[dic4 removeObjectForKey:@"name"];
[dic4 removeAllObjects];
//改
[dic4 setValue:@"JianZe" forKey:@"name"];
[dic4 setObject:@"jianze" forKey:@"name"];
//查
NSArray *arr = [dic4 allKeys];
if ([arr containsObject:@"jianze"]) {
}