小結Foundation框架

一、簡介

 
框架是由許多類、方法、函數和文檔按照一定的邏輯關係組織起來的集合,以便使研發程序變得更容易。
Mac OS開發會使用Cocoa框架,由Foundation和ApplicationKit兩個框架組成。iOS程序開發會使用Cocoa Touch框架,由Foundation和UIKit框架組成。其中ApplicationKit和UIKit主要是一些用戶界面設計的類,而Foundation框架則主要定義了一些基礎類,如數字、字符串、數組、字典等。
 
二.結構體
 
 
CGPoint    CGRect     CGSize
 
// 比較兩個點是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)
 
// 查看一點是否在Rect範圍內
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));
 
// 結構體的設置
// 使用CGPointZero等的前提是添加CoreGraphics框架
NSPoint p2 = CGPointMake(20, 20);// 最常用
NSSize s1 = CGSizeMake(100, 50);
NSRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};
 
// 將結構體轉爲字符串
NSString *str = NSStringFromPoint(p1);
NSString *str = NSStringFromSize(s3);   
NSString *str = NSStringFromRect(r1);
 
 
三、NSString
 
NSString : 不可變字符串
NSMutableString : 可變字符串
 
// NSString字符串的創建
NSString *s1 = @"jack";
NSString *s2 = [[NSString alloc] initWithString:@"jack"];    
NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d", 10];
 
// 字符串的導出
NSString *str = @"4234234";
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/my2.txt"];
 
// NSUTF8StringEncoding 用到中文就可以用這種編碼
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
 
// NSMutableString字符串的創建
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
 
// NSString字符串和NSMutableString字符串的拼接
NSMutableString 字符串的拼接,直接拼接
NSMutableString *s1 = [NSMutableString stringWithFormat:@"my age is 10"];
// 拼接內容到s1的後面
[s1 appendString:@" 11 12"];
 
NSString字符串的拼接之後會創造一個新的字符串,不能在原NSString上拼接
NSString *s2 = [NSString stringWithFormat:@"age is 10"];
NSString *s3 = [s2 stringByAppendingString:@" 11 12"];
 
// 獲取is的範圍
NSRange range = [s1 rangeOfString:@"is"];
 
 
四、NSArray
 
NSArray :不可變數組
NSMutableArray : 可變數組
 
// NSArray的創建
// OC數組只能存放OC對象、不能存放非OC對象類型,比如int、struct、enum等
NSArray *array2 = [NSArray arrayWithObject:@"jack"];
NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil];
 
// 快速創建一個NSArray對象
NSArray *array4 = @[@"jack", @"rose", @"4324324"];
 
// NSArray的元素個數
NSLog(@"%ld", array3.count);
 
// NSArray中元素的訪問
NSLog(@"%@", [array3 objectAtIndex:1]);
NSLog(@"%@", array3[0]);
 
// 遍歷數組
[array enumerateObjectsUsingBlock:
// id obj代表着數組中的每一個元素
     // 找出obj元素在數組中的位置
     // NSUInteger idx = [array indexOfObject:obj];
     // 每遍歷到一個元素,就會調用一次block
     // 並且當前元素和索引位置當做參數傳給block
     ^(id obj, NSUInteger idx, BOOL *stop)
     {
         NSLog(@"%ld - %@", idx, obj);
         
         
         if (idx == 0)
         {
             // 停止遍歷
             *stop = YES;
         }
         
     }];
 
// NSMutableArray的創建
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", @"jim", nil];
 
// 添加元素
[array addObject:[[Person alloc] init]];
[array addObject:@"jack"];
 
// 刪除元素
[array removeAllObjects];
 
// 刪除指定的對象
[array removeObject:@"jack"];
 
 
五、NSSet
 
 * NSArray有順序,NSSet沒有順序
// NSSet的創建
NSSet *s = [NSSet set];
NSSet *s2 = [NSSet setWithObjects:@"jack",@"rose", @"jack2",@"jack3",nil];
 
// 隨機拿出一個元素
NSString *str =  [s2 anyObject];
 
// NSMutableSet的創建
NSMutableSet *s = [NSMutableSet set];
 
// 添加元素
[s addObject:@"hack"];
 
// 刪除元素
// [s removeObject:<#(id)#>];
 
六、NSDictionary  字典
 
字典:
     key ----> value
     索引 ---->文字內容     
     裏面存儲的東西都是鍵值對
 
// NSDictionary的創建
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
     @"jack", @"name",
     @"北京", @"address",
     @"32423434", @"qq", nil];
 
// NSDictionary的快速創建
NSDictionary *dict = @{
    @"address" : @"北京",
    @"name" : @"jack",
    @"name2" : @"jack",
    @"name3" : @"jack",
    @"qq" : @"7657567765"};
 
// NSDictionary的創建
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 
// 添加鍵值對
[dict setObject:@"jack" forKey:@"name"];
 
// 移除鍵值對
[dict removeObjectForKey:<#(id)#>];
 
// 遍歷字典
[dict enumerateKeysAndObjectsUsingBlock:
     ^(id key, id obj, BOOL *stop) {
         NSLog(@"%@ - %@", key, obj);
         // *stop = YES;
     }];
 
// 遍歷字典r
    NSArray *persons = @[
    @{@"name" : @"jack", @"qq" : @"432423423", @"books": @[@"5分鐘突破iOS編程", @"5分鐘突破android編程"]},
    @{@"name" : @"rose", @"qq" : @"767567"},
    @{@"name" : @"jim", @"qq" : @"423423"},
    @{@"name" : @"jake", @"qq" : @"123123213"}
    ];
NSString *bookName = persons[0][@"books"][1];
 
 
七、NSNumber
 
// 將各種基本數據類型包裝成NSNumber對象
@10.5;
@YES;
@'A'; // NSNumber對象
@"A"; // NSString對象
 
// NSNumber的創建
NSNumber *n = [NSNumber numberWithDouble:10.5];
NSArray *array = @[
@{@"name" : @"jack", @"age" : @20}
];
 
八、NSDate
 
// NSDate的創建
// 創建一個時間對象
NSDate *date = [NSDate date];
 
// 打印出的時候是0時區的時間(北京-東8區)
NSLog(@"%@", date);
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
 
// 日期格式化類
// 09/10/2011
NSString *time = @"2011/09/10 18:56";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 
// y 年  M月  d 日
// m 分 s秒  H (24)時  h(12)時
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
NSDate *date = [formatter dateFromString:time];
NSLog(@"%@", date);
 
 
九、NSValue
 
// NSNumber之所以能包裝基本數據類型爲對象,是因爲繼承了NSValue
// 結構體--->OC對象
CGPoint p = CGPointMake(10, 10);
 
// 將結構體轉爲Value對象
NSValue *value = [NSValue valueWithPoint:p];
 
// 將value轉爲對應的結構體
// [value pointValue];
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章