1. NSString类
// 求字符串长度
NSString *str = @"Hello郭String";
NSUInteger len = [str length]; // NSUInterger即long
NSLog(@"%ld",len);
//获取子字符串
NSString *substr = [str substringFromIndex:5];//从下标5开始取子字符串
NSLog(@"%@",substr);
// 从开始取子字符串到下标为5的位置截止
NSString *substr2 = [str substringToIndex:5];
NSLog(@"%@",substr2);
NSRange rang = {2,3}; //起始位置和长度
// 从起始位置2取长度为3的子字符串
NSString *substr3 = [str substringWithRange:rang];
NSLog(@"%@",substr3);
//拼接字符串
NSString *comStr1 = [str stringByAppendingString:@"IOS"];
NSLog(@"%@",comStr1);
NSString *comStr2 = [str stringByAppendingFormat:@"hehe%@ %@",@"hhh",@"jjj"];
NSLog(@"%@",comStr2);
//字符串替换
NSString * newStr1 = [str stringByReplacingOccurrencesOfString:@"string" withString:@"world"];
NSLog(@"%@",newStr1);
// 判断字符串是否相等
//判断字符串是否相等用 isEqualTo方法,不能用==判断
// isEqualTo方法判断的时字符串的内容是否相等,而==判断的时两个指针是否指向同一地址
NSString *cmpStr = @"Hello String";
BOOL eq = [str isEqualToString:cmpStr];
if (eq) {
NSLog(@"相等");
}
else{
NSLog(@"不相等");
}
// 判断前缀是否已给定的字符串相等,即是否已该字符串开头
BOOL prefix = [str hasPrefix:@"Hello"];
if (prefix) {
NSLog(@"以Hello开头");
}
else {
NSLog(@"不以Hello开头");
}
//判断后缀
//判断是否已png结尾,是就替换成jpg,否则拼接jpg
BOOL isPng = [str hasSuffix:@"png"];
if (isPng) {
//执行替换
NSString * tem = [str stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
NSLog(@"%@",tem);
}
else
{ //执行拼接jpg
NSString *tem = [str stringByAppendingString:@"jpg"];
NSLog(@"%@",tem);
}
=====================================================================================
2. NSMutableString//可变字符串
NSMutableString *mulStr = [NSMutableString stringWithString:@"Hello"];
NSLog(@"%@",mulStr);
//拼接
[mulStr appendFormat:@"guozai"];
NSLog(@"%@",mulStr);
[mulStr appendString:@"mutible"];
NSLog(@"%@",mulStr);
//删除子字符串
NSRange ran = {4,1};//结构体类型
[mulStr deleteCharactersInRange:ran];
NSLog(@"%@",mulStr);
//替换
NSRange ran2 = {3,2};
[mulStr replaceCharactersInRange:ran2 withString:@"yy"];
NSLog(@"%@",mulStr);
//插入
[mulStr insertString:@"tt" atIndex:2];
NSLog(@"%@",mulStr);
// 对于不可变字符串NSString的字符串拼接,分割等操作,都会创建新的字符串
// 对于可变字符串NSMutableString的字符串拼接分割替换等操作是在原字符串的基础上
// 进行修改,不会创建新字符串
// NSMutableString是NSString的子类,所以NSString的方法,NSMutableString也
//可以使用
// 在以后的学习中,凡是出现Mutable的类,都是不带Mutable类的子类:
//如,NSMutableArray是NSArray的子类,NSMutableDictionary是NSDictonary的子
//类
==================================================================================== 3.NSArray
//数组
//最后的nil不可丢掉
NSArray *arr =[NSArray arrayWithObjects:@"guozai", @"guo",@"zaiguo",nil];
// 获取数组元素个数
NSUInteger count = [arr count];
NSLog(@"%lu",count);
//获取第一个对象
NSString *p1 = [arr firstObject];
NSLog(@"%@",p1);
// 获取最后一个对象
NSString *p2 = [arr lastObject];
NSLog(@"%@",p2);
// 获取下标对应的对象
NSString *p3 = [arr objectAtIndex:1];
NSLog(@"%@",p3);
//遍历数组
for (int i = 0; i < [arr count]; i++) {
NSLog(@"%@",[arr objectAtIndex:i]);
}
=====================================================================================
4.NSMutableArray
//可变数组
// 一个数组的内容赋给另一个数组
NSMutableArray * mulArray = [NSMutableArray arrayWithArray:arr];
//删除下标为index的对象
[mulArray removeObjectAtIndex:2];
// for (int i = 0; i < [arr count]; i++) {
// NSLog(@"%@",[arr objectAtIndex:i]);
// }
//
// 添加一个对象元素
[mulArray addObject:@"guoguo"];
// 交换下标对应的元素对象
[mulArray exchangeObjectAtIndex:0 withObjectAtIndex:[mulArray count]-1]; //交换第一个元素和最后一个元素
=====================================================================================
下面通过一个实例来形象的了解:
使用可变数组管理BOOk类,实现图书的增删查改
BOOK有两个成员变量:_name,_price;
Book * book1 = [[Book alloc] initWithName:@"guozai1" andPrice:10];
Book * book2 = [[Book alloc] initWithName:@"guozai2" andPrice:15];
Book * book3 = [[Book alloc] initWithName:@"guozai3" andPrice:13];
//数组赋值
NSMutableArray *books = [NSMutableArray arrayWithObjects:book1,book2,book3, nil];
Book * book4 = [[Book alloc] initWithName:@"guozai4" andPrice:12];
//添加一本书
[books addObject:book4];
//删除一本书
[books removeObjectAtIndex:2];
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[[books objectAtIndex:i] name],[[books objectAtIndex:i] price]);
}
//查找名字是guozai3的书,打印价格
for (int i = 0; i < [books count]; i ++) {
if ([[[books objectAtIndex:i] name] isEqualToString:@"guozai3"]) {
NSLog(@"%f",[[books objectAtIndex:i] price]);
}
}
// 对数组进行排序,按价格从高到低
for (int i = 0; i < [books count] - 1; i ++) {
for (int j = 0; j < [books count] - i - 1; j ++) {
if ([books[j] price] < [books[j+1] price]) {
[books exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[books[i] name],[books[i] price]);
}
========================================================================================
5.NSNumber:将基本数据类型转化成对象类型
//将基本数据类型int转化为对象类型
NSNumber * intNum = [NSNumber numberWithInt:100];//便利构造器
NSMutableArray * ar = [NSMutableArray arrayWithObjects:intNum, nil];
NSNumber * tem = [ar objectAtIndex:0];
//将对象类型转化成基本数据类型
int result = [tem intValue];
NSLog(@"%d",result);
========================================================================================
6.NSValue:将结构体转化成对象
//将一个点转化成NSValue对象
NSPoint point = {1,2};
//将一个结构体转化成NSValue对象
NSValue *vPoint = [NSValue valueWithPoint:point];
//将vPoint转化成结构体
NSPoint point2 = [vPoint pointValue];
// NSLog(@"%.2f,%.2f",point2.x,point2.y);
// NSStringFromPoint可以将点转化成字符串
NSLog(@"%@",NSStringFromPoint(point2));
其他的例子类比就行:例如
//将NSsize结构体转化成NSValue对象
NSSize size = {22,44};
NSValue * sValue = [NSValue valueWithSize:size];
//将NSValue对象转化成NSSize结构体;
NSSize size2 = [sValue sizeValue];
NSLog(@"%@", NSStringFromSize(size2)); |
|