一、常用的结构体
NSRange (location,length)
NSPoint\CGPoint (x,y)
NSSize\CGSize (width, height)
NSRect\CGRect (CGPoint,CGSize)
1.NSRange
void range()
{
/* @"i love oc" love的范围
NSRange r1 = {2, 4}; // 不用
NSRange r2 = {.location = 2, .length = 4};// 不用
NSRange r3 = NSMakeRange(2, 4); // 掌握 */
// 查找某个字符串在str中的范围
// 如果找不到,length=0,location=NSNotFound==-1
NSString *str = @"i love oc";
NSRange range = [str rangeOfString:@"java"];
NSLog(@"loc = %ld, length=%ld", range.location, range.length);
}
2、快速创建一个结构题
NSPoint p2 = CGPointMake(20, 20);
NSSize s1 = CGSizeMake(100, 50);
CGRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r2 = { {0, 0}, {100, 90}};
CGRect r3 = {p1, s1};
表示原点:CGPointZero == CGPointMake(0, 0)
将结构体转为字符串:NSString *str = NSStringFromRect(r1);
3、使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架
二、NSString 与 NSMutableString
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];
// NSUTF8StringEncoding 用到中文就可以用这种编码
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
// URL : 资源路径
// 协议头://路径
// file://
// ftp://
// http://weibo.com/a.png
// NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
一般都会有一个类方法跟对象方法配对
[NSURL URLWithString:<#(NSString *)#>];
[NSString stringWithFormat:@""];
[NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
// 拼接内容到s1的后面
[s1 appendString:@" 11 12"];
2、字符串的导出
void stringExport()
{
// 字符串的导出
[@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
三.数组
NSSet和NSArray的对比
1> 共同点
* 都是集合,都能存放多个OC对象
* 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、char、float等,结构体,枚举)
* 本身都不可变,都有一个可变的子类
2> 不同点
NSArray有顺序,NSSet没有顺序
四、集合
1.NSArray\NSMutableArray
* 有序
* 快速创建(不可变):@[obj1, obj2, obj3]
* 快速访问元素:数组名[i]
2.NSSet\NSMutableSet
* 无序
3.NSDictionary\NSMutableDictionary
* 无序
* 快速创建(不可变):@{key1 : value1, key2 : value2}
* 快速访问元素:字典名[key]
五、NSNumber
作用:将各种基本数据类型包装成NSNumber 对象
例如:
[NSNumber numberWithInt:age];
NSNumber *n = [NSNumber numberWithDouble:10.5];
六、NSValue
NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue
// 结构体--->OC对象
CGPoint p = CGPointMake(10, 10)
// 将结构体转为Value对象
NSValue *value = [NSValue valueWithPoint:p];
// 将value转为对应的结构体
// [value pointValue];
NSArray *array = @[value ];
七、NSDate
void date2string()
{
NSDate *date = [NSDate date];
// 日期格式化类
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// y 年 M 月 d 日
// m 分 s 秒 H (24)时 h(12)时
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *str = [formatter stringFromDate:date];
NSLog(@"%@", str);
}
void use()
{
// 创建一个时间对象
NSDate *date = [NSDate date];
// 打印出的时候是0时区的时间(北京-东8区)
NSLog(@"%@", date);
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
// 从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
// [date2 timeIntervalSinceNow];
}