一Foundation简介
Foundation—基础框架。框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础。Foundation框架中的类都是以NS为前缀(Next Step的缩写)
二Foundation常用类示例
1)NSString类
字符串比较函数:compare 它区分大小写
//示例代码
NSString a = @"abc";
NSString b = @"acd";
NSComparisonResult s = [a compare:b];
前后缀:hasPrefix hasSuffix
//示例代码
NSString a = @"www.baidu.com";
NSString b = @"xing.jpg";
if ([a hasPrefix:@"www."])//返回BOOL值
NSLog(@"YES");
else
NSLog(@"NO");
if([b hasSuffix:@".jpg"])//返回BOOL值
NSLog(@"YES");
else
NSLog(@"NO");
字符串查找:rangeOfString 查找字符串首次出现的位置
NSString a = @"www.baidu.com";
NSString b = @"baidu";
NSRange range = [a rangeOfString:b];
NSLog(@"%lu,%lu",range.location,range.lenth);
字符串截取和替换:substringFromIndex 从index处截取到最后(包含index位置)
substringToIndxe 从头截取到index这个位置(不包含index位置)
substringWithRange //截取一个范围的字符串
stringByReplacingOcurrenceOfString: a withString b
//查找a并将a替换成b
获取每个字符串的字符:characterAtIndex: //获取在index处的字符
NSString *string = @"abcdefg";
unsigned char c ;
for( int i = 0 ;i < string.length; i ++)
{
c = [string characterAtIndex:i];
printf("%c\t",c);
}
将字符串换成基本数据类型:intValue,floatValue,doubleValue
NSString *num = @"12";
int number = [num intValue];
NSLog(@"%d",number);
将C字符串转换成OC字符串:stringWithUTF8String
char *s = "abc";
NSString *str = [NSString stringWithUTF8String:s];
NSLog(@"%@",str);
将OC字符串转换成C字符串:UTF8String
NSString *str = @"abc";
char *s = [str UTF8String];
NSLog(@"%s",s);
可变字符串NSMutableString: 占用的空间和位置都可以改变
初始化:stringWithFormat: //用字符串进行格式化
appendString: //在尾部附加字符串
NSMutableString string //创建空可以字符串
NSMuatbleString *a = [NSMuatbleString stringWithForamt:@"abc"];
[a appendString:@"def"];
NSLog(@"%@",a);
可变字符串其它功能:
deleteCharactersInRange: //删除某一范围的字符串
insertString:a AtIndex: b //在索引位置b处插入字符串a
replaceCharactersInRange:a withString :b//将a范围的字符串用b替换
2)NSArray类
创建数组方法
NSArray *a1=[NSArray array]; //空数组
NSArray *a2=[NSArray arrayWithObject:@"abc"];//以一个对象填充数组
NSArray *a3=[NSArray arrayWithObjects:@"abc",@"def",nil];//以多个对象填充数组
NSArray *a4=[[NSArray alloc] initWithObjects:@"1",@"w",nil];
NSArray *a5=[NSArray arrayWithArry:a4];//以数组填充数组
数组遍历
1 是通过下标
2 是快速枚举
for(NSString *a int arr)
{
NSLog(@"%@",a);
}
3 通过block
[arr enumerateObjectsWithBlock:^(id obj,NSUIteger idx,BOOL *stop)]
{
NSLog("id = %ld,obj = %@",idx,obj);
}
其它功能:
arr writeToFile:path atomically: //将数组arr的内容写到path路径的文件中
arrayWithContensOfFile:path //将路径中的文件写到数组中
可变数组NSMuableArray
创建
NSMuableArray *a1 = [NSMuableArray array];
NSMuableArray *a2 = [NSMuableArray arrayWithObject:@"abc"];
NSMuableArray *a3 = [NSMuableArray arrayWithObjects:@"abc",@"def",nil];
NSMuableArray *a4 = [NSMuableArray arrayWithCapacity:6];
NSMuableArray 增删改查
insertObject: a AtIndex: b //在b位置插入a
removeObject:a //移除a
replaceWithObjectAtIndex: a withObject:b //用b将a替换
containsObject:a // 数组中是否包含a对象
exchangeWithObjectAtIndex:a withObjectAtIndex:b//a和了互换
3)NSDictionary类
通过 一个key,找到相应 的value;
创建对象
NSDictionary *d1 = [NSDictionary dictionary];
NSDictionary *d2 = [NSDictionary dictionaryWithObject:@"1" forKey:@"2"];
NSDictionary *d3 = [NSDictionary dictionaryWithObjectAndKeys:@"name1",@"1",@"name2",@"2",nil];
NSDictionary *d4 = @{@"1":@"name1",@"2":@"name2"};
NSLog(@"%d",d4.count);//输出字典长度
NSString *str = [d4 objectWithKey:@"1"];//找到对应key的value
for(NSString *str1 in d4)//遍历字典
{
NSLog(@"key = %d,value = %@",str1,[d4 objectWithKey:key]);
}
//通过block遍历字典
[d4 enumerateObjectsWithBlock:^(id obj,NSUIteger idx,BOOL *stop)]
{
NSLog("%@ = %@",idx,obj);
}
字典同样有文件操作,它类似array的文件操作。
写文件和array的名一样
读文件dictionaryWithContentsOfFile
NSMutableDictionary
//创建
NSMutableDictionary *d1 = [NSMutableDictionary dictionary];NSMutableDictionary *d2 = [NSMutableDictionary dictionaryWithCapacity:5];
//NSMutableDictionary 增删改查
setObject:a forKey:b //修改值,增加值
removeObjectForKey:a //根据a 删除value
containsObject:a //查找
4)NSFileManager文件管理类
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isYes = [fm fileExistsAtPath:@"//Users//Desktop//a.txt"];//判断是否是文件或目录
BOOL isDir;
BOOL isYes = [fm fileExistsAtPath:@"//Users//Desktop" isDirectory:&isDir]; //判断 是否是目录
isYes = [fm isReadableFileAtPath:@"//Users//Desktop//a.txt" ];//判断是否可读
isYes = [fm isWritableFileAtPath:@"//Users//Desktop//a.txt" ];//判断是否可写
isYes = [fm isDeletableFileAtPath:@"//Users//Desktop//a.txt" ];//判断是否可删除
Dictionary *dic = [fm attributesOfItemAtPath:@"//Users//Desktop"];/获取文件属性
NSArray *arr = [fm subpathsAtPath:@"//Users//Desktop"];//获取本目录及子目录
NSArray *arr1 = [fm contentsOfDirectoryAtPath:@"//Users//Desktop"];//获取本目录
5)NSNumber类
它是OC中处理数值类型的一个类
int a = 10;
float c = 3.23f;
double d = 2.34;
NSNumber *intObj = [NSNumber numberWithInt:a];//int a-->obj
NSNumber *floatObj = [NSNumber numberWithFloat:f1];//float c -->obj
NSNumber *dObj = [NSNumber numberWithDouble:d];//double d -->obj
5)NSValue类
将结构体转换为对象
CGPoint p1 = CGPointMake(20, 20);
NSMutableArray *arr = [NSMutableArray array];
NSValue *pointVal = [NSValue valueWithPoint:p1];//p1-->obj
6)NSDate类
日期类
NSDate *d1 = [NSDate date];//得到现在时间
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss"; //格式化显示时间
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:24];//时间差方法1
formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss"; //格式化显示时间
NSDate *zt = [now addTimeInterval:-t];//时间差方法2
formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:ss"; //格式化显示时间
Foundation中类当然不止这些,列举的都是常用的。