Foundation框架
正如其名,Foundation框架是其他所有ios框架的基礎
Foundation框架包含了很多開發中常用的數據類型:
1.結構體
2.枚舉
3.類
要想使用Foundation中的數據類型,包含他的主文件即可
#import<Foundation/Foundation.h>
Foundation中的結構體
NSRange
NSPoint\CGPoint
NSSize\NSSize
NSRect\NSRect
NSRange中的成員
Location位置
length 長度
例如:
Love的範圍
@“I love oc”
NSRange r1 = {2,4};//不用
NSRange r2 = {.location = 2,.length = 4};
NSRange r3 = NSMakeRange(2,4);
NSString *str = @”I love oc”;
//查找某個字符串在str中的範圍
//如果找不到,length=0,location=NSNotFound==-1
NSRange range = [str rangeOfString@”love”];
NSLog(@”loc=%ld,len=%ld”,range.location,range.length);
NSPoint\CGPoint
CGPointp1=NSMakePoint(10,10);
NSPointp2 = CGPointMake(10,10);//這個是開發中最常用的
NSSize\NSSize
表示UI元素的尺寸的
NSSize s1 = CGSizeMake(100,50);
NSSize s2 = NSMakeSize(100,50);
NSRect\NSRect
包含了CGPoint、CGSize
NSRect r1 = CGRectMake(0,10,100,20);
當需要打印的時間要把結構體轉換成字符串再進行打印
NSString *mmm = NSStringFromPoint()
NSString *mmm = NSStringFromSize()
NSString *mmm = NSStringFromRect()
NSRect r1 = CGRectMake{{0,0},{100,20}};
NSRect r1 = CGRectMake{p1,s2};
表示原點:CGPointZero == CGPointMake(0,0);
//比較兩個點是否相同
BOOL b = CGPointEqualToPoint(CGPointMake(10,10),CGPointMake(10,10));
CG需要導入框架CoreGraphics
Foundation中的類
NSString有個子類NSMutableString
NSArray有個子類NSMutableArray
NSSet有個子類NSMutableSet
NSDictionary有個子類NSMutableDictionary
NSDate
NDObject
1. NSString
NSString:不可變字符串
NSMutableString:可變字符
A. 字符串的創建
NSString *s1 = @”jack”;
NSString *s2 = [[NSString alloc] initWithString:@”jack”];
NSString *s3 = [[NSString alloc] initWithFormat:@”ageis %d”,10];
//c字符串轉化成OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8Sting:”jack”];
//OC字符串轉換成c字符串
Const char *C = [s4UTF8String];
//NSUTF8StringEncoding用到中文就可以用這種編碼
NSString *s5 = [[NSString alloc]initWithCountsOfFile:@”/Users/apple/Desktop/1.txt”encoding:NSUTF8StringEncoding error:nil];
//URL資源
NSString *s6 = [[NSString alloc]initWithCountsOfURL:@”file:///Users/apple/Desktop/1.txt”encoding:NSUTF8StringEncoding error:nil];
B. NSMutableString可變字符串
補充詞彙:append增加、拼接
NSRange跟刪除字符串的結合使用
2. 集合類
NSArray有個子類NSMutableArray
NSSet有個子類NSMutableSet
NSDictionary有個子類NSMutableDictionary
a. OC中的數組:NAArray
在OC數組中不能存放基本數據類型,即只能存放OC對象不能存放非OC對象類型
NSArry *array =[NSArry array];//這個array永遠是空數組
NSArray :是一個不可變數組
NSMutableArray:是一個可變數組
NSArray *array =[NSArray arrayWithObject:@“asfd”,@“rose”,nil]//nil是數組元素結束的標記
@[]創建數組對象
數組中的遍歷:
方式一、 For(int i=0;i<array.count;i++)
{
NSLog(@“%@”,array[i]);
}
方式二、 For(id obj in array)
{
NSLog(@”%@”,obj);
}
方式三、
//沒遍歷到一個元素,就會調用一次block
//並且當前袁術和索引位置當作參數傳給Block
[array enumrateObjectUsingBlock:
^(id obj ,NSUInteger idx,BOOL*stop)
{
NSLog(@“------------%ld--%@”idx,obj);
If(ind==0){
*stop= YES;
}
}];
遞歸思想
NSMutableArray:是一個可變數組
NSMutableArray*array = [NSMutableArray array];
//添加數組元素
[arrary addObject:[Person alloc] init];
[arrary addObject:@”jack”];
//刪除元素
[array removeAllObject];
[array removeObject:@”jack”];
//下面代碼中2是數組的下標號
[array removeObjectAtIndex:2];
在NSMutableArray中不能用@[]這個快速寫數組,因爲@[]數組是不可寫數組
b. NSSet是沒有數據的
anyObject 隨機返回元素
NSMurableSet:
addObject
remove
c. NSDictionary有個子類NSMutableDictionary
裏面存儲的東西都是鍵值對
@{key,value}但是快速創建自字典之後是不能更改的,即不可變的
字典的遍歷:
方式一、NSDictiongary *dict = @{@”address”:@”北京”@”name”:@”jack”,@”qq”:@”565632362”};
NSArray *keys = [dict allKeys];
For(int i=0;i<dict.count;i++)
{
NSString *key =key[i];
NSString *object = dict[key];
}
方式二、
[dict enumerateKeyAndObjectsUsingBlock:^(id key,idobj,BOOL *stop){
NSLog(@”%@--%@”,key,obj);
*stop =YES;//只遍歷一次
}];
集合總結:
1. NSArray\NSMutableArray
l 有序
l 快速創建(不可變的):@[]
l 快速訪問元素:數組名[i]
2. NSSet\ NSMutableSet
l 無序
3. NSDictionary\NSMutableDictionary
l 無序
l 快速創建(不可變的):@{key1:value1,key2:value2}
l 快速訪問:字典名[key]
d. NSNumber
@20 -------------將20包裝成一個NSNumber對象
e. NSValue
這是一個萬能包裝,任何東西都能包裝成NSValue對象
例如:
Int main()
{
//結構體--àOC對象
CGPointp = CGPointMake(10,10);
NSValue*value = [NSValue nalueWithPoint:p];
NSArray*array = @[ value];
}
4. NSDate
NSDate *date =[NSDate date];
//日期格式化
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
//y年M月d日
//m分s秒H(24)時h(12)時
Formatter.dateFormar = @”yyyy-MM-ddHH:mm:ss”;
NSString *str = [formatterstringFromDate:date];