### 牛客網(wǎng) iOS 題 107-126
107.NSRunLoop 以下描述錯(cuò)誤的是( )
- RunLoop 并不是由系統(tǒng)自動(dòng)控制的
- 有三類對(duì)象可以被 RunLoop 監(jiān)控:sources,timers,observers
- 線程是默認(rèn)啟動(dòng) RunLoop 的
- NSTimer 可手動(dòng)添加到新建的 NSRunLoop 中
答案:<font color=“LightYellow”>3</font>
- RunLoop 的作用在于當(dāng)有事情要做的時(shí)候它使當(dāng)前的 thread 工作,沒(méi)有事情做時(shí)又使 thread 休眠 sleep。RunLoop 并不是由系統(tǒng)自動(dòng)控制的,尤其是對(duì)那些新建的次線程需要對(duì)其進(jìn)行顯示的控制。
- 每一個(gè)線程都有自己的 RunLoop,主線程是默認(rèn)開(kāi)啟的,創(chuàng)建的子線程需要手動(dòng)開(kāi)啟,因?yàn)?NSApplication 只啟動(dòng) main application thread。
- NSTimer 默認(rèn)添加到當(dāng)前的 NSRunLoop 中,也可以手動(dòng)制定添加到自己新建的 NSRunLoop 中
108.參考90題
109.參考22題
110.參考20題
111.下面關(guān)于線程管理錯(cuò)誤的是( )
- GCD 在后端管理著一個(gè)線程池
- NSOperationQueue 是對(duì) NSThread 的更高層封裝
- NSThread 需要自己管理線程的生命周期
- GCD 可以根據(jù)不同優(yōu)先級(jí)分配線程
答案:<font color=“LightYellow”>2</font>
NSOperationQueue 是對(duì) GCD 的更高層封裝
112.關(guān)于 Objective-C 中屬性的說(shuō)明,以下錯(cuò)誤的是( )
- readwrite 是可讀可寫(xiě)特性,需要生成 getter方法和 setter 方法
- readonly 是只讀特性,只有 getter 方法,沒(méi)有 setter 方法
- assign 是賦值屬性,setter 方法將傳入?yún)?shù)賦值給實(shí)例變量
- retain 表示持有特性,copy 屬性表示拷貝屬性,都會(huì)建立一個(gè)相同的對(duì)象
答案:<font color=“LightYellow”>4</font>
copy 是創(chuàng)建一個(gè)新對(duì)象,retain 是創(chuàng)建一個(gè)指針,引用對(duì)象計(jì)數(shù)加1
113.C 和 Objective-C 的混合使用,以下描述錯(cuò)誤的是( )
- cpp 文件只能使用 C/C++ 代碼
- cpp 文件 include 的頭文件中,可以出現(xiàn) Objective-C 代碼
- mm 文件中混用 cpp 直接使用即可
- cpp 使用 Objective-C 的關(guān)鍵是使用接口,而不能直接使用代碼
答案:<font color=“LightYellow”>2</font>
cpp 文件以及頭文件都只能用 c/c++代碼
114.delegate 中的 property 使用以下哪個(gè)屬性( )
- assign
- retain
- copy
- strong
答案:<font color=“LightYellow”>1</font>
assign 防止循環(huán)引用
115.以下哪一段代碼不會(huì)拋出異常( )
NSArray *array = @[1, 2, 3]; NSNumber *number = array[3];NSDictionary *dict = @{@"key":nil};NSString *str = nil; NSString *str2 = [str substringFromIndex:3];NSString *str = @"hi"; NSString *str2 = [str substringFromIndex:3];
答案:<font color=“LightYellow”>3</font>
- OC 數(shù)組中的元素不能使基本數(shù)據(jù)類型,必須是對(duì)象,而且 array[3]下標(biāo)越界;
- 字典中鍵值對(duì)不能為 nil,運(yùn)行時(shí)崩潰
- OC 中向空對(duì)象發(fā)送消息,不執(zhí)行任何操作,不報(bào)錯(cuò),不崩潰
- 數(shù)組下標(biāo)越界,運(yùn)行時(shí)崩潰
116.參考題1
117. float x 與“零值”比較的 if 語(yǔ)句是?
if(x == 0)if(x < 0.00001f)if(fabs(x) < 0.00001f) 或 if(Math.abs(x) < 0.00001f)if(x > -0.00001f)
答案:<font color=“LightYellow”>3</font>
float 和 double 都是有精度限制,應(yīng)該用 |x-0| < err 來(lái)判斷,這里 |x-0| 表示絕對(duì)值,err 表示限定誤差。
118.一次鼠標(biāo)單擊會(huì)產(chǎn)生下列哪些事件( )
- onmousemove
- onmousedown
- onmouseup
- onclick
- onmouseover
答案:<font color=“LightYellow”>2,3,4</font>
119.UIView 的以下哪些屬性是 Animatable 的:( )
- backgroundColor
- opaque
- transform
- contentStretch
答案:<font color=“LightYellow”>1,3,4</font>
opaque 是 BOOL 型變量。如果要調(diào)整透明度,應(yīng)該是 alpha 屬性。
120.[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopModebeforeDate:[NSDate distantFuture]];這段代碼的作用是
- 設(shè)置當(dāng)前線程的運(yùn)行模式
- 暫停當(dāng)前線程一段時(shí)間
- 在當(dāng)前線程中進(jìn)行一次消息輪詢
- 設(shè)置當(dāng)前線程的最大運(yùn)行時(shí)間
答案:<font color=“LightYellow”>3</font>
進(jìn)行一次消息輪詢,監(jiān)聽(tīng)線程是否有消息輸入(default 模式),有就線程開(kāi)始工作,沒(méi)有就休眠。
121.下面的定義中,錯(cuò)誤的個(gè)數(shù)為:( )
NSString a[] = {@"abc", @"def"};
NSArray *a = @[@"abc", @"def"];
NSArray *a = [NSArray arrayWithObjects:@"abc", @"def"];
NSArray *a = [NSMutableArray arrayWithArray:@[@"abc"]];
答案:<font color=“LightYellow”>2</font>
NSString *a[] = {@"abc", @"def"};
NSArray *a = [NSArray arrayWithObjects:@"abc", @"def", nil];
122.應(yīng)用申明一下哪些權(quán)限可以被系統(tǒng)授予后臺(tái)運(yùn)行的權(quán)限?
- 視頻播放應(yīng)用
- 音樂(lè)播放應(yīng)用
- 地理位置應(yīng)用
- VoIP 應(yīng)用
- 雜志新聞?lì)愋枰聝?nèi)容的應(yīng)用
答案:<font color=“LightYellow”>2,3,4,5</font>
123.以下代碼欲在 NSOperation 子線程中異步請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù),請(qǐng)?jiān)谧⑨屘帉?xiě)上一段代碼以滿足要求。finished_代表異步請(qǐng)求結(jié)束的標(biāo)志變量。請(qǐng)選出有用的選項(xiàng):
- (void) start {
if (![self isCancelled]) {
connection_ = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:download_url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15] delegate:self];
if (connection_ != nil) {
executing_ = YES;
} else {
finished_ = YES;
}
while (!finished_) {
// 代碼放入此處
}
}
else {
finished_ = YES;
}
}
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithinterval:0.1]];[[NSThread currentThread]waitForTimeinterval:0.1];[NSThread sleepForTimeinterval:0.1];
答案:<font color=“LightYellow”>1</font>
只有主線程的NSRunLoop會(huì)默認(rèn)啟動(dòng),自己新起的線程,要自己手動(dòng)調(diào)用RunLoop。
這里新起了一個(gè)線程,調(diào)用NSURLConnection的異步方法,如果不設(shè)置RunLoop,則NSURLConnection的delegate函數(shù)不會(huì)被運(yùn)行,因?yàn)榫€程已經(jīng)結(jié)束。在NSURLConnection的delegate方法中去設(shè)置finished_變量為YES,線程才會(huì)退出while循環(huán)而結(jié)束。
124.在定義屬性時(shí),下面哪些數(shù)據(jù)類型可以使用 retain 和 copy:
- NSArray
- float
- int
- NSString
- NSNumber
- NSInteger
答案:<font color=“LightYellow”>1,4,5</font>
copy 是在數(shù)據(jù)類型聲明時(shí),選擇遵守 NSCopying 協(xié)議的才可以使用
9大基本類型不能使用 retain
NSInteger = long
125.C++/Java/Objective-C/C# 是如何體現(xiàn)面對(duì)對(duì)象思想的?
繼承、封裝、多態(tài) ?
126.iOS 系統(tǒng)提供了哪些手勢(shì)?選擇一個(gè)自己寫(xiě)代碼實(shí)現(xiàn)
- UITapGestureRecognizer敲擊手勢(shì)(單擊和雙擊)
- UIPanGestureRecognizer(拖動(dòng)手勢(shì))
- UIPinchGestureRecognizer(縮放手勢(shì))
- UISwipeGestureRecognizer(擦碰手勢(shì))
- UIRotationGestureRecognizer(旋轉(zhuǎn)手勢(shì))
- UILongPressGestureRecognizer(長(zhǎng)按手勢(shì))
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
gestureView.backgroundColor = [UIColor blackColor];
[self.view addSubview:gestureView];
/*---------------點(diǎn)擊手勢(shì)-------------*/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tap1Action:)];
//注意:一個(gè)點(diǎn)擊手勢(shì),只能識(shí)別一種手勢(shì),單擊和雙擊是不同的兩個(gè)手勢(shì)
//設(shè)置點(diǎn)擊的數(shù)量
tap1.numberOfTapsRequired = 1 ;
//設(shè)置點(diǎn)擊的個(gè)數(shù)
tap1.numberOfTouchesRequired = 1 ;
//往gestureView上添加一個(gè)手勢(shì)
[gestureView addGestureRecognizer:tap1];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (tap2Action:)];
tap2.numberOfTapsRequired = 2 ;
[gestureView addGestureRecognizer:tap2];
//如果參數(shù)中的手勢(shì)出發(fā)了,則自身失效
[tap1 requireGestureRecognizerToFail:tap2];
/*---------------輕掃手勢(shì)-------------*/
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipe:)];
// 設(shè)置方向---向上輕掃
swipeGesture. direction = UISwipeGestureRecognizerDirectionUp;
[gestureView addGestureRecognizer:swipeGesture];
/*---------------平移手勢(shì)-------------*/
// 平移手勢(shì)(滑動(dòng))
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (panAction:)];
[gestureView addGestureRecognizer:panGesture];
/*---------------長(zhǎng)按手勢(shì)-------------*/
// 長(zhǎng)按手勢(shì)
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (longPress:)];
//設(shè)置長(zhǎng)安的最短時(shí)間
longPressGesture.minimumPressDuration = 3;
[gestureView addGestureRecognizer:longPressGesture];
/*---------------旋轉(zhuǎn)手勢(shì)-------------*/
// 旋轉(zhuǎn)手勢(shì)
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc ] initWithTarget : self action : @selector (rotation:)];
[gestureView addGestureRecognizer:rotationGesture];
/*----------捏合手勢(shì)--------------*/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (pinchAction:)];
[gestureView addGestureRecognizer:pinch];
}
//單擊
- (void)tap1Action:(UITapGestureRecognizer *)tap1
{
NSLog(@" 單擊 ");
}
//雙擊
- (void)tap2Action:(UITapGestureRecognizer *)tap2
{
NSLog(@" 雙擊 ");
}
//輕掃
- ( void )swipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe. direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@" 向上清掃 ");
}
}
//平移
- ( void )panAction:(UIPanGestureRecognizer *)pan
{
CGPoint p = [pan locationInView:pan.view];
NSLog( @"%@" , NSStringFromCGPoint(p));
}
//長(zhǎng)按
- ( void )longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress. state == UIGestureRecognizerStateBegan) {
NSLog(@" 開(kāi)始長(zhǎng)按 ");
} else if (longPress. state == UIGestureRecognizerStateEnded) {
NSLog(@" 結(jié)束長(zhǎng)按 ");
}
}
//旋轉(zhuǎn)
- ( void )rotation:(UIRotationGestureRecognizer *)rotation
{
//獲取旋轉(zhuǎn)角度---弧度
CGFloat r = rotation.rotation;
//弧度轉(zhuǎn)角度
//180/Pi = x/r
NSLog(@"r is %.2f", 180 / M_PI * r);
}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
//獲得捏合的比例
CGFloat scale = pinch.scale;
// pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}