IOS学习笔记6—Objective C—Foundation框架

来源:互联网 发布:网络的利与弊征文字少 编辑:程序博客网 时间:2024/05/22 06:14

字符串类:NSString

NSString

1.按格式生成:[NSString stringWithFormat:@"The result is %@",5];


2.得到字符串长度:myString.length


3.将字符串写入文件

NSString *myString = @“hello world”;

NSError *error;

//NSHomeDirectory()返回的字符串指向应用程序沙盒的路径

//Cocoa中,大多数文件访问例程都提供了一个原子选项,将原子参数设为YES,Iphone将文件写到一个临时辅助位置,然后就地重命名,使用原子写入可以使文件避免损坏。

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file.txt"];if(![myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]){     NSLog(@"Error writing to file:%@",[error localizeDescription]);     return;}

4.从文件读取字符串

NSString *inString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];if(!inString){     NSLog(@"Error writing to file:%@",[error localizeDescription]);     return;}

5.按指定符号切割字符串

//切割结果为数组NSArray *array = [myString componentSeparatedByString:@" "];

6.字符串比较

[s1 isEqualToString:s2];

7.将字符串转换成数字

[s intValue];[s floatValue];[s boolValue];[s doubleValue];

日期和时间NSDate

1.NSDate *date = [NSDate date]

使用线程使程序休眠一段时间

[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0f];

2.格式化时间

NSDateFormatter *formatter = [[[NSDateFormatter alloc]init] autorelease];formatter.dateFormater = @"MM/dd/YY HH:mm:ss";NSString *timestamp = [formatter stringFromDate:[NSDate date]];

集合

数组:NSArray

NSArray *array = [NSArray arrayWithObjects:@"one",@"two",nil];[array count];[array objectAtIndex:0];

字典:NSDictionary

创建

NSMutableDictionary *dict =  [NSMutableDictionary dictionary];[dict setObject:@“A” forKey:@"a"];取值:[dict objectForKey:@“a"];数量:[dict count];索引:[dict allKeys];

URL

NSURL *url = [NSURL URLWithString : urlPath];


NSData

类似与缓存类

[[NSData dataWithContentsOfURL:url] length];NSMutableData  (可变缓存类)appendData,追加新信息


文件管理

NSFileManager *fm  = [NSFileManager defaultManager];


加入我们的QQ群或微信公众账号请查看:Ryan's zone公众账号及QQ群