移动开发(IOS) – Objective-C-04-Foundation框架
1.Foundation框架类结构图
2.Foundation常用类
3.NSObject的常用方法
3.1.比较两个对象是否为同一个对象(指针是否指向同一个内存区域)
1
|
- (
BOOL
)isEqual:(
id
)object;
|
3.2.调用一个方法
1
|
- (
id
)performSelector:(
SEL
)aSelector;
|
1
2
|
//调用一个方法,并且传递1个参数
- (
id
)performSelector:(
SEL
)aSelector withObject:(
id
)object;
|
1
2
|
//调用一个方法,并且传递2个参数
- (
id
)performSelector:(
SEL
)aSelector withObject:(
id
)object1 withObject:(
id
)object2;
|
1
2
|
//延迟调⽤方法
- (
void
)performSelector:(
SEL
)aSelector withObject:(
id
)anArgument afterDelay:(
NSTimeInterval
)delay;
|
常用于延迟调用一个函数:
1
2
3
|
//延迟2.5秒调用methodName函数
[
self
performSelector:
@selector
(methodName) withObject:arg afterDelay:2.5]
[[
NSRunLoop
currentRunLoop] run]
|
3.3.某一个对象是否派生或属于某一类
1
2
|
//判断某一个对象是否是某类或其子类创建的
- (
BOOL
)isKindOfClass:(Class)aClass;
|
3.4.某一个对象是否属于某类
1
2
|
//判断某一个对象是否是某类创建的
- (
BOOL
)isMemberOfClass:(Class)aClass;
|
3.5.某对象是否实现某协议
1
2
|
//判断某个对象是否实现了某个协议
- (
BOOL
)conformsToProtocol:(Protocol)aProtocol;
|
3.6.某对象是否响应指定的方法
1
2
|
//判断某一个对象是否实现了某个方法
- (
BOOL
)respondsToSelector:(
SEL
)aSelector;
|
3.7.返回指定对象的父类和本类
1
2
|
- (Class)superclass;
- (Class)
class
;
|
4.字符串
4.1.NSString 实例,为不可变字符串(一旦被创建,就不可以再修改)。NSMutableString 实例,为可变字符串(创建后可以被修改)。
4.2.字符串的创建
4.2.1.创建一个字符串常量
1
|
NSString
*string =
@"这是一个字符串常量"
;
|
4.2.2.创建一个空的字符串
1
|
NSString
*string = [[
NSString
alloc] init];
|
1
|
NSString
*string = [
NSString
string];
|
4.2.3.快速创建一个字符串
1
2
|
NSString
*string1 = [[
NSString
alloc] initWithString:
@"字符串1"
];
NSString
*string2 = [[
NSString
alloc] initWithFormat:
@"%@,字符串2,%d,%s"
,string1,3,
"C语言中的字符串"
];
|
1
2
3
|
//调用类方法创建字符串
NSString
*string1 = [
NSString
stringWithString:
@"字符串1"
];
NSString
*string2 = [
NSString
stringWithFormat:
@"%@,字符串2,%d,%s"
,string1,3,
"C语言中的字符串"
];
|
4.3.比较字符串
4.3.1.测试字符串内容是否相同
1
2
3
4
5
|

NSString
*string1 = [[
NSString
alloc] initWithFormat:
@"docoder"
];
NSString
*string2 = [[
NSString
alloc] initWithFormat:
@"docoder"
];
if
([string1 isEqualToString:string2]) {
NSLog
(
@"字符串相等"
);
}
|
4.3.2.测试字符串是否为同一个对象(内存地址是否相同)
1
2
3
4
5
|

NSString
*string1 = [[
NSString
alloc] initWithFormat:
@"docoder"
];
NSString
*string2 = [[
NSString
alloc] initWithFormat:
@"docoder"
];
if
(string1 == string2) {
NSLog
(
@"为同一个对象"
);
}
|
4.3.3.比较字符串的先后顺序
返回 NSComparisonResult 枚举: NSOrederedAscending, NSOrederedSame, NSOrederedDescending
1
2
3
4
|

