Foundation框架精選

一、NSString用法

1.字符串比較函數:
NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];
返回值:NSOrderedAscending(str1<str2)
NSOrderedDescending(str1>str2)
NSOrderedSame(str1 = str2)

2. 判讀字符串是否相等:
[str1 isEqualToString:str3]


3. 檢測字符串前後綴:
[url hasPrefix:@"http://"]; 字符串是否以http://開頭
[imgName hasSuffix:@".jpg"]; 檢測字符串是否以.jpg結尾


4.查找字符串的位置
NSRange range = [str1 rangeofString:str2]; //str1 中找str2

5.字符串截取
NSString *str1 = [str substringFromIndex:5];//從xx位置開始到搜字符結束
NSString *str2 = [str substringToIndex:5];// 從開始位置到xx位置結束
NSRange r1 = {3,4};
NSString *str3 = [str substringWithRange:r1];// 截取一個range範圍


6. 將字符串轉成int類型
int b = [str intValue]; // 前提是字符串是數值類型




二、NSRange

1. NSRange的結構體初始化
NSRange r4 = NSMakeRange(3,3)

2.打印NSSrange:
NSStringFromRange(r4)



三、NSURL

1. 通過urlwithstring創建NSURL
NSURL *url = [NSURL URLWithString:@"sms://10086"];



三、NSMutableString


1. 格式化和拼接字符串:
NSMutableString *str = [NSMutableString string];
[str appendFormat:@"http://www.baidu.com/%d",100];

2. 刪除一部分內容:
[str deleteCharactersInRange:NSMakeRange(3, 4)];

3. 插入一個字符串:
[str insertString:@"p://" atIndex:3];

4. 將某個字符串內容替換成指定內容:
[str replaceCharactersInRange:NSMakeRange(11, 5) withString:@"itcast"];





四、NSArray基本使用

1. 直接初始化
NSArray *arr1 = [NSArray array];


2. 用一個數組可以創建另外一個數組
NSArray *arr5 = [NSArray arrayWithArray:arr3];

3. 簡化數組元素的創建:
NSArray *arr = @[@"1",@"one",@"3",@4,@"ONE"];

4. 通過下標訪問數組元素:
NSArray *arr = [@"one",@"two",@"three"];
arr[下標];

5. 數組的遍歷方式:
普通for循環,取角標
快速枚舉法:
for(NSString *str in arr){

}

6. NSArray讀寫文件:
將數組寫入到文件中:
BOOL isWrite = [array writeToFile:@"/Users/zhaoxiaohu/Desktop/arr.xml" atomically:YES];

從文件中,讀取一個數組信息
NSArray *readArr = [NSArray arrayWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/arr.xml"];

7. 快速包裝數組:
NSArray *arr = @[@1,@2,@3,@4];

將數組連在一起變成NSString
NSString *str = [arr componentsJoinedByString:@"-"];

8. 將字符串分割成數組
str2 = @"400#800#12580#400#888#11200";
NSArray *arr3 = [str2 componentsSeparatedByString:@"#"];

9. 取數組的元素方法:
[arr firstobject] //取出第一個元素
[arr lastobject] // 取出最後一個元素




五、NSMutableArray的基本使用

1. 創建數組的時候指定放置多少個元素:
NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];
[arr4 addobject:@"fengjie"]

2. 插入某個元素到數組當中
[arr1 insertObject:@"fengjie" atIndex:0];
[arr1 removeAllobjects]; // 移除數組中所有的元素


3. 判斷數組中是否包含某個元素
BOOL isSearch = [arr3 containsObject:@"four"];




六,NSDictionary的基本使用

1. 普通初始化:
NSDictionary *dictionary = [NSDictionary dictionary]


2. 快速創建鍵值對的方法:
NSDictionary *dict4 = @{@"zs":@"zhaosi",@"zs":@"zhangsan",@"ls":@"lisi",@"bz":@"banzhang"};
dict4.count// 計算字典中鍵值對的個數


3. 獲取字典中的某個元素:
dict[@"zbz"]

4. 把字典寫入到文件中:
BOOL isWrite = [dict writeToFile:@"/Users/zhaoxiaohu/Desktop/dict.plist" atomically:YES];

5. 從文件中讀取字典
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/dict.plist"];





七、NSMutableDictionary的基本使用

1. 可變字典的創建
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; //創建空字典


2. 給可變字典添加鍵值對
[dict1 setValue:@"zhaosi" forKey:@"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.獲取文件的屬性(信息)
NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];


5. 根據路徑創建文件
BOOL isYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YES attributes:nil error:nil];

6.將字符串轉換成二進制數據
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

7.移動文件
[fm moveItemAtPath:createDirPath toPath:targetPath error:nil];

8.刪除文件
[fm removeItemAtPath:targetPath error:nil];

九、沙盒
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 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";



4. 計算日曆對象:
NSDate *d = [NSDate date];
//創建日期的對象
NSCalendar *cal = [NSCalendar currentCalendar];
//cal components:獲取日期的哪些部分 fromDate:日期對象
NSDateComponents *coms = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:d];








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章