---------------------- Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練,期待與您交流! ---------------------
一、CoreGraphics架構中的結構體
先引入CoreGraphics架構(CG開頭的架構,NS是NextStep的Foundation架構)
1. NSRange
NSRange ==》 (NSUInteger location, NSUInteger length)
初始化:NSRange r = NSMakeRange(2, 4);
// NSString(NSRange):
NSString *str = @"i love oc.";
// 查找某個字元串在str中的範圍
// 如果找不到,length=0,location=NSNotFound=-1
NSRange range = [str rangeOfString:@"java"];
NSLog(@"loc=%ld, len=%ld", range.location, range.length);
2. NSPoint/CGPoint
NSPoint/CGPoint ==》 (CGFloat x, CGFloat y)
初始化: // 表示原點:CGPointZero == CGPointMake(0, 0)
CGPoint p1 = NSMakePoint(5, 4);
CGPoint p2 = CGPointMake(10, 10); // 最常用
// 以字元串形式快速列印CGPoint CGSize CGRect中的内容:
NSString *str = NSStringFromPoint(p1);
// NSString *str = NSStringFromSize(s1);
// NSString *str = NSStringFromRect(r1);
NSLog(@"%@", str);
3. NSSize/CGSize
NSSize/CGSize ==》 (CGFloat width, CGFloat height)
初始化: // CGSizeZero == CGSizeMake(0,0)
CGSize s1 = NSMakeSize(25, 26);
CGSize s2 = CGSizeMake(10, 15); // 推薦
// 以字元串形式快速列印CGPoint CGSize CGRect中的内容:
// NSString *str = NSStringFromPoint(p1);
NSString *str = NSStringFromSize(s1);
// NSString *str = NSStringFromRect(r1);
NSLog(@"%@", str);
4. NSRect/CGRect
NSRect/CGRect ==》 (CGPoint, CGSize) == ( (CGFloat x, CGFloat y), (CGFloat width, CGFloat height))
初始化:// CGRectZero == CGRectMake(0,0,0,0)
CGRect r1 = NSMakeRect(0, 0, 100, 50);
CGRect r2 = CGRectMake(0, 0, 100, 50);
CGRect r3 = {{0, 0}, {100, 50}};
CGRect r4 = {p2, s2};
NSLog(@"x=%f, y=%f, width=%f, height=%f", r1.origin.x, r1.origin.y, r1.size.width, r1.size.height);
// 以字元串形式快速列印CGPoint CGSize CGRect中的内容:
// NSString *str = NSStringFromPoint(p1);
// NSString *str = NSStringFromSize(s1);
NSString *str = NSStringFromRect(r1);
NSLog(@"%@", str);
5. CGPoint、CGSize、CGRect結構體的其他函數
這些函數都是在CoreGraphics架構中的
// 比較相不相同:
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
NSLog(@"%d", b);
// CGSizeEqualToSize(CGSize size1, CGSize size2)
// CGRectEqualToRect(CGRect rect1, CGRect rect2)
// 矩形是否包含某個點
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(130, 70));
NSLog(@"%d", b2);
二、Foundation架構中的集合類
集合:
OC JAVA
NSArray ArrayList
NSSet HashSet
NSDictionary HashMap
1. NSString \ NSMutableString
NSString:不可變字元串(内容不可改變)
NSMutableString:可變字元串(可以改變字元串的内容)
(1)NSString
字元串的建立:
NSString *str = @"Jack";
// NSString *str1 = [[NSString alloc] initWithString:@"range"];
NSString *str2 = [[NSString alloc] initWithFormat:@"age is %d", 3];
// C字元串 --》 OC字元串
NSString *str3 = [[NSString alloc] initWithUTF8String:"ewrty"];
// OC字元串 --> C字元串
const char *s = [str3 UTF8String];
// NSUTF8StringEncoding 轉碼格式:檔案中有中文就可以用這個
NSString *str4 = [[NSString alloc] initWithContentsOfFile:@"/Users/qitan/Desktop/learning/1.txt" encoding:NSUTF8StringEncoding error:nil];
NSURL *url = [NSURL URLWithString:@"file:///Users/qitan/Desktop/learning/1.txt" ];
NSString *str5 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\n%@", str5);
系統自帶的對象方法一般都有一個類方法與之對應,不需要alloc和init
直接調用NSString的類方法進行字元串的建立:
[NSString stringWithFormat:(NSString *), ...]
[NSString stringWithContentsOfFile:(NSString *) encoding:(NSStringEncoding) error:(NSError *__autoreleasing *)]
[NSString stringWithContentsOfURL:(NSURL *) encoding:(NSStringEncoding) error:(NSError *__autoreleasing *)]
字元串的導出:
[@"im tried~~\nwow!" writeToFile:@"/Users/qitan/Desktop/learning/2.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str = @"werty";
NSURL *url = [NSURL fileURLWithPath:@"/Users/qitan/Desktop/learning/2.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
字元串中的length方法:計算字數,不是字元數。
NSString *name2 = @"哈哈Jack"; //6
//length方法算的是字數,不是字元數
NSLog(@"%ld", [name2 length]); //%ld
(2)NSMutableString
NSString的很多方法在NSMutableString裡都有,而且還有自己的一些方法:
NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is %d", 10];
// 拼接内容到s1的後面 (這個函數傳回值為void)
[s1 appendString:@" 11 12"];
// 删除某寫range的字元[s1 deleteCharactersInRange:[s1 rangeOfString:@"is"]]; // 推薦
[s1 deleteCharactersInRange:NSMakeRange(4, 2)];
NSLog(@"%@", s1);
NSString *s2 = [NSString stringWithFormat:@"age is %d", 10];
// 通過s2和@" 11 12 13" --》 s3
NSString *s3 = [s2 stringByAppendingString:@" 11 12 13"];
2. NSArray \ NSMutableArray
NSArray:不可變數組
NSMutableArray:可變數組
(1)NSArray
* 有序
* 快速建立(不可變的):@[]
* 快速通路元素:數組名[i]
基本使用:
/*1.建立對象數組*****************************/
// OC數組不能存放nil值(結束标志不能過多)
// OC數組隻能存放OC對象,不能存放非OC對象類型,比如int、struct、enum等
// 這個數組永遠都是空數組
NSArray *array = [NSArray array];
NSArray *array2 = [NSArray arrayWithObject:@"jack"];
// nil是數組元素結束的标志
NSArray *array3 = [NSArray arrayWithObjects:@"jouse", @"rose", nil];
// @[] 代表建立數組對象
// 快速建立數組,編譯器特性
NSArray *array4 = @[@"rose", @"jack", @"jose"];
/*2.對象數組元素計數*****************************/
// [array3 count];
long c = array3.count;
NSLog(@"%ld", c);
/*3.對象數組元素通路*****************************/
// NSLog(@"%@", [array3 objectAtIndex:1]);
NSLog(@"%@", array3[0]); // 也可以跟C一樣通路數組元素(編譯器特性)
周遊數組元素:
Person *p = [[Person alloc] init];
NSArray *arr = @[p, @"jack", @"rose"];
// 不推薦使用
for (int i=0; i<arr.count; i++) {
NSLog(@"%@", arr[i]);
}
// 快速周遊
// id obj:數組中的每一個元素
// 缺點:不知道循環的第幾次
//int i = 0;
// for(id obj in arr)
// {
// // 找出obj元素在數組中的位置
// long i = [arr indexOfObject:obj];
//
// NSLog(@"arr[%ld]=%@", i, obj);
// //i++;
//
// if (i == 1) {
// break;
// }
// }
// 【推薦】既然是對象就用對象方法。。
[arr enumerateObjectsUsingBlock:
// 每周遊到一個元素,就會調用一次block
// 并且目前元素和索引位置當做參數傳遞給block
^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%ld--%@", idx, obj);
if (idx == 1) {
// 停止周遊
*stop = YES;
}
}];
(2)NSMutableArray
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"rose", @"write", nil];
// 添加元素
[arr addObject:[[Person alloc] init]];
[arr addObject:@"jack"];
// 錯誤寫法!
// [arr addObject:nil];
// 删除所有元素
// [arr removeAllObjects];
// 删除指定元素
// [arr removeObject:@"jack"];
// 删除指定下标的元素
// [arr removeObjectAtIndex:2];
NSLog(@"%@", arr);
NSLog(@"%ld", arr.count);
// @[] 隻建立不可變數組NSArray
/* 錯誤寫法:unrecognized selector sent to instance 0x100109960
NSMutableArray *arr = @[@"jack", @"rose"];
[arr addObject:@"jim"];
*/
3. NSSet \ NSMutableSet
NSSet:不可變集合
NSMutableSet:可變集合
NSSet和NSArray的對比:
1》共同的
* 都是集合,都能存放多個OC對象
* 隻能存放OC對象,不能存放非OC對象類型(基本資料類型:int、char、結構體、枚舉)
* 本身 不可變,都有一個可變的子類
2》不同點
* NSArray有順序,NSSet無序
(1)NSSet
* 無序
NSSet *s1 = [NSSet set];
NSSet *s2 = [NSSet setWithObjects:@"rose", @"jack4",@"rose2", @"jack", nil];
// 随機取出某個元素(随機的)
NSString *str = [s2 anyObject];
NSLog(@"%@", str);
NSLog(@"%ld", s2.count);
(2)NSMutableSet
NSMutableSet *s1 = [NSMutableSet set];
[s1 addObject:@"hack"];
[s1 addObject:@"rose"];
[s1 removeObject:@"hack"];
4. NSDictionary \ NSMutableDictionary
字典:
索引 --> 文字内容
key --> value
* 快速建立(不可變的):@{key1: value1, key2: value2}
* 快速通路元素:字典名[key]
字典是無序的,裡面存儲的都是鍵值對。
NSDictionary:不可變的字典
NSMutableDictionary:可變的字典
(1)NSDictionary
* 無序
/*
NSDictionary *dict1 = [NSDictionary dictionary];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"jacken" forKey:@"name"];
NSArray *keys = @[@"name", @"address"];
NSArray *values = @[@"jack", @"北京"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"rose",@"name", @"beijing",@"address", @"46454542",@"qq", nil];
id obj = [dict2 objectForKey:@"name"];
id obj2 = [dict3 objectForKey:@"name"];
id obj3 = [dict4 objectForKey:@"qq"];
NSLog(@"%@", obj3);
*/
// Xxode特性
NSDictionary *dict = @{@"name": @"jim", @"address": @"beijing", @"qq": @"64565844"};
// 取出某個key對象對應的value對象
id obj = [dict objectForKey:@"address"];
id obj1 = dict[@"qq"];
NSLog(@"%@", obj1);
// 傳回鍵值對的個數
NSLog(@"%ld", dict.count);
</pre><pre name="code" class="objc"> // 字典的周遊
// 字典不允許有相同的key,但允許有相同的value
// 無序的
NSDictionary *dict = @{@"name": @"jim", @"address": @"beijing", @"qq": @"64565844"};
// NSArray *keys = [dict allKeys];
// for (int i=0; i<dict.count; i++) {
// NSString *key = keys[i];
// NSString *object = dict[key];
//
// NSLog(@"%@ - %@", key, object);
// }
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ - %@", key, obj);
}];
NSArray *persons [email protected][
@{@"name": @"jack", @"no": @"001"},
@{@"name": @"rose", @"no": @"002"},
@{@"name": @"aaa", @"no": @"003"},
@{@"name": @"bbb", @"no": @"004"}
];
NSDictionary *rose = persons[1];
NSLog(@"%@", rose);
// 先取出2對應位置的字典,再取出字典中name這個key對應的資料
NSLog(@"%@", persons[2][@"name"]);
(2)NSMutableDictionary
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 添加鍵值對
[dict setObject:@"jim" forKey:@"name"];
[dict setObject:@"beijing" forKey:@"address"];
// 覆寫以前的值,一個字典隻有一個key
[dict setObject:@"jack" forKey:@"name"];
// 删除鍵值對
// [dict removeObjectForKey:@"address"];
NSLog(@"%@", dict);
/* 錯誤寫法 找不到setObject方法
NSMutableDictionary *dict = @{@"name": @"jim", @"address": @"beijing", @"qq": @"64565844"};
[dict setObject:@"rose" forKey:@"name"];
*/
NSMutableDictionary不能使用不可變字典的建立方式:@{key : value, ...};
5. NSNumber
把基本資料類型(int、double、char、BOOL等數字)包裝成NSNumber對象
// 基本資料類型 ---》 對象
NSNumber *n = [NSNumber numberWithDouble:10.5];
// 對象 ---》 基本資料類型
double d = [n doubleValue];
int a = 20;
NSString *str = [NSString stringWithFormat:@"%d", a];
int b = [str intValue];
// @20 将20包裝成一個NSNumber對象
// @10.5 @YES @'A'
NSDictionary *dict =
@{
@"name": @"jack",
@"age": @20
};
@"A"; // NSString
@'A'; // NSNumber
// 将age變量包裝成NSNumber對象
int age = 100;
@(age);
6. NSValue
NSNumber 之是以能包裝基本資料類型為對象,是因為繼承了NSValue。
NSValue功能更加強大,對CG架構的結構體也可以包裝。
// 将結構體包裝成OC對象
CGPoint p = CGPointMake(10, 10);
// 結構體 --》 NSValue對象
NSValue *val = [NSValue valueWithPoint:p];
NSArray *arr = @[val, val];
// NSValue對象 --> 結構體
CGPoint p2 = [val pointValue];
7. NSDate
日期、時間類型
NSDate的基本使用:
// 建立一個時間對象
NSDate *date = [NSDate date];
// 列印的時間是0時區的時間(北京-東8區)
NSLog(@"%@", date);
// 5 秒
NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
NSLog(@"%@", date2);
// NSTimeInterval == double
// NSTimeInterval seconds = [date2 timeIntervalSince1970];
NSTimeInterval seconds = [date2 timeIntervalSinceNow]; // 調用程式的時間
NSLog(@"%f", seconds);
NSDate和NSString之間的互相轉換
/*Date轉換為NSString*********************/
NSDate *date = [NSDate date];
// 日期格式化類
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// y-年 M-月 d-日
// HH-24時(hh-12時) mm-分 ss-秒
formatter.dateFormat = @"yyyy/MM/dd HH:mm:ss";
NSString *str = [formatter stringFromDate:date];
NSLog(@"%@", str);
/*NSString轉換為Date*********************/
NSString *time = @"2012/05/12 16:45";
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
formatter2.dateFormat = @"yyyy/MM/dd HH:mm";
NSDate *date2 = [formatter2 dateFromString:time];
NSLog(@"%@", date2); // 自動輸出為0時區的時間
CGPoint p2 = CGPointMake(10, 10); // 最常用 初始化: // CGSizeZero == CGSizeMake(0,0) 初始化:// CGRectZero == CGRectMake(0,0,0,0) CGRect r3 = {{0, 0}, {100, 50}};