(一)框架:编程语⾔言中的框架指的是集成的一些⽅方法类库,按照一定的结构组织起来提供给开发⼈人员使⽤用,从⽽而降低开发难度提⾼高开发效率。
Cocoa框架: Cocoa本身是一个框架的集合,它包含了众多⼦子框架,其中最重要的要数 “Foundation”和“UIKit”
Foundation框架: 是用于开发Mac OS / iOS程序的基本类库,包含字符串、数组、集合、字典、日期......这些用于数据处理的基础类。
(二)NSObject类
1. 重要知识点
Ø NSObject类是OC中所有类的基类,也就是说所有的OC类都直接或间接继承这个类。
Ø NSObject类是OC中所有类的基类,也就是说所有的OC类都直接或间接继承这个类。
2. NSObject常用方法:
Ø + (instancetype)alloc; // 创建对象方法
Ø (instancetype)init; // 初始化⽅方法
Ø -(id)performSelector:(SEL)aSelector; // 调⽤用没有参数的方法
Ø -(id)performSelector:(SEL)aSelector withObject:(id)object;// 调用带有一个参数的方法
Ø -(BOOL)isKindOfClass:(Class)aClass; // 判断一个类是否是某个类或者它的父类
Ø -(BOOL)isMemberOfClass:(Class)aClass; // 判断一个类是否是某个类
Ø -(BOOL)respondsToSelector:(SEL)aSelector;// 判断一个方法是否是现实了
Ø + (Class)superclass; // 获取一个类的父类
(三)NSString字符串
1. NSString对象的创建
Ø NSString *s1 = @"哈哈哈";
Ø NSString *s2 = [NSStringstringWithFormat:@"我是字符串 %@",s1];
Ø NSString *s3 = [[NSStringalloc] initWithFormat:@"qwe,%d,%@",12,s2 ];
2. 字符串的比较
a) - (BOOL)isEqualToString:(NSString *)aString; //比较内容是否相同
b) ”==”比较是否是同一个对象(地址是否相同)。例如:if(s1 == s2)
c) - (NSComparisonResult)compare:(NSString *)string; //比较字符串的升序还是降序,首先比较两个字符串的首字母的ASCII码大小,被比较的字符串首字母ASCII码大,则为升序,反之降序。相同则进行下一个字母的比较,知道最后一个,依然相同,则一样(NSOrderSame);
NSComparisonResult:NSOrderedAscending(升序)、NSOrderedDescending(降序)、NSOrderedSame(一样/相同)
3. 字符串的大小写转换:
Ø @property (readonly, copy)NSString *uppercaseString;
Ø @property (readonly, copy)NSString *lowercaseString;
Ø @property (readonly, copy)NSString *capitalizedString;
4. 字符串à基本数据类型
Ø @property (readonly) doubledoubleValue;
Ø @property (readonly) floatfloatValue;
Ø @property (readonly) intintValue;
Ø @property (readonly) NSIntegerintegerValue;
Ø @property (readonly) long longlongLongValue;
Ø @property (readonly) BOOLboolValue;
5. 基本数据类型à字符串
Ø +(instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
6. 字符串的拼接(s1,s2,s3均为字符串)
Ø s3 = [NSStringstringWithFormat:@"%@ %@",s1,s2];
Ø s3 = [s1stringByAppendingString:s2];
Ø s3 = [s1stringByAppendingFormat:@"%@ %@",s2,@"---" ];
7. 字符串的查找和替换
Ø - (NSRange)rangeOfString:(NSString*)searchString;
NSNotFound:
if(r.location == NSNotFound){
//所需找的字符串没有找到
}
s3 = [s15stringByReplacingOccurrencesOfString:@"我" withString:@"ta"];
8. 字符串的截取
Ø - (NSString*)substringFromIndex:(NSUInteger)from;//取索引为from到结束的字符串
Ø - (NSString*)substringToIndex:(NSUInteger)to;//取从索引为0到索引为to的字符串
Ø - (NSString*)substringWithRange:(NSRange)range; //取指定范围内的字符串
Ø - (NSString*)characterAtIndex:(NSUInteger)index;//取指定索引位置的字符,也可为(char)用%c、%d都可输出。
9. 判断开头结尾
Ø - (BOOL)hasPrefix:(NSString*)str;//判断是否以某个字符串开头
Ø - (BOOL)hasSuffix:(NSString*)str;//判断是否以某个字符串结尾
(四)NSMutableString可变字符串
1. NSMutableString对象的创建
Ø NSMutableString *mms= @”224”;//错误,不能这样创建
Ø NSMutableString *mStr = [[NSMutableString alloc] initWithFormat:@”1234567”];
Ø NSMutableString *mStr1 = [[NSMutableString alloc] init];
Ø NSMutableString *mStr2 = [[NSMutableString alloc] initWithCapacity:10];//创建指定大小的字符串
2. NSMutableString常用方法
Ø insertString:atIndex: 在指定位置插入字符串
Ø deleteCharactersInRange: 删除指定位置的字符串
Ø appendString: 拼接指定字符串
Ø appendFormat: 接受格式符号的拼接
Ø setString: 设置字符串的内容
(五)NSNumber用于将节本数据类型保存为对象类型
3. 快捷创建方式
Ø NSNumber *num1 = @100;
Ø NSNumber *num2 = @34.1123;
Ø NSNumber *num3 = @NO;
Ø NSNumber *num4 = [[NSNumber alloc] initWithDouble:32.32133];
Ø NSNumber *num5 = [NSNumber numberWithBool:NO];
4. 转换为基本数据类型
Ø intValue NSNumber对象转换为整形
Ø doubleValue
Ø integerValue
(六)NSValue可以将结构体保存为对象类型
1. 注意:NSNumber 不能用于结构体的数据保存. NSValue可以将结构体保存为对象类型
2. 常用属性
Ø @property (readonly) NSPoint pointValue; NSValueà NSPoint
Ø @property (readonly) NSRange rangeValue; NSValueà NSRange
Ø @property (readonly)NSSize sizeValue;
Ø @property (readonly)NSRect rectValue;
Ø @property (readonly) NSEdgeInsets edgeInsetsValue;
3. 常用方法
Ø NSValue的实例方法 – getValue(void *)无返回值 NSValue à void *(任意指针类型) IOHIDDevicePlugIn.h
Ø + (NSValue*)valueWithRange:(NSRange)range; NSRangeà NSValue
Ø + (NSValue*)valueWithPoint:(NSPoint)point; NSPointà NSValue
Ø + (NSValue*)valueWithSize:(NSSize)size;
Ø + (NSValue*)valueWithRect:(NSRect)rect;
Ø + (NSValue *)valueWithEdgeInsets:(NSEdgeInsets)insets;
4. 封装自定义的结构为NSValue对象
struct Student
{
char*name;
int age;
long stuID;
}stu;
stu.name = "彭敬";
stu.age = 20;
stu.stuID = 201306181114;
char *StuType = @encode(structStudent);
NSValue *value_4 = [NSValuevalue:&stu withObjCType:StuType];
NSLog(@"%@",value_4);
5. 解析NSValue对象为结构体
struct Student stu_;
[value_4 getValue:&stu_];
NSString *stuName = [NSString stringWithUTF8String:stu_.name];
NSLog(@"%s",stu_.name);
NSLog(@"%@ %d %ld",stuName,stu_.age,stu_.stuID);
(七)NSArray类
1. 重点知识
Ø NSArray对象数组类型,只能存放对象类型,不能存放基本数据类型。如果要存放基本数据,只有通过NSNumber、NSString、NSValue
2. 创建方式
Ø NSArray *arr1 =[[NSArray alloc] init];
Ø NSArray *arr2 =[[NSArray alloc] initWithObjects:@"字符串",@"123",@100,nil];
Ø NSArray *arr3 =[[NSArray alloc] initWithArray:arr2];
Ø NSArray *arr4 = [NSArrayarrayWithObjects:@"字符串",@"123",@100,nil];
Ø +(instancetype)arrayWithObject:(ObjectType)anObject;
Person *per = [[Person alloc] init];
NSArray *arr5 = [NSArrayarrayWithObject:per];
Ø @[@””,…]
Person *perTest = [[Person alloc]init];
perTest.name = @"wo";
NSArray *arr6 = @[@"字符串",@"123",perTest,@100,@NO];
3. 常用属性
Ø @property (readonly) NSUInteger count;
Ø @property (nullable,nonatomic, readonly) ObjectType firstObject;
Ø @property (nullable,nonatomic, readonly) ObjectType lastObject;
4. 常用方法
Ø - (ObjectType)objectAtIndex:(NSUInteger)index; //根据下标获取元素
Ø - (NSUInteger)indexOfObject:(ObjectType)anObject; //获取指定元素所在的位置
Ø -(NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject; //在数组末尾添加一个元素
Ø - (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType>*)otherArray; //在数组末尾添加一个数组
Ø - (NSString *)componentsJoinedByString:(NSString *)separator; //用一个指定字符串把数组的所有元素连接成一个字符串
Ø - (BOOL)containsObject:(ObjectType)anObject; //判断数组中是否有指定对象
5. 注意:id为任意对象类型
6. 对象数组的遍历
a) 普通遍历(arr为对象数组 对象)
for(int i=0; i < arr6.count;i++)
{
NSLog(@"%@",arr6[i]);
}
b) 快速遍历
for(id i in arr6)
NSLog(@"%@",i);
c) 枚举遍历,把数组元素保存到枚举对象中
NSEnumerator *enumer = [arr6objectEnumerator];
id id_obj;
// nextObject 获取枚举对象中的下一个数据
while (id_obj = [enumernextObject]) {
NSLog(@"%@",id_obj);
}
d) block遍历
[arr6enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
NSLog(@"%ld:%@",idx,obj);
}];
(八)NSMutableArray(可变数组)
1. 重要知识
Ø 可以直接在原来的数组中操作(增删改查)不返回新的数组,不可变数组只能在最后添加元素
2. 可变数组的创建
Ø NSMutableArray *mArr1 =[[NSMutableArray alloc] init];
Ø NSMutableArray *mArr2 =[[NSMutableArray alloc] initWithObjects:@"123",@"字符串",nil];
Ø NSMutableArray *mArr3 =[NSMutableArray arrayWithObjects:@"123",@100, nil];
Ø NSMutableArray *mArr4 =[NSMutableArray arrayWithCapacity:10];
3. 常用方法
Ø - (void)addObject:(ObjectType)anObject; 在数组末尾添加一个元素
Ø -(void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray; 在数组末尾添加一个数组
Ø -(void)exchangeObjectAtIndex:(NSUInteger)idx1withObjectAtIndex:(NSUInteger)idx2; 交换两个元素的位置
Ø - (void)removeLastObject; 移除最后一个元素
Ø -(void)removeObjectAtIndex:(NSUInteger)index; 根据索引移除元素
Ø - (void)removeAllObjects; 移除所有元素
Ø -(void)removeObject:(ObjectType)anObject inRange:(NSRange)range; 在range范围中移除指定元素
Ø -(void)removeObject:(ObjectType)anObject; 移除指定元素
(九)NSDictionary(不可变字典)
1. 重要知识点
a) 字典是一种键值一一对应(keyàvalue)的存储结构,一个键(key)对应一个值(value),不可能存在一个(key)对应两个值或者同一个值对应两个键(key)
b) 通过键去获取对应的值
c) 键和值都是对象类型,不能使用基本数据类型,键一般是NSString类型
2. 字典的创建
Ø NSDictionary *dic =[[NSDictionary alloc]initWithObjectsAndKeys:@"value",@"key",@"value1",@"key1",nil];
Ø NSDictionary *dic1 =[NSDictionarydictionaryWithObjectsAndKeys:@"val",@"k",@"val1",@"k1",nil];
Ø NSDictionary *dic2 =[NSDictionary dictionaryWithObjectsAndKeys:arr,@"key1" ,nil]; //arr是NSArray类型
Ø NSDictionary *dic3 =@{@"key1":@"val1",@"key2":@"val2",@"key3":@"val1"};
3. 常用属性
Ø @property (readonly)NSUInteger count; //获取字典中键值对的个数
Ø @property (readonly, copy)NSArray<KeyType> *allKeys; //获取所有的键
Ø @property (readonly, copy)NSArray<ObjectType> *allValues; //获取所有的值
4. 常用方法
Ø - (nullableObjectType)objectForKey:(KeyType)aKey; //根据键获取值
Ø - (NSArray<KeyType>*)allKeysForObject:(ObjectType)anObject; //获取所有值为anObject对应的键
(十)NSMutableDictionary(可变字典)
1. 创建
Ø NSMutableDictionary*mDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"val1",@"key1",@"val2",@"key2",nil];
Ø NSMutableDictionary*mDic1 = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"val-1",@"key-1",@"val-2",@"key-2",nil];
2. 常用方法
Ø -(void)removeObjectForKey:(KeyType)aKey; //根据键移除元素
Ø -(void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey; //1、给字典添加元素;2、按照键来修改元素的值
Ø - (void)removeAllObjects; //移除所有元素
Ø -(void)removeObjectsForKeys:(NSArray<KeyType> *)keyArray; //根据键数组移除元素
Ø + (nullableNSMutableDictionary<KeyType, ObjectType>*)dictionaryWithContentsOfFile:(NSString *)path; //根据内容路径加载出字典对象,类方法
Ø + (nullableNSMutableDictionary<KeyType, ObjectType>*)dictionaryWithContentsOfURL:(NSURL *)url; //根据内容URL加载出字典对象,类方法
Ø - (nullableNSMutableDictionary<KeyType, ObjectType>*)initWithContentsOfFile:(NSString *)path; //根据内容路径加载出字典对象,实例方法
Ø - (nullable NSMutableDictionary<KeyType,ObjectType> *)initWithContentsOfURL:(NSURL *)url; //根据内容URL加载出字典对象,实例方法
3. 字典遍历(可变字典、不可变字典)(*mic 为可变字典)
a) 普通遍历
NSLog(@"第一种 普通遍历");
NSArray *arrKey = [mDic2allKeys];
for (int i=0;i<mDic2.count; i++) {
NSString *key = [arrKey objectAtIndex:i];
NSString *val = [mDic2 objectForKey:key];
NSLog(@"%@ = %@",key,val);
}
b) 快速遍历
NSLog(@"第二种 快速遍历");
for(id obj in mDic2)
{
//此处的obj为键
NSLog(@"%@ = %@",obj,[mDic2objectForKey:obj]);
}
c) 枚举遍历 keyEnumerator 拿到所有的key
NSEnumerator *enumer = [mDic2keyEnumerator];
id obj_;
NSLog(@"第三种 枚举类型遍历");
while (obj_ = [enumernextObject]) {
NSLog(@"%@ = %@",obj_,[mDic2objectForKey:obj_]);
}
d) block 遍历
NSLog(@"第四种,block遍历");
[mDic2enumerateKeysAndObjectsUsingBlock:^(id key,id val,BOOL *stop){
NSLog(@"%@ = %@",key,val);
}];
(十一) NSSet(不可变集合)
1. 集合跟数组最大的区别是
Ø 集合里面不能存在相同的元素,比如:@”a”,@”a”这样相同的字符是不可以一起保存到集合中,系统会自动保留唯一一个。
Ø 集合中的元素是无序的,即不可以使用set[2]这样的取值方式
2. 创建
Ø NSSet *set = [[NSSet alloc]init];
Ø NSSet *set1 = [[NSSet alloc]initWithObjects:@"123",@"字符串",@100, nil];
Ø NSSet *set2 = [[NSSet alloc]initWithArray:arr]; //arr是NSArray类型
Ø NSSet *set3 = [NSSetsetWithObjects:@"123",@100, nil];
3. 常用属性
Ø @property (readonly)NSUInteger count;
Ø @property (readonly, copy)NSArray<ObjectType> *allObjects;
4. 常用方法
Ø - (nullable ObjectType)member:(ObjectType)object; //指定输出元素,可以判断集合中是否有某元素
Ø -(BOOL)isEqualToSet:(NSSet<ObjectType> *)otherSet; //比较集合对象是否相同,返回布尔类型
Ø - (nullableObjectType)anyObject; //随机取得集合的任意一个元素
Ø - (NSSet<ObjectType>*)setByAddingObjectsFromSet:(NSSet<ObjectType> *)other; //给集合追加元素
Ø - (NSSet<ObjectType>*)setByAddingObjectsFromArray:(NSArray<ObjectType> *)other; //两个集合的并集,返回一个新的集合
(十二) NSMutableSet(可变集合)
一、 创建
Ø NSMutableSet *mSet =[[NSMutableSet alloc]initWithObjects:@"1",@"2",@"-",@100, nil];
Ø NSMutableSet *mSet1 = [NSMutableSetsetWithObjects:@"ha",@21,@NO,@"1",@"2", nil];
5. 常用方法
Ø -(void)addObject:(ObjectType)object; //无序添加一个元素
Ø -(void)addObjectsFromArray:(NSArray<ObjectType> *)array; //将数组中的每个元素添加到集合中
Ø -(void)intersectSet:(NSSet<ObjectType> *)otherSet; //求两个集合的交集
Ø - (void)minusSet:(NSSet<ObjectType>*)otherSet; //求两个集合的差集,集合中减去公共部分
Ø -(void)unionSet:(NSSet<ObjectType> *)otherSet; //求两个集合的并集,并保存到当前集合中去
Ø -(void)setSet:(NSSet<ObjectType> *)otherSet; //将otherSet集合中的元素替换当前集合的元素
Ø -(void)removeObject:(ObjectType)object; //移除指定的元素
Ø - (void)removeAllObjects; //移除集合中所有元素
6. 集合遍历(*mSet为集合对象)
a) 快速遍历
for(id obj in mSet3){
NSLog(@"%@",obj);
}
b) 利用枚举遍历
NSEnumerator *enumer = [mSet3 objectEnumerator];
id obj1;
while (obj1 = [enumer nextObject]) {
NSLog(@"-- %@",obj1);
}
c) block 遍历
[mSet3enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
NSLog(@"值: %@",obj);
}];
(十三) NSDate日期类
1. 获取当前时间,获取的0失去的时间跟北京时间相差8小时
NSDate *currDate = [NSDate date];// GMT 格林威治时间(世界标准时间)
2. 常见属性
Ø@property (readonly)NSTimeInterval timeIntervalSinceNow; //距离当前时间的秒数
Ø@property (readonly)NSTimeInterval timeIntervalSince1970; //当前时间到1970-1-1的秒数
Ø@property (readonly)NSTimeInterval timeIntervalSinceReferenceDate; //当前时间到2000-1-1的秒数
3. 常用方法
Ø - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti; //追加/添加秒数/时间戳(可为正数,也可为负数)
Ø - (BOOL)isEqualToDate:(NSDate *)otherDate; //比较两个日期是否相同
Ø - (NSComparisonResult)compare:(NSDate *)other; //比较两个日期,升序、相同、降序
Ø + (NSDate *)distantFuture; //未来的一个时间 4001-01-01
Ø + (NSDate *)distantPast; //远古的一个时间 0001-12-30
Ø + (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; //距离当前时间的秒数
Ø + (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; //距离当前时间的秒数
Ø + (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs; //当前时间到1970-1-1的秒数,返回NSDate
4. 计算北京时区的时间
NSDate *currDate = [NSDate date];//GMT 格林威治时间(世界标准时间)
NSTimeInterval interval2 = [[NSTimeZone systemTimeZone] secondsFromGMT];//0时区与系统时间相差的秒数
NSLog(@"%lf",interval2);
NSDate *date2 = [currDate dateByAddingTimeInterval:interval2];
NSLog(@"date2: %@",date2);
5. 时间格式化(将日期类型转化为时间字符串)
NSDate *dateNow = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = @"yyyy年M月d日 HH:mm:ss";
// stringFromDate 将日期类型格式化,转为NSString类型
NSString *strDateFormated = [dateFormat stringFromDate:dateNow];
NSLog(@"strDateFormated: %@",strDateFormated);
yyyy | MM | dd | HH | mm | ss | EEEE |
年 | 月 | 日 | 时 | 分 | 秒 | 星期 |
“M”与”MM”的区别:M – 7月 MM – 07月,其他符号类似
6. 将时间字符串转化为日期类型,可以实现自定义日期
dateFormat.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];// 默认使用0时区,所以需要时区的转换
NSDate *dateNow_ = [dateFormat dateFromString:strDateFormated];