一、NSRange、NSPoint\CGPoint、 NSSize\CGSize、NSRect\CGRect (CGPint CGSize)结构体
1)NSRange
NSRange表示一个范围,比如我们想求个@“I fall in love with Objective-C~”; 这个字符串中“love”的表示范围就是位置从11开始,长度为4;
NSRange的本质是一个结构体,
里面含有location和 length两个属性,分别表示位置和长度。
定义一个NSRange变量有很多种方式,如下列出:
这种方式我们一般不用,可读性太差
<span style="font-size:14px;">NSRange r1 = {2, 4}; // 不用</span>
这种方式还是不要用,可读性还是很差
NSRange r2 = {.location = 2, .length = 4};// 不用
NSRange r3 = NSMakeRange(2, 4);
下面我们再来看一个例子:
2)NSPoint\CGPoint
NSPoint和CGPoint是同义词(以下出现的诸如此类的都是同义词);在开发中,我们常常用CGPoint;
CGPoint表示的是一个点,
内部结构为:
struct CGPoint {
CGFloat x;
CGFloat y;
};
定义方式:
CGPoint p1 = NSMakePoint(10, 10);//
NSPoint p2 = CGPointMake(20, 20);// 最常用
// 表示原点
// CGPointZero == CGPointMake(0, 0)
// 将结构体转为字符串
//NSString *str = NSStringFromPoint(p1);
3) NSSize\CGSize
顾名思义,CGSize肯定表示的是一个尺寸,其内部结构为:
struct CGSize{
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
定义一个CGSize
NSSize s1 = CGSizeMake(100, 50);
NSSize s2 = NSMakeSize(100, 50);
CGSize s3 = NSMakeSize(200, 60);
将CGSize转为字符串:
NSString *str = NSStringFromSize(s3);
4)NSRect\CGRect (CGPoint CGSize)
NSRect从其表面就可知道,它表示一个矩形,其实它内部结构就是CGPoint和CGSize;
定义一个CGRect :
CGRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r2 = { {0, 0}, {100, 90}};
CGPoint p1 = NSMakePoint(10, 10);
NSSize s2 = NSMakeSize(100, 50);
CGRect r3 = {p1, s2};
// 使用CGPointZero等的前提是添加CoreGraphics框架
CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};
将结构体转换为字符串:
NSString *str = NSStringFromRect(r1);
二、NSString和NSMutableString
很显然,这代表一个字符串。
创建方式:
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];
//注意这两种书写方式的区别
// 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];
NSLog(@"s6=\n%@", s6);
将一段字符串写到一个文件中:
//writeToFile:文件名 atomically:YES表示原子操作,NO反之
//encoding: 编码
[@"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];
NSMutableString表示可以修改的字符串:
意味着可以增删改查
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
// 拼接内容到s1的后面
[s1 appendString:@" 11 12"];
// 获取is的范围
NSRange range = [s1 rangeOfString:@"is"];
// 删除该范围的字符串
[s1 deleteCharactersInRange:range];
//
NSString *s2 = [NSString stringWithFormat:@"age is 10"];
// 注意此时的s2还是s2,s3跟s2是两个字符串
NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
三、NSArray和NSMutableArray
为了演示示例,先声明并定义了一个类:
<span style="font-size:14px;">#import <Foundation/Foundation.h>
@interface Person : NSObject
@end</span>
<span style="font-size:14px;">#import "Person.h"
@implementation Person
@end</span>
NSArray见名知意,显然是个数组嘛,但是它只能存储OC对象类型,不能存储基本数据类型;
.NSArray的创建
<span style="font-size:14px;"> NSArray *array2 = [NSArray arrayWithObject:@"jack"];</span>
<span style="font-size:14px;">NSArray *array = [NSArray array];</span>
nil是数组元素结束的标记
<span style="font-size:14px;"> NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil];</span>
NSArray的元素个数
<span style="font-size:14px;">NSLog(@"%ld", array3.count);</span>
NSArray中元素的访问
NSLog(@"%@", [array3 objectAtIndex:1]);
//array3[1];
NSLog(@"%@", array3[0]);
遍历数组:
第一种方式:(利用传统的for循环)
<span style="font-size:14px;">Person *p = [[Person alloc] init];
NSArray *array = @[p, @"rose", @"jack"];
for (int i = 0; i<array.count; i++)
{
NSLog(@"%@", array[i]);
}</span>
第二种方式: (利用增强for循环)
//id obj代表着数组中的每一个元素
int i = 0;
for (id obj in array)
{
// 找出obj元素在数组中的位置
NSUInteger i = [array indexOfObject:obj];
NSLog(@"%ld - %@", i, obj);
i++;
if (i==1)
{
break;
}
}
第三种方式: (利用代码块的方式)
[array enumerateObjectsUsingBlock:
// 每遍历到一个元素,就会调用一次block
// 并且当前元素和索引位置当做参数传给block
^(id obj, NSUInteger idx, BOOL *stop)
{
NSLog(@"%ld - %@", idx, obj);
if (idx == 0)
{
// 停止遍历
*stop = YES;
}
}];
NSMutableArray可变数组的基本使用
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", @"jim", nil];
// 添加元素
[array addObject:[[Person alloc] init]];
[array addObject:@"jack"];
// 删除元素
//[array removeAllObjects];
// 删除指定的对象
// [array removeObject:@"jack"];
[array removeObjectAtIndex:0];
// 错误写法
// [array addObject:nil];
NSArray的快速创建方式:
@[ ] 只创建不可变数组NSArray
/* 错误写法
NSMutableArray *array = @[@"jack", @"rose"];
[array addObject:@"jim"];
*/
NSArray *array = @[@"jack", @"rose"];
四、NSSet 和 NSMutableSet
NSSet和NSArray的对比
1> 共同点
* 都是集合,都能存放多个OC对象
* 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、char、float等,结构体,枚举)
* 本身都不可变,都有一个可变的子类
2> 不同点
* NSArray有顺序,NSSet没有顺序
set的基本使用
NSSet *s = [NSSet set];
NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil];
// 随机拿出一个元素
NSString *str = [s2 anyObject];
NSMutbaleSet的使用:
NSMutableSet *s = [NSMutableSet set];
// 添加元素
[s addObject:@"hack"];
// 删除元素
// [s removeObject:<#(id)#>];
五、NSDirectory和NSMutableDictionary
顾名思义,这是个字典,里面存储的数据结构为键值对的形式;
字典:
key ----> value
索引 ----> 文字内容
里面存储的东西都是键值对
创建方式:
// NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];
// NSArray *keys = @[@"name", @"address"];
// NSArray *objects = @[@"jack", @"北京"];
// NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
/*
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"jack", @"name",
@"北京", @"address",
@"32423434", @"qq", nil];*/
NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京"};
// id obj = [dict objectForKey:@"name"];
id obj = dict[@"name"];
NSLog(@"%@", obj);
// 返回的是键值对的个数
NSLog(@"%ld", dict.count);
NSMutableDictionary的使用:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 添加键值对
[dict setObject:@"jack" forKey:@"name"];
[dict setObject:@"北京" forKey:@"address"];
[dict setObject:@"rose" forKey:@"name"];
// 移除键值对
// [dict removeObjectForKey:<#(id)#>];
NSString *str = dict[@"name"];
六、NSNumber和NSValue
它可以将一个基本数据类型装换成OC对象类型;我们知道在NSArray和NSDirectory中都不能使用基本数据类型,于是乎,它的作用就来了。
将基本数据类型转换成NSNumber类型:
<span style="font-size:14px;">NSNumber *num = [NSNumber numberWithInt:10];</span>
<span style="font-size:14px;"> // 将各种基本数据类型包装成NSNumber对象
@10.5;
@YES;
@'A'; // NSNumber对象
@"A"; // NSString对象</span>
将age变量包装成NSNumber对象
int age = 100;
@(age);
<span style="font-size:14px;">[NSNumber numberWithInt:age];</span>
NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue
结构体--->OC对象
<span style="font-size:14px;"> CGPoint p = CGPointMake(10, 10);
// 将结构体转为Value对象
NSValue *value = [NSValue valueWithPoint:p];</span>
将value转为对应的结构体
[value pointValue];
NSArray *array = @[value ];
七、NSDate
我们在处理日期类的数据时,往往很棘手,用结构体操作实在略显蛋疼。于是乎,NSDate应运而生。
创建一个时间对象
NSDate *date = [NSDate date];
打印出的时候是0时区的时间(北京-东8区)
NSLog(@"%@", date);
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
[date2 timeIntervalSinceNow];
将日期转换成字符串:
<span style="font-size:14px;">// 创建一个时间对象
NSDate *date = [NSDate date];
// 打印出的时候是0时区的时间(北京-东8区)
NSLog(@"%@", date);
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
// 从1970开始走过的秒数
NSTimeInterval seconds = [date2 timeIntervalSince1970];
// [date2 timeIntervalSinceNow];</span>
将日期转换成日期类型:
<span style="font-size:14px;"> // 09/10/2011
NSString *time = @"2011/09/10 18:56";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
NSDate *date = [formatter dateFromString:time];
NSLog(@"%@", date);</span>