iOS开发基础-Foundation框架

本文详细介绍了iOS开发中Foundation框架的基础知识,包括字符串、日期与时间、深浅复制、集合(NSArray, NSSet, NSDictionary)操作、谓词、文件I/O以及沙盒路径等内容。重点讲解了NSString、NSDate、NSMutableString、NSArray、NSSet、NSMutableDictionary的使用方法,以及对象归档、谓词表达式和沙盒目录结构。" 112733152,10544174,PPT时间轴制作秘籍:纵向布局的3个技巧,"['PPT设计', '图形设计', '演示文稿', '办公软件', '视觉呈现']
摘要由CSDN通过智能技术生成

目录

一、字符串

二、日期与时间

三、深复制与浅复制

四、集合概览

1.NSArray/NSMutableArray

2.NSSet/NSMutableSet

3.NSDictionary/NSMutableDictionary

五、谓词

六、文件I/O

1.NSDate/NSMutableData

2.NSFileManger

3.NSPathUtilities

4.NSProcessInfo

5.NSFileHandle

6.NSURL

7.NSBundle

8.对象归档

七、沙盒路径/相对路径

1.沙盒目录结构

2.获取各种目录的路径方法

3.NSSearchPathForDirectoriesInDomains


一、字符串

字符串就是一连串字符序列,OC中有两个字符串:NSString和NSMutableString。NSString代表字符序列不可变的字符串,NSMutableString代表字符序列可变的字符串。

//字符串创建方式,NSString
//使用Unicode数值数组初始化字符串
unichar data[6] = {97,98,99,100,101,102};
NSString *str1 = [[NSString alloc] initWithCharacters:data length:6];
NSLog(@"%@",str1);

//将C风格的字符串转为NSString对象
char *cstr = "hello ios";
NSString *str2 = [NSString stringWithUTF8String:cstr];
NSLog(@"%@",str2);