NSString
*string1 = [[
NSString
alloc] initWithFormat:
@"a"
];
NSString
*string2 = [[
NSString
alloc] initWithFormat:
@"b"
];
//caseInsensitiveCompare 不区分大小写比较
NSLog
(
@"[string1 caseInsensitiveCompare:string2] : %ld"
, [string1 caseInsensitiveCompare:string2]);
|
1
2
3
4
5
6
|

NSString
*content =
@"Hello"
;
NSComparisonResult
result1 = [content compare:
@"hello"
];
// 区分⼤小写 NSLiteralSearch
NSComparisonResult
result2 = [content compare:
@"hello"
options:
NSLiteralSearch
];
// 不区分⼤小写 NSCaseInsensitiveSearch
NSComparisonResult
result3 = [content compare:
@"hello"
options:
NSCaseInsensitiveSearch
range:
NSMakeRange
(0, 5)];
|
4.3.5.求字符串的长度
1
2
|

NSString
*string = [[
NSString
alloc] initWithFormat:
@"string length"
];
NSUInteger
*length = [string length];
|
4.4.字符串的转换
4.4.1.改变字符串大小写
1
2
3
4
|

NSString
*hello =
@"hello WORLD"
;
NSLog
(
@"%@"
, [hello uppercaseString]);
// 全部大写
NSLog
(
@"%@"
, [hello lowercaseString]);
// 全部小写
NSLog
(
@"%@"
, [hello capitalizedString]);
// ⾸字母大写,其他字母变⼩写
|
4.4.2.将字符串转换成基本数据类型
1
2
3
4
5
|

NSString
*string =
@"2.467"
;
NSLog
(
@"%d"
, [string boolValue]);
// 转换成BOOL类型
NSLog
(
@"%f"
, [string floatValue]);
// 转换成浮点型
NSLog
(
@"%f"
, [string doubleValue]);
// 转换成双精度型
NSLog
(
@"%d"
, [string intValue]);
// 转换成整型
|
4.4.3.用指定字符串,分割字符串,转换为数组
1
2
|

NSString
*string =
@"One Two Three Four"
;
NSArray
*array = [string componentsSeparatedByString:
@" "
];
|
4.5.字符串的截取与拼接
4.5.1.截取字符串
1
2
3
4
5
|

NSString
*string = [
NSString
stringWithFormat:
@"abcdef"
];
// 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString
*string1 = [string substringToIndex:2];
// 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString
*string2 = [string substringFromIndex:2];
|
4.5.2.根据范围截取字符串
1
2
3
4
|

NSRange
rang;
rang.location = 2;
rang.length = 2;
NSString
*string2 = [string substringWithRange:rang];
|
4.5.3.拼接字符串
1
2
3
4
|

NSString
*str1 =
@"1"
, *str2 =
@"2"
;
NSString
*string = [[
NSString
alloc] initWithFormat:
@"这是拼接:%@ and %@"
, str1, str2];
NSString
*string1 = [str1 stringByAppendingFormat:
@"%@"
, str2];
NSString
*string2 = [str1 stringByAppendingString:str2];
|
4.6.字符串查找
1
2
3
4
5
|
NSString
*link =
@"balabalatarget=_blankbalabalabala"
;
NSRange
range = [link rangeOfString:
@"target=_blank"
];
if
(range.location !=
NSNotFound
) {
NSLog
(
@"string 找到了"
);
}
|
4.7.字符串优化
1
2
3
4
5
|

NSString
*str1 =
@"docoder"
;
NSString
*str2 =
@"docoder"
;
if
(str1==str2){
//YES,在内存中的常量区,不会重复创建相同的对象,都指向一个对象。
NSLog
(
@"为同一个对象"
);
}
|
1
2
3
4
5
|

NSString
*str1 = [[
NSString
alloc] initWithString:
@"docoder"
];
NSString
*str2 = [[
NSString
alloc] initWithString:
@"docoder"
];
if
(str1==str2){
//YES,在内存中的常量区,不会重复创建相同的对象,都指向一个对象。
NSLog
(
@"为同一个对象"
);
}
|
1
2
3
4
5
|

