一、NSString与NSMutableString
1.NSString (不可变字符串)
1.1字符串的创建
方式一 NSString *s1 = @"jack";
方式二 NSString *s2 = [[NSString alloc] initWithString:@"jack"];
方式三 NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
方式四
C字符串 --> OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
OC字符串 --> C字符串
const char *cs = [s4 UTF8String];
方式五
把文件资源转为字符串
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
方式六
把各种资源转为字符串
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"s6=\n%@", s6);
1.2字符串写入文件资源路径
[@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
1.3字符串写入各种资源路径
NSString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
1.4拼接形成新的字符串
NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
2.NSMutableString (可变字符串)
2.1NSMutableString的创建
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
2.2拼接可变字符串
[s1 appendString:@" 11 12"];
2.3获取字符串的范围
NSRange range = [s1 rangeOfString:@"is"];
2.4删除字符串
[s1 deleteCharactersInRange:range];
二、NSValue
1.NSValue
1.1NSValue的创建
CGPoint p = CGPointMake(10, 10);
NSValue *value = [NSValue valueWithPoint:p];
CGSize s = CGSizeMake(3,4);
NSValue *v2 = [NSValue valueWithSize:s];
CGRect r = CGRectMake(1,32,5,6);
NSValue *v3 = [NSValue valueWithRect:r];
1.2将value还原结构体
还原为Point结构
[value pointValue];
还原为Size结构
[v2 sizeValue];
还原为Rect
[v3 rectValue];
三、NSNumber
1.1NSNumber的创建
NSNumber *num = [NSNumber numberWithInt:10];
NSNumber *n = [NSNumber numberWithDouble:10.5];
@10.5;
@'A'; // NSNumber对象
1.2还原位int数据类型
int a = [num2 intValue];
还原为double数据类型
double d = [n doubleValue];
四、NSDate
注意:
输出的时候是0时区的时间
y 年 M 月 d 日
m 分 s 秒 H (24)时 h(12)时
1.1NSDate的创建
NSDate *date = [NSDate date];
日期格式化类
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *str = [formatter stringFromDate:date];
1.2从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
1.3输出一个表示时间的字符串
NSString *time = @"2011/09/10 18:56";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
NSDate *date = [formatter dateFromString:time];
1.NSString (不可变字符串)
1.1字符串的创建
方式一 NSString *s1 = @"jack";
方式二 NSString *s2 = [[NSString alloc] initWithString:@"jack"];
方式三 NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
方式四
C字符串 --> OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
OC字符串 --> C字符串
const char *cs = [s4 UTF8String];
方式五
把文件资源转为字符串
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
方式六
把各种资源转为字符串
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"s6=\n%@", s6);
1.2字符串写入文件资源路径
[@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
1.3字符串写入各种资源路径
NSString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
1.4拼接形成新的字符串
NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
2.NSMutableString (可变字符串)
2.1NSMutableString的创建
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
2.2拼接可变字符串
[s1 appendString:@" 11 12"];
2.3获取字符串的范围
NSRange range = [s1 rangeOfString:@"is"];
2.4删除字符串
[s1 deleteCharactersInRange:range];
二、NSValue
1.NSValue
1.1NSValue的创建
CGPoint p = CGPointMake(10, 10);
NSValue *value = [NSValue valueWithPoint:p];
CGSize s = CGSizeMake(3,4);
NSValue *v2 = [NSValue valueWithSize:s];
CGRect r = CGRectMake(1,32,5,6);
NSValue *v3 = [NSValue valueWithRect:r];
1.2将value还原结构体
还原为Point结构
[value pointValue];
还原为Size结构
[v2 sizeValue];
还原为Rect
[v3 rectValue];
三、NSNumber
1.1NSNumber的创建
NSNumber *num = [NSNumber numberWithInt:10];
NSNumber *n = [NSNumber numberWithDouble:10.5];
@10.5;
@'A'; // NSNumber对象
1.2还原位int数据类型
int a = [num2 intValue];
还原为double数据类型
double d = [n doubleValue];
四、NSDate
注意:
输出的时候是0时区的时间
y 年 M 月 d 日
m 分 s 秒 H (24)时 h(12)时
1.1NSDate的创建
NSDate *date = [NSDate date];
日期格式化类
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *str = [formatter stringFromDate:date];
1.2从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
1.3输出一个表示时间的字符串
NSString *time = @"2011/09/10 18:56";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
NSDate *date = [formatter dateFromString:time];