今天參加了某公司的面試,答得感覺懵逼的,下面是面試題
1.@import跟#include的區別
(1)#import指令是Object-C針對#include的改進版本,#import確保引用的文件只會被引用一次,這樣就不會陷入遞歸包含的問題中。
(2)#import與@class二者的區別在于:
#import會鏈入該頭文件的全部信息,包括實體變量和方法等;而@class只是告訴編譯器,其后面聲明的名稱是類的名稱,至于這些類是如何定義的,暫時不用考慮。在頭文件中, 一般只需要知道被引用的類的名稱就可以了。
不需要知道其內部的實體變量和方法,所以在頭文件中一般使用@class來聲明這個名稱是類的名稱。 而在實現類里面,因為會用到這個引用類的內部的實體變量和方法,所以需要使用#import來包含這個被引用類的頭文件。
2.如何向類別中添加屬性?
static char*PersonNameKey ="PersonNameKey";
//name是你在頭文件中的屬性名稱,PersonNameKey就相當于是一個keyId了
- (void)setName:(NSString*)name {
objc_setAssociatedObject(self,PersonNameKey, name,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString*)name {
return objc_getAssociatedObject(self,PersonNameKey);
}
//demo可以參見https://github.com/miliPolo/CategoryAddPropertyByRuntime
3.關于mrc模式下block內存泄露的問題
先看下面這段代碼
@autoreleasepool {
Person *p = [[Person alloc] init];
p.age = 30;
void (^block) () = ^{
NSLog(@"%d", p.age);
};
Block_copy(block);
block();
[p release];
}
此時此刻,有人想對block進行release,但是不管你是否對block進行release ,都無法釋放d對象。為什么呢??
這個時候,我們就要對block進行分析了:
1.默認情況下, block的內存是在棧中
*它不會對所引用的對象進行任何操作
2.如果對block做一次copy操作, block的內存就會在堆中
*它會對所引用的對象做一次retain操作*非ARC : 如果所引用的對象用了__block修飾, 就不會做retain操作*ARC : 如果所引用的對象用了__unsafe_unretained\__weak修飾, 就不會做retain操作
所以,只需要在Dog類前面加上__block即可!
4.block實現原理詳解
先來看下面兩份代碼
(1).int age = 10;
void (^block)() = ^{
NSLog(@"%d", age);
};
age = 30;
block();//10
(2).__block int age = 10;
void (^block)() = ^{
NSLog(@"%d", age);
};
age = 30;
block();//30
兩個結果是不一樣的,為何呢?關鍵在于第二段代碼變量age 使用__block進行修飾,第一段代碼的變量age其實是值傳遞,而第二份代碼是引用傳遞
block內部是調用了一個結構體中的函數:
static struct?main_block_desc_0 {
size_t reserved;
size_t Block_size;
void (copy)(struct __main_block_impl_0, structmain_block_impl_0);
void (dispose)(struct __main_block_impl_0*);
}
5.iOS安全
主要涉及到三個方面:網絡通信,本地文件和數據,源代碼安全
網絡通信方面:
1).禁止使用明文傳輸密碼,正確的做法應該是這樣:事先生成一對用于加密的公私鑰,客戶端在登錄時,使用公鑰將用戶的密碼加密后,將密文傳輸到服務器。服務器使用私鑰將密碼解密,然后加鹽(Salt,在密碼學中是指,通過在密碼任意固定位置插入特定的字符串,讓散列后的結果和使用原始密碼的散列結果不相符,這個過程稱之為“加鹽”),之后再多次求MD5,然后再和服務器原來存儲的用同樣方法處理過的密碼匹配,如果一致,則登錄成功。這樣的做法,保證黑客即使截獲了加密后的密文,由于沒有私鑰,也無法還原出原始的密碼。而服務器即使被黑客攻陷,黑客除了暴力嘗試,也無法從加鹽和多次MD5后的密碼中還原出原始的密碼。這樣就保證了用戶密碼的安全。
2).防止通信協議被輕易破解
3).驗證應用內的支付憑證
ProtoBuf
本地文件和數據安全
程序文件的安全
本地數據的安全
源代碼的安全
6.iOS使用的第三方通訊庫
xmpp
7.支付寶的支付流程
8.viewController的生命周期
init->loadView->viewDidLoad->viewWillAppear->viewDidAppear->viewWillDisappear->viewDidDisappear->viewDidUnload->dealloc
9.二維碼掃描,相冊權限判斷
NSString*mediaType =AVMediaTypeVideo;// Or AVMediaTypeAudio
AVAuthorizationStatusauthStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];
if(authStatus ==AVAuthorizationStatusRestricted){
NSLog(@"Restricted");
}elseif(authStatus ==AVAuthorizationStatusDenied){
UIAlertView*alert =[[UIAlertViewalloc]initWithTitle:@"無法啟動相機"message:@"請在iPhone的“設置”-“隱私”-“相機”功能中,找到“聰明屋”打開相機訪問權限"delegate:nilcancelButtonTitle:@"確定"otherButtonTitles:nil];
[alertshow];
return;
}
elseif(authStatus ==AVAuthorizationStatusAuthorized){//允許訪問
QRCodeReaderViewController*reader = [[QRCodeReaderViewControlleralloc]init];
if(reader) {
reader.modalPresentationStyle=UIModalPresentationFormSheet;
__weaktypeof(self) wSelf =self;
[readersetCompletionWithBlock:^(NSString*resultAsString) {
[wSelfdismissViewControllerAnimated:YEScompletion:NULL];
self.textField.text= resultAsString;
}];
[selfpresentViewController:readeranimated:YEScompletion:NULL];
}
}elseif(authStatus ==AVAuthorizationStatusNotDetermined){
[AVCaptureDevicerequestAccessForMediaType:mediaTypecompletionHandler:^(BOOLgranted) {
if(granted){//點擊允許訪問時調用
//用戶明確許可與否,媒體需要捕獲,但用戶尚未授予或拒絕許可。
NSLog(@"Granted access to %@", mediaType);
QRCodeReaderViewController*reader = [[QRCodeReaderViewControlleralloc]init];
if(reader) {
reader.modalPresentationStyle=UIModalPresentationFormSheet;
__weaktypeof(self) wSelf =self;
[readersetCompletionWithBlock:^(NSString*resultAsString) {
[wSelfdismissViewControllerAnimated:YEScompletion:NULL];
self.textField.text= resultAsString;
}];
[selfpresentViewController:readeranimated:YEScompletion:NULL];
}
}
else{
NSLog(@"Not granted access to %@", mediaType);
}
}];
}else{
NSLog(@"Unknown authorization status");
}
10.最近遇到一個非常坑爹的問題,我在用Reachability的時候,編譯老是會出現先的錯誤,Duplicate interface definition for class?,一開始以為合代碼的時候配置文件除了問題,我刪了pods的東西,重新更新了一遍,但是并沒有什么用。想了下#import不會交叉編譯頭文件啊,可是還是出現了重定義錯誤,我在代碼里面搜了頭文件就只有一個啊?這是開什么國際玩笑啊,后來在Finder里面重新搜了一下,果然在我合代碼的時候,有份代碼沒有刪除只是去除引用了,悲傷~~~
11.UITabBar改變默認高度
1.繼承UITabBarController
@interfaceALTabBarController:UITabBarController
2.重寫UITabBarController的UITabBarController方法
- (void)viewWillLayoutSubviews{CGRecttabFrame =self.tabBar.frame;//self.TabBar is IBOutlet of your TabBartabFrame.size.height=80;? ? tabFrame.origin.y=self.view.frame.size.height-80;self.tabBar.frame= tabFrame;}
如果想要添加的工具條點擊事件生效請在viewWillLayoutSubviews添加此代碼
[self.tabBarbringSubviewToFront:self.bottomToolView];
12.UITextField placeholder的使用
我們有時需要定制化UITextField對象的風格,可以添加許多不同的重寫方法,來改變文本字段的顯示行為。這些方法都會返回一個CGRect結構,制定了文本字段每個部件的邊界范圍,甚至修改placeHolder顏色,字體。
– textRectForBounds://重寫來重置文字區域
– drawTextInRect://改變繪文字屬性.重寫時調用super可以按默認圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了.
– placeholderRectForBounds://重寫來重置占位符區域
– drawPlaceholderInRect://重寫改變繪制占位符屬性.重寫時調用super可以按默認圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了
– borderRectForBounds://重寫來重置邊緣區域
– editingRectForBounds://重寫來重置編輯區域
– clearButtonRectForBounds://重寫來重置clearButton位置,改變size可能導致button的圖片失真
– leftViewRectForBounds:
– rightViewRectForBounds:
通過– drawPlaceholderInRect:方法可改變placeHolder顏色、字體,請看代碼:
首先定義一個類CustomTextField讓它繼承UITextField實現以下方法即可:
//控制清除按鈕的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
{
returnCGRectMake(bounds.origin.x+ bounds.size.width-50, bounds.origin.y+ bounds.size.height-20,16,16);
}
//控制placeHolder的位置,左右縮20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
//return CGRectInset(bounds, 20, 0);
CGRectinset =CGRectMake(bounds.origin.x+100, bounds.origin.y, bounds.size.width-10, bounds.size.height);//更好理解些
returninset;
}
//控制顯示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
//return CGRectInset(bounds, 50, 0);
CGRectinset =CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width-10, bounds.size.height);//更好理解些
returninset;
}
//控制編輯文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
//return CGRectInset( bounds, 10 , 0 );
CGRectinset =CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height);
returninset;
}
//控制左視圖位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
CGRectinset =CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-250, bounds.size.height);
returninset;
//return CGRectInset(bounds,50,0);
}
//控制placeHolder的顏色、字體
- (void)drawPlaceholderInRect:(CGRect)rect
{
//CGContextRef context = UIGraphicsGetCurrentContext();
//CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
[[UIColororangeColor]setFill];
[[selfplaceholder]drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
}
//下面是使用CustomTextField的代碼,可放在viewDidLoad等方法中
_textField= [[CustomTextFieldalloc]initWithFrame:CGRectMake(20,150,280,30)];
_textField.placeholder=@"請輸入帳號信息";
_textField.borderStyle=UITextBorderStyleRoundedRect;
_textField.textAlignment=UITextAlignmentLeft;
_textField.delegate=self;
_textField.clearButtonMode=UITextFieldViewModeWhileEditing;
_textField.text=@"aa";
UIImageView*imgv = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"icon-iwant-2.png"]];
_textField.leftView= imgv;
_textField.leftViewMode=UITextFieldViewModeAlways;
[self.viewaddSubview:_textField];
//設置NavigationController跳轉動畫
/*
NSString?*types[4]?=?{kCATransitionMoveIn,?kCATransitionPush,?kCATransitionReveal,?kCATransitionFade};
NSString?*subtypes[4]?=?{kCATransitionFromLeft,?kCATransitionFromRight,?kCATransitionFromTop,?kCATransitionFromBottom};
//立方?吸走?翻轉?水波?翻頁?翻頁回
NSString?*moreTypes[]={@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};
*/
CATransition?*animation?=?[CATransition?animation];
[animation?setDuration:0.4];
[animation?setType:?@"oglFlip"];
[animation?setSubtype:?kCATransitionFromLeft];
[animation?setTimingFunction:[CAMediaTimingFunction?functionWithName:kCAMediaTimingFunctionLinear]];
[self.navigationController.view.layer?addAnimation:animation?forKey:nil];
[self.navigationController?popViewControllerAnimated:NO];
//設置Frame失效問題
如果有AutoLayout或者SizeClass,在Xib上的控件無法通過Frame修改位置。如果是手工創建的View,是可以通過Frame移動的。
如果要移動SizeClass,可以映射它的Constraint,在代碼中修改Constraint.constant.
12.enumeration value not handled in switch
use default and break;
13 pod 導入第三方庫后,使用import 不提示第三方庫頭文件。
解決辦法:
選擇target -> BuildSettings -> search Paths 下的 User Header Search Paths, 添加$(PODS_ROOT)
持續更新中...