NSString
*str1 = [[
NSString
alloc] initWithFormat:
@"docoder%@"
,
@".com"
];
NSString
*str2 = [[
NSString
alloc] initWithFormat:
@"docoder%@"
,
@".com"
];
if
(str1!=str2){
//NO,在内存中的堆区,为两个不同对象。
NSLog
(
@"为两个不同对象"
);
}
|
4.8.可变字符串
4.8.1.NSMutableString 是可变字符串,可以修改字符串中的内容,NSMutableString继承自NSString, NSString所有的方法都能使用。
4.8.2.可变字符串不能用字符串常量的方式定义。
1
|

NSMutableString
*str =
@"这是错误的!"
;
//错误
|
4.8.3.插入字符串
1
2
|

NSMutableString
*str = [
NSMutableString
stringWithString:
@"字符串"
];
[str insertString:
@"可变"
atIndex:0];
|
4.8.4.删除字符串
1
2
|

NSMutableString
*str = [
NSMutableString
stringWithString:
@"字符串"
];
[str deleteCharactersInRange:
NSMakeRange
(1, 2)];
|
4.8.5.替换字符串
1
2
|

NSMutableString
*str = [
NSMutableString
stringWithString:
@"字符串"
];
[str replaceCharactersInRange:
NSMakeRange
(0, 2) withString:
@"⽺肉"
];
|
5.数字对象常创建与转换
OC中提供了数字对象 NSNumber 可以将基本数据类型包装成对象
5.1.数字对象的初始化
1
2
3
4
5
6
7
8
9
10
|

// 类⽅法创建数字对象
int
number = 10;
NSNumber
*intNumber = [
NSNumber
numberWithInt:number];
BOOL
isBool =
YES
;
NSNumber
*boolNumber = [
NSNumber
numberWithBool:isBool];
// 实例⽅法创建数字对象
float
pi = 3.14;
NSNumber
*piNumber = [[
NSNumber
alloc] initWithFloat:pi];
char
character =
'a'
;
NSNumber
*cNumber = [[
NSNumber
alloc] initWithChar:character];
|
5.2.数字对象的转换
1
2
3
4
5
|

// 还原成基本数据类型
int
month = [intNumber intValue];
BOOL
isOpen = [boolNumber boolValue];
float
length = [piNumber floatValue];
char
firstName = [cNumber charValue];
|
5.3.数字对象的新写法
1
2
3
4
|
NSNumber
*intNumber =
@10
;
NSNumber
*boolNumber =
@YES
;
NSNumber
*piNumber =
@3
.14;
NSNumber
*cNumber = @
'a'
;
|
6.NSValue 和 NSNull
6.1.NSValue 可以将任意值(如结构体、指针等)包装为对象,为NSNumber的父类。
1
2
3
4
5
|
//包装
NSRange
range = {1,2};
NSValue
*value = [
NSValue
valueWithRange:range];
//解包
NSRange
range=[value rangeValue];
|
1
2
3
4
5
|
//包装
struct
Test test;
NSValue
*value = [
NSValue
value: &test withObjCType:
@encode
(
struct
Test)];
//解包
struct
Test t=[value getValue: &test];
|
6.2.用NSNull代替,表示什么都不是,什么都没有,即:空。
7.NSData
7.1.NSData是对数据的一种抽象。
7.2.任何数据都可以通过NSData来存储,NSMutableData是可变的,继承于NSData:
1
2
3
|
NSString
*str =
@"docoder"
;
NSDate
*data = [str dataUsingEncoding:
NSUTF8StringEncoding
];
NSString
*str2 = [[
NSString
alloc] initWithData:data encoding:
NSUTF8StringEncoding
]
|
8.数组
8.1.NSArray ,为不可变数组,NSMutableArray,为可变数组。
8.2.数组中不可以存放基本数据类型,只能存放类的实例(对象)。如若需要将基本数据类型、结构体放入数组中,需要通过 NSNumber、NSValue 进行数据的“封装”。同时不能在 NSArray 中存储 nil (对象的零值或 NULL 值),可以存储 NSNull 表示空。建议只存相同类型的对象。
8.3.不可变数组:NSArray
8.3.1.初始化
1
2
3
4
5
6
|

