NSInvocation的基本用法,NSInvocation用法
@在 iOS中可以直接調用某個對象的訊息方式有2種:
1.performSelector:withObject: 2.NSInvocation
第一種方式比較簡單,能完成簡單的調用。但是對於>2個的參數或者有返回值的處理,那就需要做些額外工作才能搞定。那麼在這種情況下,我們就可以使用NSInvocation來進行這些相對複雜的操作.
@其實也是工作所使,原先只是知道,但沒實際的用過,果然工作的時候才會實際的區去運用各個知識點.
//// HMTTest1.h// Created by HMT on 14-8-3.//#import <Foundation/Foundation.h>@interface HMTTest1 : NSObject- (NSString *)addTestString:(NSString *)testString;@end//// HMTTest1.m// Created by HMT on 14-8-3.//#import "HMTTest1.h"@implementation HMTTest1- (NSString *)addTestString:(NSString *)testString{ NSString *string = [NSString stringWithFormat:@"這是一個關於%@的測試Demo", testString]; return string;}@end- (void)testOfNSInvocation{ /*******************************normal方式*******************************/ HMTTest1 *test1 = [[HMTTest1 alloc] init]; NSString *normalString = [test1 addTestString:@"直接調用方法"]; NSLog(@"normal = %@",normalString); NSLog(@"normal = %@",[test1 performSelector:@selector(addTestString:) withObject:@"performSelector:withObject"]); /*******************************NSInvocation方式*******************************/ NSString *testString = @"haha"; // 方法簽名類,需要被調用訊息所屬的類HMTTest1 ,被調用的訊息addTestString: NSMethodSignature *signature = [[HMTTest1 class] instanceMethodSignatureForSelector:@selector(addTestString:)]; // 根據方法簽名建立一個NSInvocation NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:signature]; // 設定調用者也就是HMTTest1的執行個體對象,也就是test1 [myInvocation setTarget:test1]; // 設定被調用的訊息 [myInvocation setSelector:@selector(addTestString:)]; // 如果此訊息有參數需要傳入,參數是void*類型,需要注意的是atIndex的下標必須從2開始。原因為:0 1 兩個參數已經被target 和selector佔用 //id(泛型對象)和void*(泛型指標)並非完全一樣 [myInvocation setArgument:&testString atIndex:2]; // 定義接收結果的變數 NSString *result = nil; // retain所有參數,防止參數被釋放dealloc [myInvocation retainArguments]; // 訊息調用 [myInvocation invoke]; // 將得到的結果賦值給定義的變數 [myInvocation getReturnValue: &result]; NSLog(@"The NSInvocation invoke string is: %@", result); // //獲得傳回值類型// const char *returnType = signature.methodReturnType;// // 聲明傳回值變數// id returnValue;// // 如果沒有傳回值,也就是訊息聲明為void,那麼returnValue=nil// if( !strcmp(returnType, @encode(void)) ){// // returnValue = nil;// // }// // 如果傳回值為對象,那麼為變數賦值// else if( !strcmp(returnType, @encode(id)) ){// // [myInvocation getReturnValue:&returnValue];// // }// else{// // } /*******************************@encode(aType) 可以返回該類型的 C 字串(char *)的表示*******************************/ NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"key1",[NSNumber numberWithDouble:100.00f],@"key2",[NSNumber numberWithInt:200],@"key3",[NSNumber numberWithFloat:300.0f], @"key4", nil]; for(NSString *key in dic){ id value = [dic valueForKey:key]; if([value isKindOfClass:[NSNumber class]]){ const char * pObjCType = [((NSNumber*)value) objCType]; if (strcmp(pObjCType, @encode(int)) == 0) { NSLog(@"字典中key=%@的值是int類型,值為%d",key,[value intValue]); } if (strcmp(pObjCType, @encode(float)) == 0) { NSLog(@"字典中key=%@的值是float類型,值為%f",key,[value floatValue]); } if (strcmp(pObjCType, @encode(double)) == 0) { NSLog(@"字典中key=%@的值是double類型,值為%f",key,[value doubleValue]); } if (strcmp(pObjCType, @encode(BOOL)) == 0) { NSLog(@"字典中key=%@的值是bool類型,值為%i",key,[value boolValue]); } } }}
一般將來時的基本用法是什?
一般將來時 一般將來時
am/is/are/going to + do和will/shall + do
1) shall用於第三人稱單數,常被will 所代替,二者都可以縮寫成'll。
will 在陳述句中用於各人稱,在徵求意見時常用於第二人稱。
Which paragraph shall I read first.
Will you be at home at seven this evening?
2) be going to +動詞原形\地點,表示將來。
a. 主語的意圖,即將做某事。
What are you going to do tomorrow?
b. 計劃,安排要發生的事。
The play is going to be produced next month。
c. 有跡象要發生的事
Look at the dark clouds, there is going to be a storm.
3) be +不定式表將來,按計劃或正式安排將發生的事。
We are to discuss the report next Saturday.
4) be about to +不定式,意為馬上做某事。
He is about to leave for Beijing.
注意:be about to 不能與tomorrow, next week 等表示明確將來時的時間狀語連用。
一般將來時
1.一般將來時的基本概念
一般將來時表示將來某一時刻的動作或狀態,或將來某一段時間內經常的動作或狀態。一般將來時由助動詞shall(第一人稱),will(第二、三人稱)+動詞原形構成。美國英語則不管什麼人稱,一律用will。
2.一般將來時的形式
●will 常簡略為 'll,並與主語連寫在一起,如:I'll,he'll,it'll,we'll,you'll,they'll。
●一般疑問句如用will you…?其簡略答語須是Yes,I will或 No,I will not;如用 Shall you…?(較少見)其簡略答語須是 Yes,I shall.或 No, I shall not.。
3.一般將來時的用法
1)表示將來的動作或狀態
一般將來時常與一些表示將來的時間狀語連用,如:
tomorrow(明天), next week(下周), from now on(從現在開始);in the future(將來)等。
2)表示將來經常發生的動作。
4.一般將來時的其他用法
一般將來時表示將來某一時刻的動作或狀態,其表達形式除了“shall(第一人稱),will(第二、三人稱)+動詞原形構成”外,還有以下幾種形式。
1)“to be going to+動詞原形”表示即將發生的或最近打算進行的事。例如:
①It is going to rain. 要下雨了。
②We are going to have a meeting today. 今天我們開會。
2)go, come,start,move,sail,leave,arrive,stay,live,fly,等可用進行時態表示按計劃即將發生的動作,例如:
I'm leaving for Beijing.
我要去北京。......餘下全文>>
it的基本用法
1、it的基本用法
(1)用作人稱代詞,代替前文提到過的事物。
The train has arrived. It arrived half an hour ago.
(2)用以代替提示代詞this, that。
—What’s this? —It’s a knife.
—Whose watch is that? —It’s mine.
(3)起指示代詞的作用,指一個人或事物。
—Who is knocking at the door? —It’s me.
(4)指環境情況等。
It was very noisy (quiet) at the very moment.
(5)指時間、季節等。
—What time is it? —It’s eight o’clock.
It often rains in summer here.
(6)指距離。
It is a long way to the school.
(7)作形式主語。
It is not easy to finish the work in two days.
It is no use crying over spilt milk.
It is as pity that you didn’t read the book.
(8)作形式賓語。
I think it no use arguing with him.
I found it very interesting to study English.
He made it clear that he was not interested in that subject.
(9)用於強調結構。
It was Xiaoming whom(that)I met in the street last night.
It was in the street that I met Xiaoming last night.
It ws last night that I met Xiaoming in the street.
It was I who met Xiaoming in the street last night.
2、含有“It is …”的句型
(1)It is time (for sb.) to do sth.
It is (high) time that sb. did sth.(虛擬語氣)
(2)It is + 形容詞(+of / for sb.)+ to do sth.
通常用
of的詞有brave、clever、careful、hopeless、kind、good、naughty、nice、silly、stupid、foolish、wise等。
(3)It is + 形容詞 + that + sb. + (should) do sth.(虛擬語氣)
能用於這個句型的形容詞有strange、wonderful、natural、good、proper、right、wrong、well、fortunate、important、necessary、useless、likely、probable、impossible等。有時可省去should而直接用動詞原形。如:
It is important that we should pay close attention to grain.
It is natural that he(should)say so......餘下全文>>