Foundation中常用的几种数据类型
来源:互联网 发布:微整形后遗症知乎 编辑:程序博客网 时间:2024/05/01 23:14
1. NSString
1.1 不变模式
NSString类的对象在内存中的数据是不可改变的。
由于不可改变,对于NSString对象的内存管理,编译器会做一些优化
可以认为,在内存中,有一个字符串池,编译器将程序中使用的字符经常放入池中,池中的字符串对象不允许相同,如果需要池中相同的字符串对象,编译器会自动共享此对象。
1.2 创建字符串
创建空串
-init
+string
创建字符串
-initWithString
+stringWithString
@"...";
格式化创建字符串:
-initWithFormat
+stringWithFormat
不会在池中,在堆中创建
从文件创建字符串
-initWithContentsOfFile..
+stringWithContentsOfFile..
写入文件
writeToFile
1.3 比较字符串
3.3.1 相等
1)isEqualToString 比较两个字符串是否相等
2)compare 比较两个字符的大小
NSComparisonResult r;
NSOrderedAscending = -1; 前小后大
NSOrderedSame = 0; 相同
NSOrederedDescending = 1; 前大后小
3)caseInsensitiveCompare 忽略大小写比较两个字符串的大小
1.4 类型转换
各种类型转字符串
int double ... ==>NSString
NSString* str = [NSString stringWithFormat:@"%d", 100];
字符串转各种类型
intValue
doubleValue
floatValue
boolValue
integerValue
1.5 截取字符串
substringFromIndex
substringToIndex
substringWithRange
1.6 大小写转换
lowercaseString
uppercaseString
2. NSMutableString
NSString的子类,主要是内容可以改变。
append....
insert....
delete.....
replease...
3.NSString/NSMutableString
copy : NSMutableString ==> NSString
mutableCopy : NSString ==> NSMutableString
一:NSString和NSMutableString类型及方法的使用
NSString *str4 = @"abcd.txt";//判断是否包含
if([str4 rangeOfString:@"ab"].length>0) {
NSLog(@"包含ab字符串");
}
if([str4 hasPrefix:@"a"]){
NSLog(@"是以a开头");
}else NSLog(@"不是以a开头");
if([str4 hasSuffix:@"tx"]){//是否以某个字符串结尾通常判断文件的类型
NSLog(@"yes");
}else NSLog(@"NO");
//比较字符串是否一样
if([str4 compare:@"adsfasd"]==NSOrderedSame) {
NSLog(@"一样");
}else NSLog(@"不一样");
//判断字符串升序和降序
NSString*str5 = @"b";
if([str5 compare:@"a"]==NSOrderedAscending) {
NSLog(@"升序YES");
}else NSLog(@"NO");
if([str5 compare:@"a"]==NSOrderedDescending) {
NSLog(@"降序YES");
}else NSLog(@"NO");
// 判断字符串是否相等
if([str5 isEqualToString:@"a"]) {
NSLog(@"相等");
}else NSLog(@"不相等");
//替换字符串
// string = @"哈哈嘿嘿。。。";
NSString*tempString = [NSStringstringWithFormat:@"哈哈嘿嘿。。。"] ;
// 只要是通过返回值给全局变量赋值的都需要retain
string =[[tempString stringByReplacingOccurrencesOfString:@"嘿嘿"withString:@"呵呵"] retain];
NSLog(@"%@",string);
//把字符串写到文件当中
NSString*str = @"asodifjaosdifjaosdij";
NSError*err = nil;
[str writeToFile:@"/Users/apple/Desktop/a.txt"atomically:YESencoding:NSUTF8StringEncodingerror:&err];
//输出本地错误描述
if (err){
NSLog(@"%@",[errlocalizedDescription]);
}
//从文件当中读取字符串
NSString*newString = [NSStringstringWithContentsOfFile:@"/Users/apple/Desktop/a.txt"encoding:NSUTF8StringEncodingerror:nil];
NSLog(@"newString=%@",newString);
//分割字符串
NSString*str = @"abc&bbb&ccc&asdf";
NSArray*strs = [str componentsSeparatedByString:@"&"];
for (NSString *sinstrs) {
NSLog(@"s = %@",s);
}
//忽略大小写比较字符串
NSString*stringa = @"acdm";
NSString*stringb = @"AcdM";
if([stringa caseInsensitiveCompare:stringb] ==NSOrderedSame) {
NSLog(@"相等");
}
// BOOL result =[stringa caseInsensitiveCompare:stringb] == NSOrderedSame;把比较结果得到
// Yes = 1 NO= 0;
NSString*string1 = @"A String";
NSString*string = @"string is string";
NSLog(@"string1转小写 =%@",[string1lowercaseString]);
NSLog(@"string转大写 =%@",[stringuppercaseString]);
NSLog(@"首字母大写%@",[stringcapitalizedString]);
////去两端空格
// NSString *s =@"tarena ";
// NSLog(@"%d",s.length);
// s = [sstringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// NSLog(@"%ds=%@",s.length,s);
//// 去两端空格和回车
// s = [sstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
// //分割字符串和拼接
// NSString *s =@"张三李四王五赵六田七";
// NSArray *names = [scomponentsSeparatedByString:@" "];
//
// NSString *newString= [names componentsJoinedByString:@"*"];
// NSLog(@"string= %@",newString);
// 截取字符串
NSString*string = @"abcdefg";
// 从某个位置截取到最后
NSString*newString = [string substringFromIndex:3];
// 从开始截取到某个位置
// NSString *newString= [string substringToIndex:3];
// 截取一个范围
// NSString *newString= [string substringWithRange:NSMakeRange(3, 2)];
// NSLog(@"%@",newString);
// 可变的String
// NSMutableString *ms= [NSMutableString stringWithFormat:@"abc"];
// //在可变string里面追加字符串
// [msappendString:@"def"];
// [msappendFormat:@"%d----%@",30,@"lisi"];
//
// //不可变String怎么样追加字符串
// NSString *str =@"abc";
// NSString *newStr =[str stringByAppendingFormat:@"def"];
//
// NSLog(@"ms =%@ newStr = %@",ms,newStr);
// 删除指定的字符串
// NSMutableString *ms= [NSMutableString stringWithFormat:@"aaabbbccc"];
// [msdeleteCharactersInRange:NSMakeRange(6, 3)];
// NSLog(@"ms =%@",ms);
// 在指定的位置添加字符串
// NSMutableString *ms =[NSMutableString stringWithFormat:@"aaabbbccc"];
// [msinsertString:@"哈哈哈"atIndex:3];
//
// NSLog(@"%@",ms);
// 替换指定位置的字符串
NSMutableString*ms = [NSMutableStringstringWithFormat:@"aaabbbccc"];
[msreplaceCharactersInRange:NSMakeRange(3,3) withString:@"呵呵呵"];
附录:简洁版
创建字符串。
NSString *astring = @"This is a String!";
使用变量初始化
NSString *name = @"Ivan!";
NSString *astring =[[NSStringstringWithFormat:@”My name is %@!”,name]];
判断是否包含某字符串
检查字符串是否以另一个字符串开头-
(BOOL) hasPrefix: (NSString*) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1hasPrefix:@"NSString"] ==1 ? NSLog(@"YES") :NSLog(@"NO");
[String1hasSuffix:@".txt"] ==1 ? NSLog(@"YES") :NSLog(@"NO");
是否包含其它字符
NSString *astring =[[NSStringalloc]initWithString:@"This is aString!"];
Boolean contains =[astring rangeOfString:@”This”].length>0;
从文件读取字符串:initWithContentsOfFile方法
NSString *path = @"astring.text";
NSString *astring =[[NSStringalloc]initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astringrelease];
写字符串到文件:writeToFile方法
NSString *astring =[[NSStringalloc]initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astringwriteToFile:pathatomically:YES];
比较两个字符串
isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result =[astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
compare方法(comparer返回的三种值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result=[astring01compare:astring02]==NSOrderedSame;
NSLog(@"result:%d",result);
NSOrderedSame判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result=[astring01compare:astring02]==NSOrderedAscending;
NSLog(@"result:%d",result);
不考虑大小写比较字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result =[astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
改变字符串的大小写
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1uppercaseString]);//大写
NSLog(@"string2:%@",[string2lowercaseString]);//小写
NSLog(@"string2:%@",[string2capitalizedString]);//首字母大写
去除两端空格
NSString *temp =[textField.text stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
去除两端空格和回车
NSString *text =[temp stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet ]];
在串中搜索子串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRangerange =[string1 rangeOfString:string2];
intlocation =range.location;
int leight =range.length;
NSString *astring =[[NSStringalloc]initWithString:[NSStringstringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astringrelease];
替换字符串
NSString *astring01 = @"hello 中国";
NSString * new = [astring01stringByReplacingOccurrencesOfString:@”中国” withString:@"达内"];
NSLog(new);
分割字符串成数组
NSString *s =@"a b d e f";
NSArray *arr = [scomponentsSeparatedByString:@" "];
NSLog(@"count= %d",[arr count]);
字符串数组拼接成字符串
NSArray *pathArray = [NSArrayarrayWithObjects:@"here",
@"be",@"dragons", nil];
NSLog(@"%@",[pathArraycomponentsJoinedByString:@""]);
抽取子串
-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 =[string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 =[string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 =[string1 substringWithRange:NSMakeRange(0,4)];
NSLog(@"string2:%@",string2);
NSMutableString
在已有字符串后面添加字符
//appendString: and appendFormat:
NSMutableString *String1 =[[NSMutableStringalloc]initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding somecharacter"];
[String1appendFormat:[NSStringstringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
*/
在已有字符串中按照所给出范围和长度删除字符
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableStringalloc] initWithString:@"This is a NSMutableString"];
[String1deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"String1:%@",String1);
在已有字符串后面在所指定的位置中插入给出的字符串
//-insertString: atIndex:
NSMutableString *String1 =[[NSMutableStringalloc]initWithString:@"This is a NSMutableString"];
[String1insertString:@"Hi! "atIndex:0];
NSLog(@"String1:%@",String1);
将已有的换成其它的字符串
//-setString:
NSMutableString *String1 =[[NSMutableStringalloc]initWithString:@"This is a NSMutableString"];
[String1setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
按照所给出的范围,和字符串替换的原有的字符
//-setString:
NSMutableString *String1 =[[NSMutableStringalloc]initWithString:@"This is a NSMutableString"];
[String1replaceCharactersInRange:NSMakeRange(0,4)withString:@"That"];
NSLog(@"String1:%@",String1);
二:NSArray和NSMutableArray数组类型和方法的使用
// 不可变数组的创建 和判断是否包含某个数组
// NSArray *names =[[NSArray alloc]initWithObjects:@"张三",@"李四",@"王五",@"赵六", nil];
// if ([namescontainsObject:@"张三啊"]){
// NSLog(@"包含");
// }
// 可变长度的数组
NSMutableArray *names = [NSMutableArrayarray];
// 等同于
// [[NSMutableArray alloc]init];
[names addObject:@"张三"];
[names addObject:@"李四"];
[names insertObject:@"哈哈"atIndex:1];
for (NSString *nameinnames) {
NSLog(@"%@",name);
}
[names removeObject:@"张三"];
[names removeObjectAtIndex:1];
[names removeLastObject];
for (NSString *nameinnames) {
NSLog(@"%@",name);
}
NSArray*arr = [NSArrayarrayWithObjects:@"a",@"b",@"a",@"b",@"a",@"b",@"a",@"b",@"a",@"b",nil];
// 通过一个array创建一个Array
NSMutableArray*ma = [NSMutableArrayarrayWithArray:arr];
// 把数组写到文件当中
[arr writeToFile:@"/Users/apple/Desktop/arr"atomically:YES];
// 从文件当中读取数组
NSMutableArray*newArr=[NSMutableArrayarrayWithContentsOfFile:@"/Users/apple/Desktop/arr"];
NSLog(@"newArr = %@",newArr);
附录:简洁版
创建数组
NSArray *array =[[NSArray alloc] initWithObjects:
@"One",@"Two",@"Three",@"Four",nil];
[arrayrelease];
NSLog(@"self.dataArray cound:%d",[arraycount]);
获取指定索引处的对象;
NSLog(@"self.dataArray cound2:%@",[self.dataArrayobjectAtIndex:2]);
从一个数组拷贝数据到另一数组
//arrayWithArray:
//NSArray *array1 = [[NSArray alloc] init];
NSMutableArray *MutableArray =[[NSMutableArrayalloc]init];
NSArray *array =[NSArrayarrayWithObjects:
@"a",@"b",@"c",nil];
NSLog(@"array:%@",array);
MutableArray = [NSMutableArrayarrayWithArray:array];
NSLog(@"MutableArray:%@",MutableArray);
array1 = [NSArrayarrayWithArray:array];
NSLog(@"array1:%@",array1);
快速便利数组
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray =[NSArrayarrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(id objin oldArray)
{
[newArrayaddObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArrayrelease];
NSMutableArray
在数组末尾添加对象
//- (void) addObject: (id) anObject;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
[arrayaddObject:@"Four"];
NSLog(@"array:%@",array);
删除数组中指定索引处对象
//-(void) removeObjectAtIndex: (unsigned) index;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
[arrayremoveObjectAtIndex:1];
NSLog(@"array:%@",array);
三 NSDictionary和NSMutableDictionary类型和方法的使用
//创建一个只有一对儿数据的字典
// NSDictionary *dic = [NSDictionary dictionaryWithObject:@"张三" forKey:@"name"];
// NSString *name = [dic objectForKey:@"name"];
//创建一个多对儿数据的字典
NSDictionary *dic2 = [NSDictionarydictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3",nil];
NSArray *keys = [dic2 allKeys];
for (NSString *keyin keys) {
NSString *value = [dic2 objectForKey:key];
NSLog(@"key = %@ value = %@",key,value);
}
// 创建可变长度的字典
NSMutableDictionary *md = [NSMutableDictionarydictionary];
// 往字典里面添加数据
[md setObject:@"one"forKey:@"1"];
[md setObject:@"two"forKey:@"8"];
[md setObject:@"two"forKey:@"3"];
[md setObject:@"two"forKey:@"33"];
[md setObject:@"two"forKey:@"13"];
NSLog(@"md = %@",md);
[md removeObjectForKey:@"1"];
NSLog(@"md = %@",md);
//因为字典是无序的 如果想排序里面的内容的话只能让所有的key的数组排下序
附录:简洁版
NSDictionary
创建字典
//- (id) initWithObjectsAndKeys;
NSDictionary*dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
NSString *string =[dictionaryobjectForKey:@"One"];
NSLog(@"string:%@",string);
NSLog(@"dictionary:%@",dictionary);
[dictionaryrelease];
NSMutableDictionary
创建
NSMutableDictionary *dictionary =[NSMutableDictionarydictionary];
添加字典
[dictionarysetObject:@"One"forKey:@"1"];
[dictionarysetObject:@"Two"forKey:@"2"];
[dictionarysetObject:@"Three"forKey:@"3"];
[dictionarysetObject:@"Four"forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);
删除指定的字典
[dictionaryremoveObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);
四:NSValue/NSNumber类型和方法的使用
//value
// CGPoint p = CGPointMake(200, 200);
// CGRect r = CGRectMake(100, 100, 100, 100);
// NSValue *pValue = [NSValue valueWithCGPoint:p];
// NSValue *rValue = [NSValue valueWithCGRect:r];
//
// NSArray *arr = [NSArray arrayWithObjects:pValue,rValue, nil];
//
// CGPoint newP = [[arr objectAtIndex:0]CGPointValue];
// CGRect newR = [[arrobjectAtIndex:1]CGRectValue];
//number
int i = 10;
float b = 2.3;
BOOL c = YES;
NSArray *arr = [NSArrayarrayWithObjects:[NSNumbernumberWithInt:i],[NSNumbernumberWithFloat:b],[NSNumbernumberWithBool:c], nil];
int newI = [[arr objectAtIndex:0]intValue];
float newB = [[arr objectAtIndex:1]floatValue];
BOOL newC = [[arr objectAtIndex:2]boolValue];
附录:简洁版
NSValue(对任何对象进行包装)
将NSRect放入NSArray中
NSMutableArray *array =[[NSMutableArrayalloc]init];
NSValue *value;
CGRect rect = CGRectMake(0,0, 320,480);
value =[NSValuevalueWithBytes:&rectobjCType:@encode(CGRect)];
[arrayaddObject:value];
NSLog(@"array:%@",array);
从Array中提取
value =[arrayobjectAtIndex:0];
[valuegetValue:&rect];
NSLog(@"value:%@",value);
附录:简洁版
NSNumber
NSNumber
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber*)numberWithDouble:(double)value;
- (int)intValue;
- (double)doubleValue;
NSNumber可以将基本数据类型包装起来,形成一个对象,这样就可以给其发送消息,装入NSArray中等等。
NSNumber *intNumber=[NSNumber numberWithInt:100];
NSNumber*floatNumber=[NSNUmber numberWithFloat:100.00];
int i=[intNumber intValue];
if([intNumberisEqualToNumber:floatNumber]) ....
NSNumber继承NSObject ,可以使用比较compare: isEqual等消息
数学常用方法
数学常量:
#defineM_E 2.71828182845904523536028747135266250 // e #defineM_LOG2E 1.44269504088896340735992468100189214 // log 2e #defineM_LOG10E 0.434294481903251827651128918916605082 // log10e #define M_LN2 0.693147180559945309417232121458176568 // log e2 #defineM_LN10 2.30258509299404568401799145468436421 // log e10 #defineM_PI 3.14159265358979323846264338327950288 // pi #defineM_PI_2 1.57079632679489661923132169163975144 // pi/2 #defineM_PI_4 0.785398163397448309615660845819875721 // pi/4 #defineM_1_PI 0.318309886183790671537767526745028724 // 1/pi #defineM_2_PI 0.636619772367581343075535053490057448 // 2/pi #define M_2_SQRTPI 1.12837916709551257389615890312154517 // 2/sqrt(pi) #defineM_SQRT2 1.41421356237309504880168872420969808 // sqrt(2) #defineM_SQRT1_2 0.707106781186547524400844362104849039 //1/sqrt(2)
常用函数:
指数运算
NSLog(@"%.f", pow(3,2) );//result 9 NSLog(@"%.f", pow(3,3) ); //result 27
开平方运算 (计算两点间的距离时用到)
NSLog(@"%.f", sqrt(16) );//result 4 NSLog(@"%.f", sqrt(81) ); //result 9
上舍入
NSLog(@"res: %.f",ceil(3.000000000001)); //result 4 NSLog(@"res: %.f", ceil(3.00));//result 3
下舍入
NSLog(@"res: %.f",floor(3.000000000001)); //result 3 NSLog(@"res: %.f", floor(3.9999999));//result 3
四舍五入
NSLog(@"res: %.f",round(3.5)); //result 4 NSLog(@"res: %.f", round(3.46)); //result3 NSLog(@"res: %.f", round(-3.5)); //NB: this one returns -4
最小值
NSLog(@"res: %.f",fmin(5,10)); //result 5
最大值
NSLog(@"res: %.f", fmax(5,10));//result 10
绝对值
NSLog(@"res: %.f", fabs(10)); //result 10 NSLog(@"res: %.f", fabs(-10)); //result 10
五:NSData类型和方法的使用
NSData *data = [NSDatadataWithContentsOfFile:@"/Users/apple/Desktop/j0179010.jpg"];
NSLog(@"dataLength = %d",data.length);
[data writeToFile:@"/Users/apple/Desktop/111.jpg"atomically:YES];
//string data 互转
NSString *string = @"abcd";
NSData *strData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *newString = [[NSStringalloc]initWithData:strDataencoding:NSUTF8StringEncoding];
NSLog(@"newString =%@",newString);
附录:简洁版
NSDate
得到当前的日期
NSDate *date = [NSDatedate];
日期之间比较可用以下方法
-(BOOL)isEqualToDate:(NSDate *)otherDate;// 与otherDate比较,相同返回YES
- (NSDate*)earlierDate:(NSDate *)anotherDate;// 与anotherDate比较,返回较早的那个日期
- (NSDate*)laterDate:(NSDate *)anotherDate;//与anotherDate比较,返回较晚的那个日期
将日期转换成字符串
NSLog(@”date = %@”,[data description]);
设置日期显示格式
NSDateFormatter *formatter =[[[NSDateFormatteralloc] init] autorelease];
[formattersetTimeStyle:NSDateFormatterFullStyle]; //设置几种默认的显示效果
[formattersetDateFormat:@"yyyy-MM-dd HH:mm:ss a"];//设置自定义的显示效果
NSLog([formatter stringFromDate:date]);
NSData
NSData-> NSString
NSString *aString =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString= @"1234abcd";
NSData *aData =[aString dataUsingEncoding: NSUTF8StringEncoding];
六:NSFileManager文件类型和方法的使用
NSFileManager*fm = [NSFileManagerdefaultManager];
NSString *path = @"/Users/apple/Desktop/pig.png";
// 读取路径下文件的内容
NSData *fileData = [fm contentsAtPath:path];
NSLog(@"length =%d",fileData.length);
// 删除文件
NSError *err = nil;
if ([fm removeItemAtPath:patherror:&err]) {
NSLog(@"删除成功!!!");
}
// 复制文件
if ([fm copyItemAtPath:pathtoPath:@"/Users/apple/Desktop/1111.png"error:nil]) {
NSLog(@"复制成功!!!");
}
// 创建文件
if ([fm createFileAtPath:@"/Users/apple/Desktop/img/1111"contents:nilattributes:nil]) {
NSLog(@"创建成功!!");
}
// 剪切文件
if ([fm moveItemAtPath:pathtoPath:@"/Users/apple/Desktop/img/pig.png"error:nil]) {
NSLog(@"剪切成功");
}
// 判断是否文件存在
if ([fm fileExistsAtPath:@"/Users/apple/Desktop/img/pig.png"]) {
NSLog(@"文件存在");
}
// 判断是否是文件夹
BOOL isDirectory;
if ([fm fileExistsAtPath:@"/Users/apple/Desktop/img/pig.png"isDirectory:&isDirectory]&&isDirectory){
NSLog(@"是文件夹");
}
// 获取文件夹下所有的文件
NSArray *files = [fm contentsOfDirectoryAtPath:@"/Users/apple/Desktop"error:nil];
for (NSString *filein files) {
NSLog(@"file =%@",file);
}
例子:遍历文件夹
自定义一个函数findImgByDirectoryPath
-(void)findImgByDirectoryPath:(NSString*)path{
NSFileManager*fm = [NSFileManagerdefaultManager];
// 获取文件夹下面的所有的文件名(包含文件和文件夹)
NSArray *files = [fm contentsOfDirectoryAtPath:path error:nil];
for (NSString *filein files) {
// 通过文件名拼接文件的绝对路径
NSString *filePath = [path stringByAppendingPathComponent:file];
BOOL isDirectory;
if ([fm fileExistsAtPath:filePath isDirectory:&isDirectory]&&isDirectory){
// 把最终输出的文件夹过滤掉
if (![file isEqualToString:@"img"]) {
[selffindImgByDirectoryPath:filePath];
}
}
// 判断是否是图片
if ([file hasSuffix:@"jpg"]||[file hasSuffix:@"png"]||[file hasSuffix:@"bmp"]) {
// 拷贝图片到img文件夹
[fm copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"/Users/apple/Desktop/img/%d%@",arc4random()%10000,file]error:nil];
}
}
}
- Foundation中常用的几种数据类型
- Oracle中常用的几种数据类型
- redis常用的几种数据类型介绍
- Redis常用几种数据类型
- Hibernate中几种数据类型的映射
- vc中几种数据类型的转换
- 数据库中几种数据类型的比较
- 《Django》中模板接收到的几种数据类型
- Java中几种数据类型的转化
- Java中switch接受的几种数据类型
- Lua中几种数据类型的默认boolean值
- java中常见的几种数据类型。
- C++ 中几种数据类型转换
- 内核中常用的几种数据结构记录。
- java中常用的几种数据结构
- python 的几种数据类型
- php几种数据类型的转换
- javascript的几种数据类型
- ios 自定义上拉下拉刷新
- 手工释放linux内存——/proc/sys/vm/drop_cache
- Java程序员必须掌握的8大排序算法
- 【Android】Android中WIFI开发总结(二)
- 500.jsp
- Foundation中常用的几种数据类型
- IDL文件模版
- 解析mysql binlog 报错:ErrorPacket[packetMarker=255,errorCode=1236,slash=#,sqlState=HY000,errorMessage=bo
- Arduino IDE 配置文件
- 解决android:background背景图片被拉伸问题
- MyEclipse8 中安装Freemarker插件
- vs2013中如何给一个项目添加MFC支持
- xcrun: error: active developer path
- spring编码拦过滤器配置范例