NSArray
*firstArray = [
NSArray
arrayWithObject:
@"one"
];
// 多个元素初始化时,注意以nil作为数组的结束
NSArray
*secondArray = [
NSArray
arrayWithObjects:
@"one"
,
@"two"
,
nil
];
NSArray
*thirdArray = [
NSArray
arrayWithArray:secondArray];
//只是变换指针(将数组元素存入)
NSArray
*FourthArray = [
NSArray
arrayWithContentsOfFile:path];
NSArray
*FifthArray = @[
@"one"
,
@"two"
,firstArray,@[
@"one"
,
@"two"
],
@123
];
//新写法
|
8.3.2.常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|

NSArray
*array = [
NSArray
arrayWithObjects:
@"a"
,
@"b"
,
@"c"
,
nil
];
// 获取数组中的元素个数
NSInteger
count = [array count];
//等价于 array.count;
// 根据下标访问对象
NSString
*firstObj = [array objectAtIndex:0];
// 在原来的数组上追加对象,返回一个新的数组对象
NSArray
*addArray = [array arrayByAddingObject:
@"ddd"
];
// ⽤指定的字符串将数组中的元素连接起来
NSString
*arrayStr = [array componentsJoinedByString:
@","
];
// 数组中是否包含某对象
BOOL
isContain = [array containsObject:
@"bbb"
];
// 查询指定对象在数组中的元素, 如果没此元素,返回NSNotFound
NSInteger
index = [array indexOfObject:
@"ccc"
];
// 获取数组中最后一个元素
NSString
*lastString = [array lastObject];
|
8.4.可变数组:NSMutableArray,它继承自NSArray
8.4.1.可变数组( NSMutableArray )的新写法:
1
|
NSMutableArray
*mutableArray = [@[
@"123"
,
@456
,@[@
'a'
,
@YES
]] mutableCopy];
|
8.4.2.常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|

// 初始化,设定元素个数为5,但可以更改的
NSMutableArray
*mutableArray = [
NSMutableArray
arrayWithCapacity:5];
// 向数组中添加一个元素
[mutableArray addObject:
@"aaa"
];
// 向数组中指定下标插入元素
[mutableArray insertObject:
@"ccc"
atIndex:0];
// 移除最后一个元素
[mutableArray removeLastObject];
// 移除指定元素
[mutableArray removeObject:
@"aaa"
];
// 移除指定下标的元素
[mutableArray removeObjectAtIndex:0];
// 向数组中添加数组
[mutableArray addObjectsFromArray:array];

// 替换指定的下标元素
[mutableArray replaceObjectAtIndex:0 withObject:
@"replace"
];
// 移除所有对象
[mutableArray removeAllObjects];
|
8.5.遍历数组
8.5.1.常规方法
1
2
3
4
5
6
|

NSArray
*array = [
NSArray
arrayWithObjects:
@"a"
,
@"b"
,
@"z"
,
nil
];
int
length = [array count];
for
(
int
i = 0; i < length; i++) {
NSString
*element = [array objectAtIndex:i];
NSLog
(
@"%@"
, element);
}
// 遍历的性能较低
|
8.5.2.快速枚举
1
2
3
|

for
(
NSString
*string in array) {
NSLog
(
@"found %@"
, string);
}
// 遍历的性能⾼
|
1
2
3
|

for
(
id
obj in array) {
NSLog
(
@"found %@"
, obj);
}
// 当不确定数组的元素的类型时,可以选择用id
|
9.集合
9.1.NSSet ,为不可变集合。NSMutableSet,为可变集合。
9.2.不可变集合:NSSet
9.2.1.初始化
1
2
3
4
5
6
7
|

// 类似与数组的构建,直接创建一个集合
NSSet
*set1 = [[
NSSet
alloc] initWithObjects:
@"one"
,
@"two"
,
nil
];
// 通过数组的构建集合
NSArray
*array = [
NSArray
arrayWithObjects:
@"1"
,
@"2"
,
@"3"
,
nil
];
NSSet
*set2 = [[
NSSet
alloc] initWithArray:array];
// 通过已有集合构集合
NSSet
*set3 = [[
NSSet
alloc] initWithSet:set2];
|
9.2.2.常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|

