-
//使用普通循环来遍历数组中的每个元素 for (int i=0; i<array1.count; i++) { NSString *str = [array1 objectAtIndex:i]; NSLog(@"str%d = %@", i, str); } NSLog(@"--------------------------------------"); //使用for in迭代输出数组中的元素 for (NSString *str in array1) { NSLog(@"str = %@", str); } NSLog(@"--------------------------------------"); //使用迭代器来遍历数组中元素 NSEnumerator *enumerator = [array1 objectEnumerator]; id object; while (object = [enumerator nextObject]) { NSLog(@"str = %@", object); }查看全部
-
NSArray *array1 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; //获取数组的长度 int count = (int)array1.count; NSLog(@"该数组有%d个元素", count); //判断数组中是否还有“2”这个元素 BOOL isHave = [array1 containsObject:@"2"]; if (isHave) { NSLog(@"存在"); } else { NSLog(@"不存在"); } //获取数组中最后一个元素 NSString *lastStr = [array1 lastObject]; NSLog(@"数组最后一个元素 - %@",lastStr); //获取数组中第一个元素 NSString *firstStr = [array1 firstObject]; NSLog(@"数组中第一个元素 - %@", firstStr); //获取数组中第三个元素 NSString *strOf3 = [array1 objectAtIndex:3]; NSLog(@"数组中第三个元素 = %@", strOf3); //数组中元素‘3’的位置 int index = (int)[array1 indexOfObject:@"3"]; NSLog(@"数组中元素‘3’的位置:%d", index);查看全部
-
//创建可变字符串 NSMutableString *mstr = [[NSMutableString alloc] initWithCapacity:10]; [mstr setString:@"hello"]; //追加字符串 [mstr appendString:@" world"]; int num = 10; [mstr appendFormat:@" - %d -",num]; NSLog(@"mstr = %@", mstr); //替换指定范围的字符串 NSRange range3 = [mstr rangeOfString:@"world"]; [mstr replaceCharactersInRange:range3 withString:@"iOS"]; NSLog(@"mstr = %@", mstr); //指定位置插入字符串 [mstr insertString:@"Android and " atIndex:6]; NSLog(@"mstr = %@", mstr); //删除指定范围的字符串 NSRange range4 = [mstr rangeOfString:@"hello "]; [mstr deleteCharactersInRange:range4]; NSLog(@"mstr = %@", mstr);查看全部
-
NSString *url = @"www.baidu.com"; NSURL *httpURL = [NSURL URLWithString:url]; //NSURL *fileURL = [NSURL fileURLWithPath:url]; NSString *str20 = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil]; NSLog(@"str20 = %@", str20); //读取本地文件的内容 NSString *str21 = [NSString stringWithContentsOfFile:@"/Users/hongliang/Documents/study Objective-C Demo/lesson 1/Lesson1/Lesson1/demo.txt" encoding:NSUTF8StringEncoding error:nil]; NSLog(@"str21 = %@", str21); //将内容写入到文件 NSString *str22 = @"Hello China!Hello Shanghai!"; BOOL result = [str22 writeToFile:@"/Users/hongliang/Documents/study Objective-C Demo/lesson 1/Lesson1/Lesson1/test.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (result) { NSLog(@"写入成功!"); } else { NSLog(@"写入失败!"); }查看全部
-
//替换字符串中指定的字符串 NSString *str19 = [str17 stringByReplacingOccurrencesOfString:@"hello" withString:@"您好"]; NSLog(@"str19 = %@", str19);查看全部
-
//从头开始截取,姐渠道指定的某一位 NSString *str15 = [str10 substringToIndex:4]; NSLog(@"str15 = %@", str15); //将字符串拆分为每一个字符 for (int i=0; i<[str10 length]; i++) { NSLog(@"%c", [str10 characterAtIndex:i]); } //查找指定字符串的位置,从头开始查找 NSString *str16 = @"ab,cd,ef,ds,gd"; NSRange range1 = [str16 rangeOfString:@"ef"]; NSLog(@"range1.location = %ld range1.length = %ld", range1.location, range1.length); //替换某一个范围的内容 NSString *str17 = @"hello iOS, hello Xcode!"; NSString *str18 = [str17 stringByReplacingCharactersInRange:NSMakeRange(0, 5) withString:@"您好"]; NSLog(@"str18 = %@", str18);查看全部
-
//判断两个字符串是否相同 NSString *str10 = @"hongliang"; NSString *str11 = @"hongliangs"; BOOL isEqual = [str10 isEqualToString:str11]; if (isEqual) { NSLog(@"Two strings are equal."); } else { NSLog(@"Two strings are not equal."); } //按指定的字符分割字符串 NSString *str12 = @"a,b,c,d,e"; NSArray *strArray = [str12 componentsSeparatedByString:@","]; for (NSString *str in strArray) { NSLog(@"%@", str); } //按照范围截取字符串 NSRange range = NSMakeRange(3, 4); NSString *str13 = [str11 substringWithRange:range]; NSLog(@"str13 = %@", str13); //从某一位开始截取,一直截取到字符串的最后 NSString *str14 = [str10 substringFromIndex:4]; NSLog(@"str14 = %@", str14);查看全部
-
//判读是否存在相应的前缀 NSString *str9 = @"prefix"; BOOL hasPrefix = [str9 hasPrefix:@"pre"]; if (hasPrefix) { NSLog(@"存在相应的前缀"); }else{ NSLog(@"不存在相应的前缀"); } //判断是否存在相应的后缀 BOOL hasSuffix = [str9 hasSuffix:@"cn"]; if (hasSuffix) { NSLog(@"存在相应的后缀"); }else{ NSLog(@"不存在相应的后缀"); }查看全部
-
//C -> OC char *cstr = "hello world"; NSString *str1 = [NSString stringWithUTF8String:cstr]; NSLog(@"str1 = %@", str1); //OC -> C NSString *str2 = @"hello objective-c"; const char *cstr1 = [str2 UTF8String]; NSLog(@"str2 = %s", cstr1); //格式化字符串 int a = 10; int b = 20; NSString *str3 = [NSString stringWithFormat:@"a + b = %d", a+b]; NSLog(@"str3 = %@", str3); //拼接字符串 NSString *str4 = [str1 stringByAppendingString:str2]; NSLog(@"str4 = %@", str4); //将字符串转换成小写 NSString *str5 = @"ABCDE"; NSString *str6 = [str5 lowercaseString]; NSLog(@"str6 = %@", str6); //将字符串转换大写 NSString *str7 = @"jackie"; NSString *str8 = [str7 uppercaseString]; NSLog(@"str8 = %@", str8);查看全部
-
为什么 self = [super。 init]报错查看全部
-
foundation 框架查看全部
-
u查看全部
-
//遍历字典 for (NSString *key in dict2) { NSLog(@"%@ = %@",key,[dict2 objectForKey:key]); } //迭代器 NSEnumerator *en = [dict2 keyEnumerator]; id key = nil; while (key = [en nextObject]) { NSLog(@"key - %@",key); } NSMutableDictionary *mDict = [[NSMutableDictionary alloc]init]; //add [mDict setObject:@"1" forKey:@"a"]; [mDict setObject:@"2" forKey:@"b"]; //delete //[mDict removeAllObjects]; //[mDict removeObjectForKey:@"b"]; [mDict removeObjectsForKeys: [NSArray arrayWithObjects: @"a",@"b",nil]]; NSLog(@"mDict=%@",mDict);查看全部
-
/*dictionary 1.内存不连续 2.用key和value进行对应(建值对,KVC keyValueCodeing)*/ //声明时赋值 NSDictionary *dict1=[NSDictionary dictionaryWithObject:@"1" forKey:@"a"]; NSLog(@"dict1%@",dict1); NSDictionary *dict2=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3",nil] forKeys:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]]; NSLog(@"dict2%@",dict2); //key:value NSDictionary *dict3=@{@"1":@"d",@"2":@"e",@"3":@"ff"}; NSLog(@"dict3%@",dict3); int count=(int)[dict2 count]; NSLog(@"dict2 count:%d",count); NSString *value=[dict2 valueForKey:@"b"]; NSLog(@"value = %@",value); NSString *value2=[dict2 objectForKey:@"b"]; NSLog(@"value2 = %@",value2); NSArray *allValues =[dict2 allValues]; NSLog(@"allValues=%@",allValues); NSArray *allKeys = [dict2 allKeys]; NSLog(@"allKeys=%@",allKeys);查看全部
-
Person *p1=[[Person alloc]initWithName:@"c1"]; Person *p2=[[Person alloc]initWithName:@"c2"]; Person *p3=[[Person alloc]initWithName:@"c3"]; NSArray *perArray =[[NSArray alloc]initWithObjects:p2,p3,@"1",@"2",@"3", nil]; NSMutableArray *array=[[NSMutableArray alloc]init]; //add element [array addObject:p1]; //add element form a array's all element [array addObjectsFromArray:perArray]; NSLog(@"%@",array); //delete //delete last element //[array removeLastObject]; //remove assign element //[array removeObject:@"3"]; //remove element by index(注意数组元素的个数,下标问题会导致崩溃) //[array removeObjectAtIndex:3]; //delete all element [array removeAllObjects]; //交换元素的位置 [array exchangeObjectAtIndex:1 withObjectAtIndex:5]; NSLog(@"%@",array);查看全部
举报
0/150
提交
取消