ios基本语法

  • Post author:
  • Post category:其他



1.NSString

//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);

//判读是否存在相应的前缀
NSString *str9 = @"prefix";
BOOL hasPrefix = [str9 hasPrefix:@"pre"];
if (hasPrefix) {
NSLog(@"存在相应的前缀");
}else{
NSLog(@"不存在相应的前缀");
}

//判断是否存在相应的后缀
BOOL hasSuffix = [str9 hasSuffix:@"cn"];
if (hasSuffix) {
NSLog(@"存在相应的后缀");
}else{
NSLog(@"不存在相应的后缀");
}

//判断两个字符串是否相同
NSString *str10 = @"hello";
NSString *str11 = @"hello";
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 *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 *str19 = [str17 stringByReplacingOccurrencesOfString:@"hello" withString:@"您好"];
NSLog(@"str19 = %@", str19);
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/hello/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/hello/Documents/study Objective-C Demo/lesson 1/Lesson1/Lesson1/test.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (result) {
NSLog(@"写入成功!");
} else {
NSLog(@"写入失败!");
}


2.NSNutableString

//创建可变字符串
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);


3.NSArray

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);
//使用普通循环来遍历数组中的每个元素
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);
}


4.NSMutableArray

Person *p1 = [[Person alloc] initWithName:@"Kevin"];
Person *p2 = [[Person alloc] initWithName:@"Steve"];
Person *p3 = [[Person alloc] initWithName:@"Gates"];

NSArray *array2 = [[NSArray alloc] initWithObjects:p2,p3, nil];
NSMutableArray *array1 = [[NSMutableArray alloc] init];

//数组中添加一个元素
[array1 addObject:p1];

//通过另一个数组给新数组添加元素
[array1 addObjectsFromArray:array2];
NSLog(@"array1 = %@", array1);

//移除数组中的最后一个元素
//[array1 removeLastObject];

//移除数组中的某一个元素
//[array1 removeObject:p2];

//删除数组中某一位的元素
//[array1 removeObjectAtIndex:0];

//删除数组中的所有元素
//[array1 removeAllObjects];

//交换数组中某两位元素
[array1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"array1 = %@", array1);


5.NSDictionary

//创建字典
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"steve" forKey:@"1"];
NSLog(@"dict1 = %@", dict1);

NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Gates",@"Cook",@"Steve", nil] forKeys:[NSArray arrayWithObjects:@"1",@"2",@"3", nil]];
NSLog(@"dict2 = %@", dict2);

NSDictionary *dict3 = @{@"1":@"Kevin",@"2":@"Tim",@"3":@"Jack"};
NSLog(@"dict3 = %@", dict3);

//获取字典的个数
int count = (int)[dict2 count];
NSLog(@"dict2的元素个数:%d", count);
//根据Key获取Value
NSString *value1 = [dict3 valueForKey:@"3"];
NSLog(@"value1 = %@", value1);

NSString *value2 = [dict3 objectForKey:@"2"];
NSLog(@"value2 = %@", value2);

//获取字典中所有的值
NSArray *arr1 = [dict3 allValues];
NSLog(@"arr1 = %@", arr1);

//获取字典中所有的键
NSArray *arr2 = [dict3 allKeys];
NSLog(@"arr2 = %@", arr2);

//遍历字典
for (NSString *key in dict3) {
NSLog(@"%@ - %@", key, [dict3 valueForKey:key]);
}

//使用迭代器遍历字典中的键
id key;
NSEnumerator *en = [dict3 keyEnumerator];
while (key = [en nextObject ]) {
NSLog(@"key = %@", key);
}


6.NSMutableDictionary

//创建可变字典
NSMutableDictionary *mdict1 = [[NSMutableDictionary alloc] init];
[mdict1 setValue:@"Steve" forKey:@"apple"];
[mdict1 setObject:@"Gates" forKey:@"microsoft"];
NSLog(@"mdict1 = %@", mdict1);

//删除字典中所有的元素
//[mdict1 removeAllObjects];
//根据键删除字典中的某个元素
//[mdict1 removeObjectForKey:@"microsoft"];
//根据一组键删除某些元素
[mdict1 removeObjectsForKeys:[NSArray arrayWithObjects:@"apple",@"microsoft", nil]];
NSLog(@"mdict1 = %@", mdict1);



版权声明:本文为chenzheng8975原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。