// 集合1中的对象个数
int
count = [set1 count];
// 以数组的形式返回集合1中所有的对象
NSArray
*allObjects = [set1 allObjects];
// 返回集合1中的任意一个对象
id
object = [set1 anyObject];
// 集合2中是否包含内容为2的字符串对象,如果包含返回YES,否则为NO
BOOL
isContain = [set2 containsObject:
@"2"
];
// 集合2中与集合1中是否存在有相同元素的对象,如果有返回YES,否则为NO
BOOL
isIntersect = [set2 intersectsSet:set1];
// 集合2与集合1中的元素是否完全匹配,如果匹配返回YES,否则为NO
BOOL
isEqual = [set2 isEqualToSet:set1];
// 集合2是否是集合1的子集合,如果是返回YES,否则为NO
BOOL
isSubset = [set2 isSubsetOfSet:set1];
// 创建⼀个新的集合1,集合2有两个对象(a和b)
NSSet
*set1 = [
NSSet
setWithObjects:
@"a"
,
nil
];
NSSet
*set2 = [set1 setByAddingObject:
@"b"
];
// 通过已有集合3和集合4,创建一个新的集合5
NSSet
*set3 = [
NSSet
setWithObjects:
@"a"
,
nil
];
NSSet
*set4 = [
NSSet
setWithObjects:
@"z"
,
nil
];
NSSet
*set5 = [set3 setByAddingObjectsFromSet:set4];
// 通过已有的集合6和数组对象,创建一个新的集合7
NSArray
*array = [
NSArray
arrayWithObjects:
@"a"
,
@"b"
,
@"c"
,
nil
];
NSSet
*set6 = [
NSSet
setWithObjects:
@"z"
,
nil
];
NSSet
*set7 = [set6 setByAddingObjectsFromArray:array];
|
9.3.可变集合:NSMutableSet,它继承自NSSet
常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
NSMutableSet
*set1 = [
NSMutableSet
set]; 
// 创建一个空的集合
NSMutableSet
*set2 = [
NSMutableSet
setWithObjects:
@"1"
,
@"2"
,
nil
];
NSMutableSet
*set3 = [
NSMutableSet
setWithObjects:
@"a"
,
@"2"
,
nil
];
// 集合2“减去”集合3中的元素,集合2最后元素只有1个,且为:1
[set2 minusSet:set3];
// 集合2与集合3中元素的交集,集合2最后元素只有1个,且为:2
[set2 intersectSet:set3]
// 集合2与集合3中的元素的并集,集合2最后元素只有3个,且为:1,2,a
[set2 unionSet:set3];
// 将空集合1设置为集合3中的内容
[set1 setSet:set3];
// 将数组的内容添加到集合
NSArray
*array = [
NSArray
arrayWithObjects:
@"1"
,
@"2"
,
nil
];
[set2 addObjectsFromArray:array];
// 删除集合2中值为1的元素
[set2 removeObject:
@"1"
];
// 删除集合2中所有元素
[set2 removeAllObjects];
|
10.字典
10.1.NSDictionary ,为不可变字典。NSMutableDictionary,为可变字典。
10.2.通过key(键),查找对应 value(值),key通常是字符串对象,也可以是其他任意类型对象
10.3.在一个字典对象 中,key的值必须是唯一的。字典对象的键和值不可以为空(nil),如果需要在 一个字典对象中表示一个空值,可以使用NSNull对象。
10.4.不可变字典:NSDictionary
10.4.1.初始化
1
2
3
4
5
6
7
8
|

// 初始化两个元素
NSDictionary
*dic =[
NSDictionary
dictionaryWithObjectsAndKeys:numObj,
@"valueKey"
,numObj2,
@"valueKey2"
,
nil
];
// 初始化新字典,新字典包含 otherDic
NSDictionary
*dic = [
NSDictionary
dictionaryWithDictionary:otherDic];
// 以文件内容初始化字典
NSDictionary
*dic = [
NSDictionary
dictionaryWithContentsOfFile:path];
// 新写法
NSDictionary
*dic = @{
@"key1"
: @[
@11
,
@22
],
@"key2"
: @{
@"key11"
:
@"1111"
}};
|
10.4.2.常用方法
1
2
3
4
5
6
7
8
9
10
|

