dispatch_async

[c]dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 耗时的操作
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
});
}); [/c]

example:

[c]dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://learn-house.idv.tw/xxx.jpg"];
NSData * data = [[NSData alloc]initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc]initWithData:data];
if (data != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
}
});[/c]

serial 的 queue

[c]dispatch_async(dispatch_queue_create("com.learnhouse.queue", DISPATCH_QUEUE_SERIAL), ^{
//your code
});[/c]

NSDictionary

[c]//一個key – value
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"@123" forKey:@"key1"];
//兩個以上 key – value
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2" ,nil];
//用字典為另一個子字典初始化
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic1]
//新方法賦值
NSDictionary *dic4 = @{@"key1": @"value1",@"key2": @"value2"};
//以為件內容初始化一個字典
NSDictionary *dic5 = [NSDictionary dictionaryWithContentsOfFile:path];
//將字典的key轉成一個枚舉對象,用於遍歷
NSEnumerator *enumerator = [dic4 keyEnumerator];
//將字典的value轉成一個枚舉對象,用於遍歷
NSEnumerator *enumerator = [dic6 objectEnumerator];[/c]

字典的常用方法

[c]//返回字典鍵值對的個數
NSInteger count = [dic4 count];
//通過key獲取對應的value對象
NSObject *object = [dic2 objectForKey:@"key1"];
//獲取所有鍵的集合
NSArray *keys = [dic3 allKeys];
//獲取所有值的集合
NSArray *values = [dic3 allValues];[/c]

NSMutableDictionary

可變字典與不可變字典的初始化方法相同,但不能用新方法。下面只介紹常用方法

常用方法

[c]NSMutableDictionary *dic6 = [NSMutableDictionary dictionary];
//像字典中追加一個新的 key5 和 value5
[dic6 setObject:@"value5" forKey:@"key5"];
//像字典中添加整個字典對象
[dic6 addEntriesFromDictionary:dic1];
//將字典6的對象內容設置與字典1的對象內容相同
[dic6 setDictionary:dic1];
//刪除鍵所對應的鍵值對
[dic6 removeObjectForKey:@"key1"];
//刪除數組中的所有key 對應的鍵值對
NSArray *array = @[@"key1",@"key2",@"key3"];
[dic6 removeObjectsForKeys:array];
//移除字典中的所有對象
[dic6 removeAllObjects];[/c]

一般遍歷

[c]for (int i = 0; i < [dic3 count]; i++) {
id key = [keys objectAtIndex:i];
id obj = [dic3 objectForKey:key];
NSLog(@"%@",obj);
}[/c]

快速遍歷

[c]for (id key in dic3) {
id obj = [dic3 objectForKey:key];
NSLog(@"%@",obj);
}[/c]

枚舉的辦法遍歷

[c]NSEnumerator *enumerator = [dic4 keyEnumerator];
id key = [enumerator nextObject];
while (key) {
id obj = [dic4 objectForKey:key];
NSLog(@"%@",obj);
key = [enumerator nextObject];
}[/c]

寫入BOOL值

[c][dicTemp setObject:[NSNumber numberWithBool:YES] forKey:@"is_default"];
//or
dicTemp[@"is_default"] = @YES;[/c]

NSString

切字串的方法
(NSString *)substringFromIndex:(NSUInteger)anIndex
(NSString *)substringToIndex:(NSUInteger)anIndex
(NSString *)substringWithRange:(NSRange)aRange

example:

[c]NSString *str = @"1234567890";
NSLog(@"Original String: %@", str);
NSLog(@"substringFromIndex: 5 ==> %@", [str substringFromIndex:5]);
NSLog(@"substringToIndex: 5 ==> %@", [str substringToIndex:5]);
NSLog(@"substringWithRange: from 7, length 3 ==> %@", [str substringWithRange:NSMakeRange(7, 3)]);[/c]

result:

[c]Original String: 1234567890
substringFromIndex: 5 ==> 67890
substringToIndex: 5 ==> 12345
substringWithRange: from 7, length 3 ==> 890[/c]

字串取代
– (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement

example:

[c]NSString *str = @"This is a string";
str = [str stringByReplacingOccurrencesOfString:@"string"
withString:@"duck"];
//This is a duck
[/c]

前戳字串比對

[c]if ([myString hasPrefix:@"http"]) {
}[/c]

NSString 和 NSDate 互相轉換

由 NSDate 轉換為 NSString:

[c]NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);[/c]

由 NSString 轉換為 NSDate:

[c]NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
NSLog(@"%@", date);[/c]

char 轉換成 NSString

char myChar = ‘r’;
[NSString stringWithFormat:@”%c” , myChar];

NSString 轉換成 char

const char *myChar = [myString UTF8String];

数组中的最大值跟最小值

[c]NSArray * arr = [NSArray arrayWithObjects:@"1",@"10",@"5", @"31", nil];
//得到数组中最小的值
NSInteger min = [[arr valueForKeyPath:@"@min.intValue"] integerValue];
//得到数组中最大的值
NSInteger max = [[arr valueForKeyPath:@"@max.intValue"] integerValue];
NSLog(@"min = %ld max = %ld", (long)min, (long)max);[/c]

Split an NSString

[c]NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"];
NSString* firstBit = [foo objectAtIndex: 0];[/c]

UITextField

http://learn-house.idv.tw/?p=1990

偵測view的返回back button

[c]-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}[/c]

Operation Queues的使用

[c]NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; //main queue
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //other queue
queue.maxConcurrentOperationCount = 1;//1 is mean serial, others are mean concurrent
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
//任務執行
}];
[queue addOperation:operation];[/c]

or

[c]NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
[oprationQueue addOperationWithBlock:^{
//your code
}];[/c]

NSInvocationOperation

[c]NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
oprationQueue.maxConcurrentOperationCount = 2;//設置最大並發數數

//NSInvocationOperation 為NSOperation的具體子類,是一個非並發操作。
NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"2.1"];
[oprationQueue addOperation:invation];//將任務添加到列隊中

//第二個任務
NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@"2.2"];
invation2.queuePriority = NSOperationQueuePriorityVeryHigh;//設置invation2線程優先級
[oprationQueue addOperation:invation2];[/c]

NSThread & NSTimer

[c]
//NSThread
[NSThread sleepForTimeInterval:5];

//NSTimer
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(yourmethod:) userInfo:nil repeats:NO];

//傳遞userInfo
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:str1 forKey:@"KEY1"];
[dic setObject:str2 forKey:@"KEY2"];

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(yourmethod:) userInfo:dic repeats:NO];

-(void)yourmethod:(NSTimer *)timer{
NSMutableDictionary *dic = timer.userInfo;
NSString *str1 = [dic objectForKey:@"KEY1"];
NSString *str2 = [dic objectForKey:@"KEY2"];
}
[/c]

Leave a comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *