一、NSString用法
1.字符串寫入文件:
[str writeToFile:@"/Users/zhaoxiaohu/Desktop/"atomically:YESencoding:NSUTF8StringEncoding error:&err];
2.字符串從文件讀取:
NSString *str = [NSString stringWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/str.txt"encoding:NSUTF8StringEncoding error:&err];
3.字符串比較函數:
NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];
返回值:NSOrderedAscending(str1
NSOrderedDescending(str1>str2)
NSOrderedSame(str1 = str2)
4.判讀字符串是否相等:
[str1 isEqualToString:str3]
5.檢測字符串前后綴:
[url hasPrefix:@"http://"];字符串是否以http://開頭
[imgName hasSuffix:@".jpg"];檢測字符串是否以.jpg結尾
6.查找字符串的位置
NSRange range = [str1 rangeofString:str2];//str1中找str2
7.字符串截取
NSString *str1 = [str substringFromIndex:5];//從xx位置開始到搜字符結束
NSString *str2 = [str substringToIndex:5];//從開始位置到xx位置結束
NSRange r1 = {3,4};
NSString *str3 = [str substringWithRange:r1];//截取一個range范圍
8.字符串替換://用*替換a
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"a"withString:@"*"];
9.將字符串轉成int類型
intb = [str intValue];//前提是字符串是數值類型
10.c字符串與oc字符串相互替換
NSString *str = [NSString stringWithUTF8String:s];// c -> oc
constchar*s1 = [str2 UTF8String];// oc -> c
二、NSRange
1.NSRange的結構體初始化
NSRange r4 = NSMakeRange(3,3)
2.打印NSSrange:
NSStringFromRange(r4)
三、NSURL
8.通過urlwithstring創建NSURL
NSURL *url = [NSURL URLWithString:@"sms://10086"];
NSURL *url = [NSURL URLWithString:@"file:///Users/zhaoxiaohu/Desktop/3.txt"];
9.獲取本地路徑:
NSURL *url = [NSURL fileURLWithPath:@"/Users/zhaoxiaohu/Desktop/4.txt"];
10.通過url創建字符串
NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
三、NSMutableString
1.不可變字符串的字符串創建:
NSMutableString *str2 = [NSMutableString stringWithFormat:@"Jack"];
NSMutableString *str3 = [NSMutableString string];
2.格式化拼接字符串:
NSMutableString *str = [NSMutableString string];
[str appendFormat:@"http://www.baidu.com/%d",100];
3.刪除一部分內容:
[str deleteCharactersInRange:NSMakeRange(3,4)];
4.插入一個字符串:
[str insertString:@"p://"atIndex:3];
5.將某個字符串內容替換成指定內容:
[str replaceCharactersInRange:NSMakeRange(11,5) withString:@"itcast"];
6.字符串拼接:
[str appendString:@"xxxxx"];
四、NSArray基本使用
NSArray:
特點:一旦創建內容不可改變,只能存儲oc對象
1.直接初始化
NSArray *arr1 = [NSArray array];
NSArray *arr2 = [[NSArray alloc]init];
2.創建數組,只有一個元素
NSArray *arr2 = [NSArray arrayWithObject:@"1"];
3.創建數組,有多個元素
// nil表示數組賦值結束
NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,nil];
4.調用對象方法,創建數組
//nil Nil NULL? NSNULL
NSArray *arr4 = [[NSArray alloc] initWithObjects:@"three",[NSNull null],@"four",nil];
5.用一個數組可以創建另外一個數組
NSArray *arr5 = [NSArray arrayWithArray:arr3];
6.簡化數組元素的創建:
NSArray *arr =@[@"1",@"one",@"3",@4,@"ONE"];
7.獲取數組的某個元素:
NSString *str =[arr objectAtIndex:2];
str = arr[1]//簡化方式訪問
8.通過下標訪問數組元素:
NSArray *arr = [@"one",@"two",@"three"];
arr[下標];
9.數組的遍歷方式:
普通for循環,取角標
快速枚舉法:
for(NSString *strinarr){
}
block:
[arr enumerateObjectsUsingBlock:^(idobj, NSUinteger index,BOOL*shop){
}];
10.NSArray讀寫文件:
創建數組:
NSArray *array = [NSArray arrayWithObjects:@"one",@"zbz",@"cgx",@"sb",@"cjk",@"senni",nil];
將數組寫入到文件中:
BOOLisWrite =? [array writeToFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"atomically:YES];
從文件中,讀取一個數組信息
NSArray *readArr = [NSArray arrayWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"];
11.快速包裝數組:
NSArray *arr =@[@1,@2,@3,@4];
將數組連在一起變成NSString
NSString *str = [arr componentsJoinedByString:@"-"];
12.將字符串分割成數組
str2 =@"400#800#12580#400#888#11200";
NSArray *arr3 = [str2 componentsSeparatedByString:@"#"];
13.取數組的元素方法:
[arr firstobject]//取出第一個元素
[arr lastobject]//取出最后一個元素
五、NSMutableArray的基本使用
1.可變數組:初始化的時候,直接放置某個元素
NSMutableArray *arr2 = [NSMutableArray arrayWithObject:@"one"];
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"one",@"two",@3,nil];
2.創建數組的時候指定放置多少個元素:
NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];
[arr4 addobject:@"fengjie"]
3.插入某個元素到數組當中
[arr1 insertObject:@"fengjie"atIndex:0];
[arr1 removeAllobjects];//移除數組中所有的元素
4.修改數組當中的某個元素
[arr3 replaceObjectAtIndex:1withObject:@"four"];
5.判斷數組中是否包含某個元素
BOOLisSearch = [arr3 containsObject:@"four"];
6.交換數組當中的元素:
NSMutableArray *arr5 =[NSMutableArray arrayWithObjects:@1,@2,@3,@4,@5,nil];
//可以交換數組元素
[arr5 exchangeObjectAtIndex:0withObjectAtIndex:4];
六,NSDictionary的基本使用
1.普通初始化:
NSDictionary *dictionary = [NSDictionary dictionary]
2.初始化的時候指定字典中的鍵值對
一組鍵值對:
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"zhangsan"forKey:@"zs"];
多組鍵值對:
NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"k1",@"value2",@"k2",nil];
3.快速創建鍵值對的方法:
NSDictionary *dict4 =@{@"zs":@"zhaosi",@"zs":@"zhangsan",@"ls":@"lisi",@"bz":@"banzhang"};
dict4.count//計算字典中鍵值對的個數
4.使用枚舉遍歷字典:
[dict4 enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
NSLog(@"%@ --> %@",key,obj);
}];
5.獲取字典中的某個元素:
dict[@"zbz"]
6.把字典寫入到文件中:
BOOLisWrite = [dict writeToFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"atomically:YES];
7.從文件中讀取字典
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"];
8.讀取citys.plist中的內容
NSDictionary *citysDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/citys.plist"];
9.使用block遍歷字典中的內容:
[citysDict enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
for(NSString *strinobj) {
NSLog(@"city = %@",str);
}
}];
七、NSMutableDictionary的基本使用
1.可變字典的創建
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];//創建空字典
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:3];
2.給可變字典添加鍵值對
[dict1 setValue:@"zhaosi"forKey:@"ls"];//因為key值重復了,所以添加不上
[dict1 setValue:@"lisi"forKey:@"ls"];// ls
3.刪除可變字典的鍵值對
[dict1 removeObjectForKey:@"ls"];
[dict1 removeAllObjects];
4.修改字典中的數據
[dict1 setObject:@"zhaosi"forKey:@"ls"];
5.獲取所有的key值
NSArray *arr = [dict1 allkeys]
[arr containsObject:@"ls"];//數組中是否包含@“ls”這個元素
八、NSFilemanger文件管理對象
1.單例對象:在程序運行期間,只有一個對象存在
NSFileManger *manger = [NSFileManger defaultManger];
2.判斷某個路勁文件是否存在
Bool isExist = [manger fileExistsAtPath:filePath];
3.判斷是否是一個目錄
Bool isDir;
[manger fileExistsAtPath:filePath? diDirectory:&isDir];
4.判斷文件是否可讀
Bool isRead = [manger isReadableFileAtPath:filePath];
5.判斷文件是否可寫
Bool isWrite = [manger isWriteableFileAtPath:filePath];
6.判斷文件是否可以刪除
Bool isDel = [manger isDeletableFileAtPath:filePath];
用法:
7.獲取文件的屬性(信息)
NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];
8.使用遞歸方式獲取文件及子目錄
NSArray *subPaths = [fm subpathsAtPath:dirPath];
9.直接獲取文件及子目錄
subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
10.獲取指定目錄下的文件及目錄信息(不在獲取后代路徑)
subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];
11.根據路徑創建文件
BOOLisYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YESattributes:nilerror:nil];
12.將字符串轉換成二進制數據
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
13.移動文件
[fm moveItemAtPath:createDirPath toPath:targetPath error:nil];
14.刪除文件
[fm removeItemAtPath:targetPath error:nil];
九、沙盒
1.沙盒:
存放數據的文件夾
特點:每個應用程序都有自己的沙盒(iOS只能訪問自己的沙盒),iOS8開始,開放了幾個固定區域
應用程序包(文件夾):Documents持久化數據
tem臨時目錄
library
cache緩存
preference (系統偏好)// SQlite,coreData
2.沙盒路徑獲取方法:
NSString *sandBoxPath = NSHomeDirectory();
//參數1:要查找的目錄參數2:是否是用戶主目錄YES/NO是否獲取全路徑
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentPath = [paths lastObject];
十、常見結構體
結構體:
CGPoint
CGPoint c4 = CGPointMake(10,10);
CGSize
CGSize s2 = CGSizeMake(100,100);
CGRect
CGRect r2 = {{0,1},{20,34}};
CGRect r3 = CGRectMake(10,10,100,30);
十一、NSNumber的包裝
1.NSNumber普通包裝:
NSNumber *intObj = [NSNumber numberWithInt:a];
2.NSNumber快速包裝:
NSNumber *number =@(18)
十二、NSValue的包裝
NSValue對象的包裝:
1.通過CGPoint包裝:
CGPoint p1 = CGPointMake(20,50);
NSValue *pointVal = [NSValue valueWithPoint:p1];
2.通過CGRect包裝
NSRect r1 = NSMakeRect(0,0,200,100);
NSValue *rectVal[NSValue valueWithRect:r1]
十三.NSDate
1.設置日期的顯示格式
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat =@"yyyy年MM月dd日HH:mm:ss";
formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";
2.顯示出時間
NSTimeInterval t =60*60*24;
NSDate *tom = [NSDate dateWithTimeIntervalSinceNow:t];
NSDate *zuotian = [NSDate dateWithTimeIntervalSinceNow:-t];
//格式化顯示時間
NSString *timeStr = [formatter stringFromDate:zuotian];
3.計算昨天的時間:
NSDate *now = [NSDate date];
NSDate *zt = [now addTimeInterval:-t];
timeStr = [formatter stringFromDate:zt];
4.計算日歷對象:
NSDate *d = [NSDate date];
//創建日期的對象
NSCalendar *cal = [NSCalendar currentCalendar];
//cal components:獲取日期的哪些部分fromDate:日期對象
NSDateComponents *coms =? [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:d];