// 获取数量
NSInteger
count = [dic count];
// 通过key获取对应的value对象
NSObject
*valueObj = [dic objectForKey:
@"key"
];
// 将字典的key转成一个枚举对象,⽤于遍历
NSEnumerator
*enumerator = [dic keyEnumerator];
//获取所有键的集合
NSArray
*keys = [dic allKeys];
//获取所有值的集合
NSArray
*values = [dic allValues];
|
10.5.可变字典:NSMutableDictionary 继承自 NSDictionary
10.5.1.可变字典( NSMutableDictionary )的新写法:
1
|
NSDictionary
*dic = [@{
@"key1"
: @[
@11
,
@22
],
@"key2"
: @{
@"key11"
:
@"1111"
}} mutableCopy];
|
10.5.2.常用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 初始化一个空的可变字典
NSMutableDictionary
*dic1 = [
NSMutableDictionary
dictionaryWithObjectsAndKeys:
@"v1"
,
@"key1"
,
@"v2"
,
@"key2"
,
nil
];
NSDictionary
*dic2 = [
NSDictionary
dictionaryWithObject:
@"v3"
forKey:
@"key3"
];
// 向字典1对象中添加整个字典对象2
[dic1 addEntriesFromDictionary:dic2];
// 向字典1对象中追加一个新的key4和value4
[dic1 setValue:
@"value4"
forKey:
@"key4"
];
// 初始化⼀个空的可变字典
NSMutableDictionary
*dic2 = [
NSMutableDictionary
dictionary];
// 将空字典2对象内容设置与字典1对象相同
[dic2 setDictionary:dic1];
[dic1 removeObjectForKey:
@"key1"
];
// 将字典中key1对应的值删除
NSArray
*array = [
NSArray
arrayWithObjects:
@"key1"
,
nil
];
[dic2 removeObjectsForKeys:array];
// 根据指定的数组(key)移除字典2的内容
[dic1 removeAllObjects];
// 移除字典所有对象
|
10.6.遍历字典
10.6.1.快速枚举
1
2
3
4
|

for
(
id
key in dic) {
id
obj = [dic objectForKey:key];
NSLog
(
@"%@"
, obj);
}
|
10.6.2.一般的枚举
1
2
3
4
5
6
7
|

NSArray
*keys = [dic allKeys];
int
length = [keys count];
for
(
int
i = 0; i < length; i++) {
id
key = [keys objectAtIndex:i];
id
obj = [dic objectForKey:key];
NSLog
(
@"%@"
, obj);
}
|
10.6.3.通过枚举类型枚举
1
2
3
4
5
6
7
|

NSEnumerator
*enumerator = [dic keyEnumerator];
id
key = [enumerator nextObject];
while
(key) {
id
obj = [dic objectForKey:key];
NSLog
(
@"%@"
, obj);
key = [enumerator nextObject];
}
|
11.集合、数字对象的新写法
11.1.数组的新写法
1
2
3
4
5
6
7
|

NSArray
*array = @[
@"123"
,
@456
,
@678
];
id
element1 = array[1];
NSLog
(
@"element 1: %@"
, element1);
NSMutableArray
*mutableArray = [@[
@"123"
,
@456
] mutableCopy];
id
element2 = mutableArray[0];
NSLog
(
@"element 2: %@"
, element2);
|
11.2.字典的新写法
1
2
3
4
5
6
7
|
NSDictionary
*dic = @{
@"key1"
:
@0
,
@"key2"
:
@1
};
id
value1 = dic[
@"key1"
];
NSLog
(
@"value 1 : %@"
, value1);
NSMutableDictionary
*mutableDic = [@{
@"key3"
:
@1
,
@"key4"
:
@2
} mutableCopy];
id
value2 = mutableDic[
@"key3"
];
NSLog
(
@"value2 : %@"
, value2);
|
11.3.嵌套写法
1
2
3
4
|

NSArray
*newArray = @[
@123
,
@456
,
@789
];
NSArray
*nestArray = @[newArray,
@999
, @[
@11
,
@22
]];
NSDictionary
*newDic = @{
@"key10"
: @[
@11
,
@22
],
@"key11"
: nestArray};
NSMutableDictionary
*mutableNewDic = [@{
@"key12"
: newDic,
@"key13"
:
@123
} mutableCopy];
|
11.4.数字对象的新写法
1
2
3
4
5
|

