------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!
一.NSString和NSMutableString
1.NSString
1>NSString是不可变字符串,字符串对象一旦创建出来,其内容不可以被改变。
2>创建字符串
//最简单的创建方式
NSString *s1 = @"jack";
NSString *s3 = [[NSString alloc] initWithFormat:@"%@",@"jack"];
// 等价的类方法
NSString *s4 = [NSString stringWithFormat:@"%@",@"jack"];
// 以C字符串初始化。C字符串 -->OC字符串
NSString *s5 = [[NSString alloc] initWithUTF8String:"jack"];
// 等价的类方法
NSString *s4 = NSString stringWithUTF8String:"jack";
// 拼接一个字符串从而创建一个新字符串
NSString *s = [@"jack" stringByAppendingString:@"luse"];
3>从文件读取内容
// 读取文件内容,将文件内容保存到字符串
// 第1个参数:文件的绝对路径 第2个参数:编码。涉及到转码,用到中文就可以用NSUTF8StringEncoding这种编码, 第3个参数
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil ];
// 等价的类方法 [NSString stringWithContentsOfFile: encoding: error:];
4>从URL资源读取内容
NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/01-常用结构体.rtf"];
// [NSURL fileURLWithPath:] 参数直接为文件路径,不用协议头file://
// 等价的类方法 [NSURL URLWithString:];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
// 等价的类方法 [NSString stringWithContentsOfURl: error:] ;
2.NSMutableString
1>NSMutableString继承自NSString,所以可以使用NSString的所有方法。与NSString不同的是它是可变字符串,字符串内容可以被改变。
2>拼接字符串
NSMutableString *s1 = [NSMutableString stringWithFormat:@"jack"];
// 拼接字符串到s1的后面
[s1 appendString:@"1 2 "];
3>删除某个范围的字符串
NSMutableString *s1 = [NSMutableString stringWithFormat:@"jack"];
// 确定范围
NSRange range = [s1 rangeOfString:@"ac"];
[s1 deleteCharactersInRange:range];
二.NSNumber和NSValue
1.NSNumber
1>在一些特殊情况下我们需要将基本数据类型的数据包装成一个对象,此时我们就用到了NSNumber,它可以将数值类型的数据包装成一个NSNumber对象。
2>将各种基本数据类型包装成NSNumber对象
// 使用方法类方法numberWith基本类型:就可以将其包装成一个NSNumber对象
int x = 10;
NSNumber *num1 = [NSNumber numberWithInt:10];
NSNumber *num2 = [NSNumber numberWithInt:x];
double x1 = 10.9;
NSNumber *num3 = [NSNumber numberWithDouble:10.9];
NSNumber *num4 = [NSNumber numberWithDouble:10.9];
3>快速包装基本类型
int x = 10;
// 使用"@常量;"这种方式可以很方便的包装秤NSNumber对象。这是一个编译器特性。
// 将变量快速包装成NSNumber对象,需要使用小括号将变量名包含
NSArray *numberArray = @[@12,@(x),@'A'];
4>将NSNumber对象转成基本数据类型:调用对象方法 "基本类型Value"可以将NSNumber对象转换成指定基本类型的数据。
NSNumber *num = [NSNumber numberWithInt:10];
NSDictionary *dic = @{
@"name" : @"jack",
@"age" : num
};
// 取出key@"age"对应的对象num
NSNumber *num2 = dic[@"age"];
// 将num2转成int类型的数据。
int a = [num2 intValue];
2.NSValue
1>NSValue是NSNumber的父类,也是一个包装类,可以包装任何类型。主要用于将结构体包装成NSValue对象。NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue
2>将常用结构体包装成NSValue
CGPoint p = CGPointMake(10, 10);
// 将结构体转为Value对象
NSValue *value = [NSValue valueWithPoint:p];
//+ (NSValue *)valueWithRect:(NSRect)rect
//+ (NSValue *)valueWithSize:(NSSize)size;
//+ (NSValue *)valueWithRange:(NSRange)range;
3>将NSValue转换成结构体
CGPoint point = [value pointValue];
//@property (readonly) NSPoint pointValue;
//@property (readonly) NSSize sizeValue;
//@property (readonly) NSRect rectValue;
三.NSDate
1.NSDate是用来表示时间的类。
2.创建NSDate对象
// 创建一个时间对象
NSDate *date = [NSDate date];
// 打印出的时候是0时区的时间(北京-东8区,早8小时)
NSLog(@"%@", date);
//根据一个时间对象创建一个晚了多少秒的时间对象
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
// 从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
3.日期格式化
NSDate *date = [NSDate date];
// 日期格式化类
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置dateFormat改变格式
// y 年 M 月 d 日
// m 分 s 秒 H (24)时 h(12)时
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 将指定时间按格式转化成字符串
NSString *str = [formatter stringFromDate:date];
4.将字符串转化成NSDate对象
NSString *time = @"2011/09/10 18:56";
// 设置时间格式
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
// 将指定字符串转化成NSDate对象
NSDate *date = [formatter dateFromString:time];