1.當參數obj為Object實例對象
創新互聯服務項目包括美蘭網站建設、美蘭網站制作、美蘭網頁制作以及美蘭網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,美蘭網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到美蘭省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
object_getClass(obj)與[obj class]輸出結果一直,均獲得isa指針,即指向類對象的指針。
2.當參數obj為Class類對象
object_getClass(obj)返回類對象中的isa指針,即指向元類對象的指針;[obj class]返回的則是其本身。
3.當參數obj為Metaclass類對象
object_getClass(obj)返回元類對象中的isa指針,因為元類對象的isa指針指向根類,所有返回的是根類對象的地址指針;[obj class]返回的則是其本身。
4.obj為Rootclass類對象
object_getClass(obj)返回根類對象中的isa指針,因為跟類對象的isa指針指向Rootclass‘s metaclass(根元類),即返回的是根元類的地址指針;[obj class]返回的則是其本身。
總結:
經上面初步的探索得知,object_getClass(obj)返回的是obj中的isa指針;而[obj class]則分兩種情況:一是當obj為實例對象時,[obj class]中class是實例方法:- (Class)class,返回的obj對象中的isa指針;二是當obj為類對象(包括元類和根類以及根元類)時,調用的是類方法:+ (Class)class,返回的結果為其本身。
更多: iOS面試題合集
1、統一收鍵盤的方法
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
2、提示框
BBAlertView *alert = [[BBAlertView alloc] initWithStyle:BBAlertViewStyleDefault
Title:@"刪除訂單"
message:@"是否刪除訂單,"
customView:nil
delegate:self
cancelButtonTitle:L(@"取消")
otherButtonTitles:L(@"確認")];
[alert setCancelBlock:^{
}];
[alert setConfirmBlock:^{
[self orderDidRemovePressDown:tempDic Index:index.section];
}];
[alert show];
3、圖片的自適應功能
self.brandImage.contentMode = UIViewContentModeScaleAspectFit;
4、cocoaPods清除緩存問題
$ sudo rm -fr ~/.cocoapods/repos/master
$ pod setup
5、設置顯示鍵盤的樣式
textView.keyboardType =UIKeyboardTypeDefault;
//設置鍵盤右下角為完成(中文輸入法下)
textView.returnKeyType=UIReturnKeyDone;
6、輸出當前時間
NSDateFormatter * dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"當前毫秒時間1==%@",[dateFormatter stringFromDate:[NSDate date]]);
7、顯示兩秒然后消失
UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(60,Main_Screen_Height-64-49-60, Main_Screen_Width-120, 50)];
lab.backgroundColor=[UIColor grayColor];
ViewRadius(lab, 20);
lab.textAlignment=NSTextAlignmentCenter;
lab.text=@"請先進行實名制驗證";
[self.view addSubview:lab];
[UILabel animateWithDuration:2 animations:^{
lab.alpha=0;
}completion:^(BOOL finished) {
[lab removeFromSuperview];
}];
8、設置placeholder屬性的大小和顏色
[_phoneFie setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
[_phoneFie setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
_phoneFie.returnKeyType=UIReturnKeyDone;
9、設置cell的交互完全不可以使用
//[cellTwo setUserInteractionEnabled:NO];
//設置cell不可以點擊,但是上面的子控件可以交互
cellTwo.selectionStyle=UITableViewCellSelectionStyleNone;
10、將textField的placeholder 屬性的字體向右邊移動5
_field.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10*Width_375, _field.frame.size.height)];
_field.leftViewMode = UITextFieldViewModeAlways;
11、開新線程使按鈕上的時間變化
-(void)startTime{
__block int timeout=60; //倒計時時間
UIButton * btn=(UIButton *)[self.view viewWithTag:1000];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒執行
dispatch_source_set_event_handler(_timer, ^{
if(timeout=0){
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
[btn setTitle:@"發送驗證碼" forState:UIControlStateNormal];
btn.enabled = YES;
});
}else{
//? int minutes = timeout / 60;
int miao = timeout % 60;
if (miao==0) {
miao = 60;
}
NSString *strTime = [NSString stringWithFormat:@"%.2d", miao];
dispatch_async(dispatch_get_main_queue(), ^{
[btn setTitle:[NSString stringWithFormat:@"剩余%@秒",strTime] forState:UIControlStateNormal];
btn.enabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
12、隱藏TableView 中多余的行
UIView * view=[[UIView alloc]initWithFrame:CGRectZero];
[_tabelView setTableFooterView:view];
13、UIView添加背景圖片
UIImage * image=[UIImage imageNamed:@"friend750"];
headSeV.layer.contents=(id)image.CGImage;
14、UITableView取消選中狀態
[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消選中
15、帶屬性的字符串
NSFontAttributeName? 字體
NSParagraphStyleAttributeName? 段落格式
NSForegroundColorAttributeName? 字體顏色
NSBackgroundColorAttributeName? 背景顏色
NSStrikethroughStyleAttributeName 刪除線格式
NSUnderlineStyleAttributeName? ? ? 下劃線格式
NSStrokeColorAttributeName? ? ? ? 刪除線顏色
NSStrokeWidthAttributeName 刪除線寬度
NSShadowAttributeName? 陰影
1.? 使用實例
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
testLabel.backgroundColor = [UIColor lightGrayColor];
testLabel.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天氣不錯呀"];
[AttributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16.0]
range:NSMakeRange(2, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(2, 2)];
testLabel.attributedText = AttributedStr;
[self.view addSubview:testLabel];
16、加大按鈕的點擊范圍
把UIButton的frame 設置的大一些,然后給UIButton設置一個小些的圖片
[tmpBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
// 注意這里不能用setBackgroundImage
[tmpBtn setImage:[UIImage imageNamed:@"testBtnImage"] forState:UIControlStateNormal];
17、//避免self的強引用
__weak ViewController *weakSelf = self;
18、//類別的創建
command +n ——Objective-C File———(File Type? 選擇是類別還是擴展)———(Class? 選擇為哪個控件寫類別)
19、修改UITableview 滾動條顏色的方法
self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
20、利用UIWebView顯示pdf文件
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[webView setDelegate:self];
[webView setScalesPageToFit:YES];
[webViewsetAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
[webView setAllowsInlineMediaPlayback:YES];
[self.view addSubview:webView];
NSString *pdfPath = [[NSBundle mainBundle]pathForResource:@"ojc" ofType:@"pdf"];
NSURL *url = [NSURLfileURLWithPath:pdfPath];
NSURLRequest *request = [NSURLRequestrequestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:5];
[webView loadRequest:request];
21、將plist文件中的數據賦給數組
NSString *thePath = [[NSBundle mainBundle]pathForResource:@"States" ofType:@"plist"];
NSArray *array = [NSArrayarrayWithContentsOfFile:thePath];
22、隱藏狀態欄
[[UIApplication shareApplication]setStatusBarHidden: YES animated:NO];
23、給navigation? Bar? 設置title顏色
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
24、使用AirDrop 進行分享
NSArray *array = @[@"test1", @"test2"];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];
[self presentViewController:activityVC animated:YES
completion:^{
NSLog(@"Air");
}];
25、把tableview里面Cell的小對勾的顏色改成別的顏色
_mTableView.tintColor = [UIColor redColor];
26、UITableView去掉分割線
_tableView.separatorStyle = NO;
27、正則判斷手機號碼地址格式
- (BOOL)isMobileNumber:(NSString *)mobileNum {
//? ? 電信號段:133/153/180/181/189/177
//? ? 聯通號段:130/131/132/155/156/185/186/145/176
//? ? 移動號段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
//? ? 虛擬運營商:170
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];
}
28、控制交易密碼位數
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField.text.length =6){
[MBProgressHUD showMessage:@"密碼為6位" afterDelay:1.8];
return NO;
}
return YES;
}
29、判斷是不是空
if ([real_name isKindOfClass:[NSNull class]] ) {
return NO;}
30、點擊號碼撥打電話
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://400966220"]];
31、控制UITabbar的選擇哪一個
[self.tabBarController setSelectedIndex:1];
32、獲取當前App的版本號
NSDictionary?*infoDictionary?=?[[NSBundle?mainBundle]?infoDictionary];
CFShow(infoDictionary);
//?app名稱
NSString?*app_Name?=?[infoDictionary?objectForKey:@"CFBundleDisplayName"];
//?app版本
NSString?*app_Version?=?[infoDictionary?objectForKey:@"CFBundleShortVersionString"];
//?app?build版本
NSString?*app_build?=?[infoDictionary?objectForKey:@"CFBundleVersion"];
33、蘋果app權限NSPhotoLibraryUsageDescriptionApp需要您的同意,才能訪問相冊NSCameraUsageDescriptionApp需要您的同意,才能訪問相機NSMicrophoneUsageDescriptionApp需要您的同意,才能訪問麥克風NSLocationUsageDescriptionApp需要您的同意,才能訪問位置NSLocationWhenInUseUsageDescriptionApp需要您的同意,才能在使用期間訪問位置NSLocationAlwaysUsageDescriptionApp需要您的同意,才能始終訪問位置NSCalendarsUsageDescriptionApp需要您的同意,才能訪問日歷NSRemindersUsageDescriptionApp需要您的同意,才能訪問提醒事項NSMotionUsageDescriptionApp需要您的同意,才能訪問運動與健身NSHealthUpdateUsageDescriptionApp需要您的同意,才能訪問健康更新NSHealthShareUsageDescriptionApp需要您的同意,才能訪問健康分享NSBluetoothPeripheralUsageDescriptionApp需要您的同意,才能訪問藍牙NSAppleMusicUsageDescriptionApp需要您的同意,才能訪問媒體資料庫
34、控件設置邊框
_describText.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
_describText.layer.borderWidth = 1.0;
_describText.layer.cornerRadius = 4.0;
_describText.clipsToBounds = YES;
35、//隱藏電池條的方法
-(BOOL)prefersStatusBarHidden{
return YES;
}
36、延時操作
[NSThread sleepForTimeInterval:2];
方法二:
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.5];
37、系統風火輪:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //隱藏
38、//didSelectRowAtIndexPath:方法里面找到當前的Cell
AssessMentCell * cell = [tableView cellForRowAtIndexPath:indexPath];
39、navigation上返回按鈕的顏色以及返回按鈕后面文字去掉
//返回按鈕后邊文字去掉
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
//設置左上角返回按鈕的顏色
self.navigationController.navigationBar.tintColor = UIColorFromRGB(0x666666);
40、lineBreakMode //設置文字過長時的顯示格式
label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯
示,后面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內
容長度,后半部分被刪除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字
以……方式省略,顯示尾部文字內容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內容
以……方式省略,顯示頭尾的文字內容。
label.lineBreakMode = NSLineBreakByTruncatingTail;結尾部分的內容
以……方式省略,顯示頭的文字內容。
label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯
示,后面部分省略不顯示。
為了更好理解使用用tabbar和切換視圖,我們創建一個Empty Application。
1、 打開Xcode ,新建項目
2、 創建View Controller
在項目上按花鍵+N創建新文件,創建 Objective-C class 文件,按Next按鈕,subClass 選UIViewController 。勾選上xib選項
以同樣方式創建另外三個ViewController ,RedViewController ,GreyViewController,YellowViewController。四個View準備好了。那么Tabbar呢?
3、 創建TabBarController.xib文件,選擇創建Empty文件
這時候你發現創建的xib文件是空白的,不用慌,去右下角控件欄中把TabBar Controller拖過來就Ok了。
4、 關聯TabBarController.xib ,tabbarAppDelegate這兩個文件
在上圖中選擇File’s Owner,打開Identity Inspector,在Class一欄選擇tabbarAppDelegate
這樣,我們就可以創建TabBarController.xib 文件指向tabbarAppDelegate 文件的Outlet映射了。
5、 在Xcode中的工具欄的View菜單找到 打開Assistant Editor,使tabbarAppDelegate.h和TabBarController.xib 同時打開。
在xib文件上按住control鍵,往tabbarAppDelegate.h,創建Outlet.
彈出窗口輸入 rootController,點connect。
6、 添加代碼
打開tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代碼:
1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
3. // Override point for customization after application launch.
4. [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];
5. [self.window addSubview:self.rootController.view];
6. self.window.backgroundColor = [UIColor whiteColor];
7. [self.window makeKeyAndVisible];
8. return YES;
7、 往TabBarController.xib上添加Tab Bar Item,
把控件欄上的Tab Bar Item控件往TabBarController.xib上拖拽即可,一個放4個。
8 、關聯Tab Bar Item和***ViewController。
選擇其中一個Tab Bar Item,在右上角打開Identity Inspector,在Class中選擇BlueViewController:
然后,打開Attribute,在NIB Name選擇BlueViewController:
其他3個tab item重復類似的操作,選中對應的ViewController,這樣在切換Tab標簽時,就可以切換到對應的頁面。
9、 設置tab item的屬性
選中其中一個tab item ,會在右上角的屬性欄里看到如下信息
Badge是紅色圈圈里面有數字 ,表示有多少條信息的屬性
Identifier 是tab item的樣式,選custom是自定義,下面的是系統的樣式。我選了其中四種。
bar ITem 的title image在custom的樣式下能設置。
10 、剩下的3個Tab Item也做類似的設置即可。
現在基本完工,運行看看結果如何。好吧,其實和第一第二個圖是一樣的`,這里就不放了。
11 、在viewDidLoad方法加Log觀察切換View
可以加寫日志看看對應的View是什么時候運行的。第一個運行的View是BlueViewController,點擊其他的tab項時,加載其他的view,加載一次之后下次點擊不再調用viewDidLoad。
1.- (void)viewDidLoad
2. [super viewDidLoad];
3. NSLog(@"BlueViewController");
4. // Do any additional setup after loading the view from its nib.
#import與@class的區別
1.import會包含這個類的所有信息,包括實體變量和方法,而@class只是告訴編譯器,其后面聲明的名稱是類的名稱,至于這些類是如何定義的,暫時不用考慮,后面會再告訴你。
2.在頭文件中, 一般只需要知道被引用的類的名稱就可以了。 不需要知道其內部的實體變量和方法,所以在頭文件中一般使用@class來聲明這個名稱是類的名稱。 而在實現類里面,因為會用到這個引用類的內部的實體變量和方法,所以需要使用#import來包含這個被引用類的頭文件。
3.在編譯效率方面考慮,如果你有100個頭文件都#import了同一個頭文件,或者這些文件是依次引用的,如A–B, B–C, C–D這樣的引用關系。當最開始的那個頭文件有變化的話,后面所有引用它的類都需要重新編譯,如果你的類有很多的話,這將耗費大量的時間。而是用@class則不會。
4.如果有循環依賴關系,如:A–B, B–A這樣的相互依賴關系,如果使用#import來相互包含,那么就會出現編譯錯誤,如果使用@class在兩個類的頭文件中相互聲明,則不會有編譯錯誤出現。
所以,一般來說,@class是放在interface中的,只是為了在interface中引用這個類,把這個類作為一個類型來用的。 在實現這個接口的實現類中,如果需要引用這個類的實體變量或者方法之類的,還是需要import在@class中聲明的類進來.
#import和#include的區別
當我們在代碼中使用兩次#include的時候會報錯:因為#include相當于拷貝頭文件中的聲明內容,所以會報重復定義的錯誤
但是使用兩次#import的話,不會報錯,所以他可以解決重復導入的問題,他會做一次判斷,如果已經導入一次就不導入了
本文名稱:ios開發class,iOs開發判斷勾股數
文章源于:http://vcdvsql.cn/article4/dsdicie.html
成都網站建設公司_創新互聯,為您提供商城網站、網站維護、網站導航、網站設計公司、品牌網站制作、定制開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