NSNumber
*intNum =
@123
;
NSNumber
*floatNum =
@3
.14159f;
NSNumber
*charNum = @
'z'
;
NSNumber
*boolNum =
@YES
;
NSNumber
*number = @(12+num);
|
11.5.新语法创建的数组、集合,将被直接添加到自动释放池中。不需要用户手动释放, 如若需要长时间保留它,可以用retain或copy,延长对象的生命周期。
12.日期类
12.1.某一日期到1970年的秒数大小,称为该日期的时间戳。
12.2.NSDate,提供日期的创建、比较、计算时间间隔等功能,是最常用的类库之一。
常用处理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|

// 获取时间戳
NSTimeInterval
time2001 = [date timeIntervalSinceReferenceDate];
//以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔
NSLog
(
@"time since 2001: %f"
, time);
// GMT
NSTimeInterval
time1970 = [date timeIntervalSince1970];
//以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔
NSLog
(
@"time since 1970: %f"
, time1970);
// GMT
NSTimeInterval
timeNow = [date timeIntervalSinceNow];
//以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔
NSLog
(
@"time since now : %f"
, timeNow);
// GMT
// 昨天、今天、明天
NSTimeInterval
time = 24 * 60 * 60;
NSDate
*_yesterday = [[
NSDate
alloc] initWithTimeIntervalSinceNow:- time];
NSDate
*_today = [[
NSDate
alloc] initWithTimeIntervalSinceNow:0];
NSDate
*_tomorrow = [[
NSDate
alloc] initWithTimeIntervalSinceNow:time];
// 在未来的⼀个时间
NSDate
*feature = [
NSDate
distantFuture];
NSLog
(
@"feature : %@"
, feature);
// 在远古的一个时间
NSDate
*ancient = [
NSDate
distantPast];
NSLog
(
@"ancient : %@"
, ancient);
// ⽇期的比较
if
(![_yesterday isEqualToDate:_tomorrow]) {
NSLog
(
@"不同的一天"
);
}
// 返回一个较早的时间
NSDate
*earlierDate = [feature earlierDate:ancient];
NSLog
(
@"earlier Date : %@"
, earlierDate);
// 返回⼀个较晚的时间
NSDate
*laterDate = [feature laterDate:ancient];
NSLog
(
@"laterDate : %@"
, laterDate);
// ⽐较时间,方法1
NSComparisonResult
result = [_tomorrow compare:_today];
// ⽐较时间,方法2
if
([_tomorrow timeIntervalSince1970]>[_today timeIntervalSince1970]){
NSLog
(
@"_tomorrom > _today"
);
}
// 时间戳转换成字符串
NSString
*timeString =
@"1234567890"
;
NSDate
*convertDate = [
NSDate
dateWithTimeIntervalSince1970: [timeString doubleValue]];
NSLog
(
@"convertDate %@"
, convertDate);
//日期对象格式化为字符串
NSDate
*nowDate = [
NSDate
date];
NSDateFormatter
*dateFormatter = [[
NSDateFormatter
alloc] init];
dateFormatter setDateFormat:
@"yyyy年MM月dd日 HH:mm:ss"
];
//设置日期的格式
//----可省略(省略后默认为系统时区)----
NSTimeZone
*timezone = [
NSTimeZone
timeZoneWithName:
@"America/New_York"
];
//设置时区
[dateFormatter setTimeZone:timezone];
//--------------------------------
NSString
*datestring = [dateFormatter stringFromDate:nowDate];
//stringFromDate:将日期对象格式化为字符串
NSLog
(
@"格式化之后:%@"
,datestring);
//将字符串格式化成日期对象
NSString
*string =
@"2013年07月29日 16:56:05"
;
NSDateFormatter
*dateFormatter2 = [[
NSDateFormatter
alloc] init];
[dateFormatter2 setDateFormat:
@"yyyy年MM月dd日 HH:mm:ss"
];
NSDate
*formatDate = [dateFormatter2 dateFromString:string];
//dateFromString: 将字符串格式化成日期对象
NSLog
(
@"%@"
,formatDate);
NSDate
*date = [formatter dateFromString:dateString];
//获取到所有时区的名称
NSArray
*zoneNames = [
NSTimeZone
knownTimeZoneNames];
for
(
NSString
*name in zoneNames) {
NSLog
(
@"%@"
,name);
}
|
12.3.格式说明符号对照一栏表
d | 月中的某⼀天,一位数的日期没有前导零。 |
dd | 月中的某⼀天,一位数的日期有⼀个前导零。 |
ddd | 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。 |
dddd | 周中某天的完整名称,在 DayNames 中定义。 |
M | 月份数字,一位数的月份没有前导零。 |
MM | 月份数字,一位数的月份有一个前导零。 |
MMM | 月份的缩写名称,在 AbbreviatedMonthNames 中定义。 |
MMMM | 月份的完整名称,在 MonthNames 中定义。 |
y | 不包含纪元的年份,如果不包含纪元的年份小于10,则显示不具有前导零的年份。 |
yy | 不包含纪元的年份,如果不包含纪元的年份小于10,则显示具有前导零的年份。 |
yyyy | 包括纪元的四位数的年份。 |
gg | 时期或纪元,如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。 |
h | 12 ⼩时制的小时,一位数的小时数没有前导零。 |
hh | 12 小时制的小时,一位数的小时数有前导零。 |
H | 24 小时制的小时,一位数的小时数没有前导零。 |
HH | 24 小时制的小时,一位数的小时数有前导零。 |
m | 分钟,一位数的分钟数没有前导零。 |
mm | 分钟,一位数的分钟数有一个前导零。 |
s | 秒,一位数的秒数没有前导零。 |
ss | 秒,一位数的秒数有⼀个前导零。 |
f | 秒的⼩数精度为1位。其余数字被截断。 |
ff | 秒的⼩数精度为2位。其余数字被截断。 |
fff | 秒的⼩数精度为3位。其余数字被截断。 |
ffff | 秒的⼩数精度为4位。其余数字被截断。 |
fffff | 秒的⼩数精度为5位。其余数字被截断。 |
ffffff | 秒的⼩数精度为6位。其余数字被截断。 |
fffffff | 秒的⼩数精度为7位。其余数字被截断。 |
t | 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。 |
tt | 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。 |
z | 时区偏移量(“+”或“-”后面仅跟小时),一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。 |
zz | 时区偏移量(“+”或“-”后⾯仅跟小时),一位数的小时数有前导零。例如,太平洋标准时间是“-08”。 |
zzz | 完整时区偏移量(“+”或“-”后⾯跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。 |
: | 在 TimeSeparator 中定义的默认时间分隔符。 |
/ | 在 DateSeparator 中定义的默认日期分隔符。 |
% c | 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。 |
c | 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\”。 |
13.定时器(NSTimer)
定时器是让程序定时执行某一个方法:
1
2
3
4
5
6
7
8
|
/*
* scheduledTimerWithTimeInterval: 间隔时间(秒)
* target: 所要应用定时器的对象
* selector: 调用的方法
* userInfo: 所要传递的参数
* repeats: 是否重复调用
*/
[
NSTimer
scheduledTimerWithTimeInterval:1 target:
self
selector:
@selector
(timerAction:) userInfo:
@"参数"
repeats:
YES
];
|
1
2
3
4
5
6
|
- (
void
)timerAction:(
NSTimer
*)timer {
//传递的参数
NSString
*s = timer.userInfo;
index++;
NSLog
(
@"%d"
,index);
}
|
14.异常处理
14.1.使用@try @catch @finally处理异常
14.2.通过使用NSException, NSError以及自定义的异常处理类来处理这些异常。
14.3.使用规则:
14.3.1.如果这句或这部分代码有可能出现问题,就把它放在@try{}中。
14.3.2.@catch 捕获异常,出现了问题后,会执行到这里,然后你就可以对错误进 行另外的处理,比如记录日志或者提醒用户哪错了。
14.3.3.@finally 无论是否会抛出异常,这个块中的代码都会执行。
14.3.4.@throw 用来抛出一个异常。
14.4.例如,数组越界异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//创建一个空数组
NSArray
*arr = [
NSArray
array];
@try
{
//有可能出异常的代码块
//数组越界异常
[arr objectAtIndex:5];
}
@catch
(
NSException
*exception) {
//如果捕捉到错误,则会执行此处的代码
NSLog
(
@"错误:%@"
,exception);
}
@finally
{
//@finally是可选的
//不管有没有捕捉到异常,此处代码都会执行
NSLog
(
@"@finally"
);
}
|