//将字符串写入指定文件
[str2 writeToFile:@"/Users/chuanxia/Desktop/OC/HelloWorld/Test/Test/myFile.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

//读取文件内容
NSString *str3 = [NSString stringWithContentsOfFile:@"/Users/chuanxia/Desktop/OC/HelloWorld/Test/Test/myFile2.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str3);

NSString的常用功能:

//字符串常用功能

//追加固定的字符串,将新生成的字符串重新赋给str指针变量
NSString *str = @"string";
str = [str stringByAppendingString:@"add string"];
NSLog(@"%@",str);

//转为C风格的字符串
const char *cstr = [str UTF8String];
NSLog(@"%s",cstr);

//追加带变量的字符串
str = [str stringByAppendingFormat:@"add times = %d",2];
NSLog(@"%@",str);

//获取字符个数
NSLog(@"%lu",[str length]);

//获取转为UTF-8字符集解码后的字节个数
NSLog(@"%lu",[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);

//获取字符串前十个字符组成的字符串
NSString *s1 = [str substringToIndex:10];
NSLog(@"%@",s1);

//获取字符串从第五个字符开始,与后面所有字符组成的字符串
NSString *s2 = [str substringFromIndex:5];
NSLog(@"%@",s2);

//获取字符串从第五个开始到第十个结束的子字符串
NSString *s3 = [str substringWithRange:NSMakeRange(5, 5)];
NSLog(@"%@",s3);

//获取add在str出现的位置
NSRange pos = [str rangeOfString:@"add"];
NSLog(@"%ld,%ld",pos.location,pos.length);

//将str转为大写
NSLog(@"%@",[str uppercaseString]);

NSMutableString与NSString类似,只是其长度内容可变。

 

二、日期与时间

NSDate代表日期与时间,NSDateFormatter代表日期格式转换器,可以设置日期的显示形式以及在NSDate与NSString之间的转换。NSCalendar为日历,NSDateComponents为日期组件,NSCalendar与NSDateComponents结合可以获取NSDate中的年月日等信息以及修改NSDate中某个类型的参数(如年月日等)。 NStimer为定时器。

NSDate:

//获取当前时间
NSDate *date1 = [NSDate date];
NSLog(@"%@",date1);

//获取当前时间开始,一天之后的时间
NSDate *date2 = [[NSDate alloc]initWithTimeIntervalSinceNow:3600*24];
NSLog(@"%@",date2);

//获取当前时间开始,三天前的时间
NSDate *date3 = [[NSDate alloc]initWithTimeIntervalSinceNow: -3*3600*24];
NSLog(@"%@",date3);

//获取从1970年1月1日开始,20年之后的日期
NSDate *date4 = [[NSDate alloc]initWithTimeIntervalSince1970:3600*24*366*20];
NSLog(@"%@",date4);

//获取当前的NSLocate
NSLocale *cn = [NSLocale currentLocale];
NSLog(@"%@",[date1 descriptionWithLocale:cn]);

//获取两个日期之间较早的日期
NSDate *earlier = [date1 earlierDate:date2];
NSLog(@"%@",earlier);

//获取时间差,单位为秒
NSLog(@"%g",[date1 timeIntervalSinceDate:date3]);

NSDateFormatter:

//创建两个地区格式
NSLocale *cn = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSLocale *us = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
//设置日期风格
[df1 setDateStyle:NSDateFormatterShortStyle];
/**
    设置日期风格:
            NSDateFormatterNoStyle:不显示日期、时间的风格;
            NSDateFormatterShortStyle:显示”短“日期、时间风格;
            NSDateFormatterMediumStyle:显示”中等“日期、时间风格;
            NSDateFormatterLongStyle:显示”长“日期、时间风格;
            NSDateFormatterFullStyle:显示”完整“日期、时间风格;
 */
//设置时间风格
[df1 setTimeStyle:NSDateFormatterFullStyle];

//设置地区风格
[df1 setLocale:cn];
//date类型转为字符串类型显示
NSLog(@"%@",[df1 stringFromDate:[NSDate date]]);

[df1 setLocale:us];
NSLog(@"%@",[df1 stringFromDate:[NSDate date]]);

//字符串类型转为date类型显示
NSString *dateStr = @"2020-09-09";
NSDateFormatter *df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:@"yyyy-MM-dd"];
NSDate *date2 = [df2 dateFromString:dateStr];
NSLog(@"%@",date2);

NSCalendar与NSDateComponents结合:

//获取时间内容
NSCalendar *cal = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate *dt = [NSDate date];

unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond | NSCalendarUnitWeekday;

NSDateComponents *comp = [cal components:unitFlags fromDate:dt];

//获取NSDate中的内容
NSLog(@"年:%ld",comp.year);
NSLog(@"月:%ld",comp.month);
NSLog(@"日:%ld",comp.day);
NSLog(@"小时:%ld",comp.hour);
NSLog(@"分钟:%ld",comp.minute);
NSLog(@"秒:%ld",comp.second);
NSLog(@"星期:%ld",comp.weekday);

//修改NSDate中的某部分信息
comp.year = 2028;
NSDate *date = [cal dateFromComponents:comp];
NSLog(@"获取的日期为:%@",date);

NSTimer:

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(task:) userInfo:nil repeats:YES];
/**
    TimeInterval:相隔多久执行一次,单位秒
    selector:执行的任务
    uerInfo:额外信息
    repeats:是否重复
 */


- (void)task:(NSTimer *)timer {
    NSLog(@"正在执行第%d次任务",_count++);
    if(_count > 10) {
        NSLog(@"取消执行定时器");
        [self.timer invalidate];
        //结束重复
    }
}

 

三、深复制与浅复制

NSObject类提供了copy和mutableCopy方法,通过这两个方法即可复制已有对象的副本,其中copy为不可变副本,mutableCopy是可变副本。

 

自定义类时,为了保证一个对象可以调用copy方法来复制自身的不可变副本,需要实现NSCopying协议,实现copyWithCopy方法。

为了保证一个对象可以调用mutableCopy方法来复制自身的可变副本,需要实现NSMutableCopying协议,实现mutableCopyWithZone方法。

 

浅复制与深复制:

浅复制指的是复制后的副本内容成员变量与原对象指向的区域还是一个位置,修改对应位置的数值,两者都会改变。深复制则是复制后的副本内容成员变量都需要重新创建,指向的位置不一样。

 

四、集合概览

1.NSArray/NSMutableArray

NSArray为不可变数组,代表元素有序、可重复的一个集合,NSMutableArray为可变数组,为NSArray的子类。

NSArray常用方法:

//创建数组
NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",nil];
//根据索引获取数据
NSLog(@"%@",[array objectAtIndex:2]);

//获取从索引为2的元素开始,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值