1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 @autoreleasepool 6 { 7 #pragma mark - Test 8 // int a = 10; 9 // double b = 7.7; 10 // float c = 5.7; 11 // 12 // NSNumber *numberInt = [NSNumber numberWithInt:a]; 13 // int a1 = [numberInt intValue]; 14 // 15 // NSNumber *numberDou = [NSNumber numberWithDouble:b]; 16 // double b1 = [numberDou doubleValue]; 17 // 18 // NSNumber *numberFlo = [NSNumber numberWithFloat:c]; 19 // float c1 = [numberFlo floatValue]; 20 // 21 // NSLog(@"numberInt = %d, numberDou = %lf, numberFlo = %f", a1, b1, c1); 22 // 23 // NSString *str1 = @"Welcome"; 24 // NSString *str2 = @"To"; 25 // NSString *str3 = @"HuaShan"; 26 // 27 // NSString *str4 = [[[[str1 stringByAppendingString:@" "] stringByAppendingString:str2] stringByAppendingString:@" "] stringByAppendingString:str3]; 28 // //连接成功 29 // NSLog(@"%@", str4); 30 // 31 // NSString *str5 = [NSString stringWithFormat:@"%@ %@ %@", str1, str2, str3]; 32 // //连接成功 33 // NSLog(@"%@", str5); 34 // 35 // NSString *str6 = [str1 stringByAppendingFormat:@" %@ %@", str2, str3]; 36 // NSLog(@"%@", str6); 37 // 38 // //第一种 39 // NSString *str7 = [str4 substringToIndex:7]; 40 // //第二种 41 // NSRange range = NSMakeRange(0, 7); 42 // NSString *str8 = [str4 substringWithRange:range]; 43 // //第三种 44 // NSArray *arr = [str4 componentsSeparatedByString:@" "]; 45 // NSLog(@"%@ %@ %@", str7, str8, arr[0]); 46 47 #pragma mark - NSNumber 48 // double a = 8.8; 49 // //实例方法:用NSNumber存储基本数据类型 50 // NSNumber *num1 = [[NSNumber alloc] initWithDouble:a]; 51 // //类方法 52 // NSNumber *num2 = [NSNumber numberWithDouble:a]; 53 // NSLog(@"num1 = %@, num2 = %@", num1, num2); 54 // double a1 = [num1 doubleValue]; 55 // double a2 = [num2 doubleValue]; 56 // NSLog(@"a1 = %.2f, a2 = %.2f", a1, a2); 57 // 58 // NSString *str1 = @"Hello World"; 59 // NSString *str2 = @"HuaShan"; 60 // //连接两个字符串 61 // NSString *str3 = [[str1 stringByAppendingString:@" "] stringByAppendingString:str2]; 62 // NSString *str4 = [NSString stringWithFormat:@"%@ %@", str1, str2]; 63 // NSString *str5 = [str1 stringByAppendingFormat:@" %@", str2]; 64 // NSLog(@"str3 = %@, str4 = %@, str5 = %@", str3, str4, str5); 65 // //去下标6之后的字符串 66 // NSString *subStr = [str1 substringFromIndex:6]; 67 // NSLog(@"subStr = %@", subStr); 68 // //取下标5之前的字符串 69 // NSString *subStr1 = [str1 substringToIndex:5]; 70 // NSLog(@"subStr1 = %@", subStr1); 71 // //取一定范围的字符串 72 // NSRange range = NSMakeRange(6, 2); 73 // NSString *subStr2 = [str1 substringWithRange:range]; 74 // NSLog(@"subStr2 = %@", subStr2); 75 // //比较两个字符串,相等返回YES,否则返回NO。 76 // BOOL bb = [subStr isEqualToString:subStr1]; 77 // if (bb) 78 // NSLog(@"相等"); 79 // else 80 // NSLog(@"不相等"); 81 // //查找字符串的range 82 // NSRange range1 = [str1 rangeOfString:@"World"]; 83 // int loc = (int)range1.location; 84 // int len = (int)range1.length; 85 // NSLog(@"loc = %d, len = %d", loc, len); 86 // 87 // //把字符串转成整型 88 // NSString *str6 = @"123"; 89 // int b = [str6 intValue]; 90 // NSLog(@"b = %d", b); 91 // //组件分割字符串 92 // NSString *str7 = @"hello,huashan,welcome,to,huashankeji"; 93 // NSArray *arr = [str7 componentsSeparatedByString:@","]; 94 // NSLog(@"arr[4] = %@", arr[4]); 95 // 96 // NSString *str8 = [str1 stringByAppendingFormat:@" %@", arr[4]]; 97 // NSLog(@"str8 = %@", str8); 98 // //字符串替换出现的字符串 99 // NSString *str9 = [str8 stringByReplacingOccurrencesOfString:@"huashankeji" withString:@"HuaShanKeJi"]; 100 // NSLog(@"str9 = %@", str9); 101 #pragma mark - NSArray 102 // //数组的创建 103 // NSArray *arr = [NSArray arrayWithObjects:@"hello", @"world", nil]; 104 // //输出为:hello, 2, 1, hello, world 105 // NSLog(@"%@, %ld, %ld, %@, %@", [arr objectAtIndex:0], [arr count], [arr indexOfObject:@"world"], [arr firstObject], [arr lastObject]); 106 // NSArray *arr1 = @[@"hello", @"world"]; 107 // NSLog(@"arr1[0] = %@", arr1[0]);//hello 108 // NSLog(@"%@", arr1);//打印出每一个元素 109 #pragma mark - NSMutableArray 110 // //创建出一个动态数组arr2 111 // NSMutableArray *arr2 = [[NSMutableArray alloc] initWithCapacity:1]; 112 // //添加对象(元素) 113 // [arr2 addObject:@"hello"]; 114 // [arr2 addObject:@"world"]; 115 // [arr2 insertObject:@"HELLO" atIndex:1]; 116 // 117 // NSLog(@"%ld", [arr2 indexOfObject:@"world"]);//找到对象的下标2,并打印 118 // NSLog(@"%ld", [arr2 indexOfObject:@"asdf"]);//对象下标为找到,NSNotFound返回一个9223372036854775807 119 //// [arr2 removeObject:@"HELLO"];//删除指定对象 120 //// [arr2 removeObjectAtIndex:1];//删除下标为1的数组元素(对象) 121 //// [arr2 removeLastObject];//删除最后一个对象 122 // [arr2 replaceObjectAtIndex:1 withObject:@"song"];//替换下标为1的对象 123 // NSLog(@"arr2 = %@", arr2); 124 // 125 // //数组的三种遍历 126 // //第一种 127 // long j = [arr2 count]; 128 // for (int i = 0; i < j; ++i) 129 // { 130 // NSLog(@"%@", arr2[i]); 131 // } 132 // [arr2 addObjectsFromArray:arr];//把数组arr添加到arr2数组后面 133 // NSLog(@"%@", arr2); 134 // //第二种 135 // [arr2 addObject:[NSNumber numberWithInteger:18]];//添加一个长整型常量到对象arr2中 136 // for (NSString *str in arr2) 137 // { 138 // NSLog(@"str = %@", str); 139 // } 140 // //第三种 141 // //用NSArray的类方法创建一个新数组 142 // NSArray *arr3 = [NSArray arrayWithObjects:@"1", @"4", @"3", @"2",nil]; 143 // for (id original in arr3) 144 // { 145 // NSLog(@"object = %@", original); 146 // } 147 // 148 // //比较单个常量字符,并排序。注:字符串比较只比首位,数字字符串线转化为NSNumber 149 // NSArray *arr4 = [arr3 sortedArrayUsingSelector:@selector(compare:)]; 150 // for (id result in arr4) 151 // { 152 // NSLog(@"result = %@", result); 153 // } 154 // 155 // NSArray *arr5 = [arr3 sortedArrayUsingSelector:NSSelectorFromString(@"compare:")]; 156 // for (id result1 in arr5) 157 // { 158 // NSLog(@"result1 = %@", result1); 159 // } 160 161 #pragma mark - NSDictionary 162 //字典的两种初始化方式 163 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"132", @"1", @"527", @"2", nil]; 164 NSString *str = [dic objectForKey:@"2"];//第一种 165 NSLog(@"%@", str);//527 166 //第二种 167 NSDictionary *dic1 = @{@"1":@"132", @"2":@"527"}; 168 NSString *str1 = dic1[@"1"]; 169 NSLog(@"%@", str1);//132 170 //取出字典里所有的键值allValues取出所有的值 171 NSArray *arr = [dic allValues]; 172 NSLog(@"arr = %@", arr); 173 //从所有的键值中一一找到对象 174 NSDictionary *arr1 = [NSDictionary dictionaryWithObjects:arr forKeys:[dic allKeys]]; 175 NSLog(@"arr1 = %@", arr1); 176 #pragma mark - NSMutableDictionary 177 //创造和返回一个空字典 178 NSMutableDictionary *mDic = [NSMutableDictionary dictionary]; 179 [mDic setObject:@"song" forKey:@"1"]; 180 [mDic setObject:@"lei" forKey:@"2"]; 181 [mDic setObject:@"hua" forKey:@"3"]; 182 [mDic setObject:@"shan" forKey:@"4"]; 183 [mDic removeObjectForKey:@"2"]; 184 [mDic removeObjectsForKeys:[NSArray arrayWithObjects:@"1",@"2", nil]]; 185 NSLog(@"mDic = %@", mDic); 186 //字典的快速遍历 187 NSArray *arr2 = [mDic allValues]; 188 for (id object in arr2) 189 { 190 NSLog(@"object = %@", object); 191 } 192 193 } 194 return 0; 195 }
时间: 2024-10-15 19:55:33