中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS從入門到精通

2018-07-20    來源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

一、iPhone機(jī)型適配

設(shè)備型號(hào) 屏幕尺寸
iPhone 4/4S 320 * 480
iPhone 5/5C/5S 320 * 568
iPhone 6/6S/7 375 * 667
iPhone 6Plus/6SPlus/7Plus 414 * 736

機(jī)型適配

二、mian()函數(shù)的作用

(1)創(chuàng)建了一個(gè)自動(dòng)釋放池。

(2)調(diào)用UIApplicationMain()函數(shù)。

(3)釋放自動(dòng)釋放池。

main.m文件

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

//整個(gè)APP程序的主函數(shù),入口函數(shù)
int main(int argc, char * argv[]) {
    //自動(dòng)內(nèi)存釋放池
    @autoreleasepool {
        //UIKit框架結(jié)構(gòu)的啟動(dòng)函數(shù)
        //參數(shù)1:  argc    啟動(dòng)時(shí)帶有參數(shù)的個(gè)數(shù)
        //參數(shù)2:  argv    參數(shù)列表
        //參數(shù)3:  nil     要求傳入一個(gè)主框架類類名,如果參數(shù)為nil系統(tǒng)會(huì)用默認(rèn)的框架類作為主框架類名
        //參數(shù)4:  主框架的代理類對(duì)象名字
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

三、iOS應(yīng)用的生命周期

iOS應(yīng)用程序擁有的5種狀態(tài):

(1)Not running 應(yīng)用還沒啟動(dòng)或正在運(yùn)行但是中途被系統(tǒng)停止

(2)Inactive 應(yīng)用正在前臺(tái)運(yùn)行(不接收事件)

(3)Active 應(yīng)用正在前臺(tái)運(yùn)行(接收事件)

(4)Background 應(yīng)用處于后臺(tái)運(yùn)行(還在執(zhí)行代碼)

(5)Suspended 應(yīng)用處于后臺(tái)運(yùn)行(停止執(zhí)行代碼)

iOS程序的生命周期

#pragma         //////////生命周期有關(guān)函數(shù)//////////
//應(yīng)用將要進(jìn)入非活動(dòng)調(diào)用    (不接受消息或事件)
- (void)applicationWillResignActive:(UIApplication *)application;
//應(yīng)用進(jìn)入活動(dòng)調(diào)用          (接收消息或事件)
- (void)applicationDidBecomeActive:(UIApplication *)application;
//應(yīng)用進(jìn)入后臺(tái)調(diào)用          (設(shè)置后臺(tái)繼續(xù)運(yùn)行)
- (void)applicationDidEnterBackground:(UIApplication *)application;
//應(yīng)用將要進(jìn)入前臺(tái)調(diào)用    
- (void)applicationWillEnterForeground:(UIApplication *)application;
//應(yīng)用將要退出調(diào)用          (保存數(shù)據(jù),退出前清理)
- (void)applicationWillTerminate:(UIApplication *)application;
//應(yīng)用被終止前調(diào)用          (內(nèi)存清理,方式應(yīng)用被終止)
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
//應(yīng)用載入后調(diào)用           
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
//應(yīng)用打開URL時(shí)調(diào)用
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

四、UIViewController生命周期

(1)第一次訪問UIViewController的view時(shí),view為nil,然后就會(huì)調(diào)用loadView方法創(chuàng)建view,通過懶加載的方式進(jìn)行加載。

(2)重寫loadView方法,可以根據(jù)重寫loadView方法創(chuàng)建View。

(3)View創(chuàng)建完畢后會(huì)調(diào)用viewDidLoad方法進(jìn)行界面元素的初始化。

(4)StoryBoard加載的是控制器及控制器View。

(5)XIB加載的僅僅是控制器View。

ViewController生命周期

#pragma         //////////生命周期有關(guān)函數(shù)//////////
- (void)loadView;                            //加載視圖資源并初始化視圖(負(fù)責(zé)創(chuàng)建UIViewController的View)
- (void)viewDidLoad;                         //視圖加載完畢(只執(zhí)行一次,界面初始化操作,往View上添加子視圖)
- (void)viewWillAppear:(BOOL)animated;       //視圖將要顯示
- (void)viewDidAppear:(BOOL)animated;        //視圖顯示
- (void)viewWillDisappear:(BOOL)animated;    //視圖將要消失
- (void)viewDidDisappear:(BOOL)animated;     //視圖消失

五、Cocoa中的類

#pragma    -核心類
NSObject                //根類           (所有類的根類,定義了所有類共有的方法,如alloc,init)
UIApplication           //應(yīng)用程序類      (應(yīng)用運(yùn)行期間的控制和協(xié)作工作)    
[UIApplication sharedApplication];       //獲取應(yīng)用程序?qū)嵗?UIWindow                //窗口類          (管理和顯示視圖的容器)
UIView                  //視圖類          (管理矩形區(qū)域內(nèi)的所有屏幕顯示)
#pragma    -響應(yīng)者鏈:
UIResponder             //響應(yīng)者類        (接收屏幕上的觸摸事件)
    - (void)touchesBegan: withEvent:;        //觸摸開始
    - (void)touchesMoved: withEvent:;        //觸摸移動(dòng)
    - (void)touchesEnded: withEvent:;        //觸摸結(jié)束
    - (void)touchesCancelled: withEvent:;    //觸摸取消
UIControl                //控件類        (根據(jù)觸摸事件觸發(fā)操作)
UIViewController         //視圖控制器類   (管理視圖的內(nèi)容)
#pragma    -數(shù)據(jù)類型類
NSString/NSMutableString                //字符串
NSArray/NSMutableArray                  //數(shù)組
NSDictionary/NSMutableDictionary        //字典
NSNumber/NSDecimalNumber                //數(shù)字
NSDate                                  //日期
NSURL                                   //url
NSData                                  //二進(jìn)制
#pragma    -UI界面類
UILabel                                 //標(biāo)簽
UIButton                                //按鈕
UISlider                                //滑塊
UIStepper                               //步進(jìn)
UIImage/UIImageView                     //圖片
UISwitch                                //開關(guān)
UISegmentedControl                      //分段
UIWebView                               //網(wǎng)頁
UIScrollView                            //滾動(dòng)
UIPageControl                           //翻頁
UIProgressView                          //進(jìn)度
UIActivityIndicatorView                 //活動(dòng)指示器
UITextField/UITextView                  //文本框
UIToolbar                               //工具欄
UIDatePicker/UIPickerView               //選擇器
UIColor                                 //顏色
UITableView                             //列表
UICollectionView                        //網(wǎng)格
UIAlertController                        //選擇控制器
UIPopoverController                      //彈框控制器
UINavigationController/UINavigationBar     //導(dǎo)航控制器
UITabBarController/UITabBar                //選項(xiàng)控制器
UISearchController/UISearchBar             //搜索控制器
UIImagePickerController                    //圖像選擇控制器

六、響應(yīng)者鏈

響應(yīng)者鏈

先向下找,再向上找的一個(gè)過程。

所有的iOS中關(guān)于的界面的類都直接或間接地繼承與UIResponder。

(1)當(dāng)用戶點(diǎn)擊某一個(gè)視圖或者按鈕的時(shí)候會(huì)首先響應(yīng)application中UIWindow一層一層的向下查找,直到找到用戶指定的view為止。(向下查找)

#pragma    -查找用戶點(diǎn)擊的View
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;   
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;

(2)查看指定的View是否響應(yīng)了點(diǎn)擊事件,如果沒有找它的nextResponder,再繼續(xù)一層一層的向上查找,直到找到AppDelegate,再?zèng)]有響應(yīng),則此點(diǎn)擊事件被系統(tǒng)丟棄。(向上查找)

點(diǎn)擊查找View

#pragma    -查找用戶點(diǎn)擊的View響應(yīng)事件
- (nullable UIResponder*)nextResponder;

七、空指針和野指針的區(qū)別

空指針: 沒有存儲(chǔ)任何內(nèi)存地址的指針就稱為空指針(NULL指針)?罩羔樉褪潜毁x值為0的指針,在沒有被具體初始化之前,其值為0。

野指針: 指向一個(gè)已刪除的對(duì)象或未申請(qǐng)?jiān)L問受限內(nèi)存區(qū)域的指針。"野指針"不是NULL指針,是指向"垃圾"內(nèi)存(不可用內(nèi)存)的指針。野指針是非常危險(xiǎn)的。

八、NSRunLoop

作用:

  • 有事情的時(shí)候當(dāng)前NSRunLoop的線程工作,沒有事情做讓當(dāng)前NSRunLoop的線程休眠。
  • 一直在循環(huán)檢測(cè),從線程開始到結(jié)束,檢測(cè)同步事件。
  • 系統(tǒng)已經(jīng)在每條線程中加入RunLoop,主線程默認(rèn)啟動(dòng),子線程默認(rèn)關(guān)閉,保證主線程在運(yùn)行起來后就處于一種等待狀態(tài),如果有接收到事件就會(huì)執(zhí)行任務(wù),否則就會(huì)處于休眠狀態(tài)。
  • 每條線程都有唯一的一個(gè)RunLoop對(duì)象與之對(duì)應(yīng)
  • 保持程序的持續(xù)運(yùn)行
  • 處理APP中的各種事件(比如觸摸,定時(shí)器,Selector)
  • 節(jié)省CPU資源,提高程序性能,該做事時(shí)做事,該休息時(shí)休息
#pragma 系統(tǒng)默認(rèn)注冊(cè)運(yùn)行模式RunLoop Mode(5個(gè))
NSDefaultRunLoopMode:             //默認(rèn)的Mode,通常主線程的RunLoop是在這個(gè)Mode下運(yùn)行。
UITrackingRunLoopMode:             //界面跟蹤Mode,當(dāng)用戶與界面交互的時(shí)候會(huì)在此Mode下運(yùn)行。
NSRunLoopCommonModes:             //這個(gè)不是一種真正的Mode,是一個(gè)占位用的Mode。
UIInitializationRunLoopMode:     //程序啟動(dòng)時(shí)的Mode,啟動(dòng)完成后就不在此Mode下。
GSEventReceiveRunLoopMode:         //接受系統(tǒng)事件的內(nèi)部Mode,一般我們用不到。
#pragma RunLoop函數(shù)
[NSRunLoop mainRunLoop];        //獲取主線程RunLoop對(duì)象
[NSRunLoop currentRunLoop];        //獲取當(dāng)前線程RunLoop對(duì)象
#pragma RunLoop應(yīng)用
1)NSTimer
2)ImageView顯示:控制方法在特定的模式下可用
3)PerformSelector
4)常駐線程:在子線程中開啟一個(gè)runloop
5)自動(dòng)釋放池
    第一次創(chuàng)建:進(jìn)入runloop的時(shí)候
    最后一次釋放:runloop退出的時(shí)候
    其它創(chuàng)建和釋放:當(dāng)runloop即將休眠的時(shí)候會(huì)把之前的自動(dòng)釋放池釋放,然后重新創(chuàng)建一個(gè)新的釋放池

(1)NSTimer應(yīng)用

NSTimer受RunLoop Mode影響

//一、NSTimer默認(rèn)添加到主線程中默認(rèn)模式下工作----------------(NSDefaultRunLoopMode)
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(run) userInfo:nil repeats:YES];

//二、NSTimer只有在默認(rèn)模式下工作-----------------------(NSDefaultRunLoopMode)
    NSTimer *timer1 = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];

//三、NSTimer只有在拖拽UI界面模式下工作------------------(UITrackingRunLoopMode)
    NSTimer *timer2 = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer2 forMode:UITrackingRunLoopMode];

//四、NSTimer只有在NSRunLoopCommonModes模式下工作-------(標(biāo)記NSDefaultRunLoopMode/UITrackingRunLoopMode兩種模式)
    NSTimer *timer3 = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer3 forMode:UITrackingRunLoopMode];

RunLoop與GCD

CCD不受RunLoop Mode影響

(2)ImageView顯示應(yīng)用

//只在NSDefaultRunLoopMode下執(zhí)行(刷新圖片,拖拽UI不會(huì)刷新圖片)
[_myImageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"1.jpg"] afterDelay:2.0 inModes:@[NSDefaultRunLoopMode]];

(3)常駐線程應(yīng)用

#import "ViewController.h"
/*
思路:為了保證線程不死,我們考慮在子線程中加入RunLoop,
    但是由于RunLoop中沒有沒有源,就會(huì)自動(dòng)退出RunLoop,
    所以我們要為子線程添加一個(gè)RunLoop,
    并且為這個(gè)RunLoop添加源(保證RunLoop不退出)
*/
@interface ViewController ()
/** 線程對(duì)象 */
@property (nonatomic, strong)NSThread *thread;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 創(chuàng)建子線程
    self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    //啟動(dòng)子線程
    [self.thread start];
}

- (void)run {
    NSLog(@"run--%@", [NSThread currentThread]); // 子線程
    // 給子線程添加一個(gè)RunLoop,并且加入源
    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    // 啟動(dòng)RunLoop
    [[NSRunLoop currentRunLoop] run];
    NSLog(@"------------"); // RunLoop啟動(dòng),這句沒有執(zhí)行的機(jī)會(huì)
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 在子線程中調(diào)用test方法,如果子線程還在就能夠調(diào)用成功
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
}

- (void)test {
    NSLog(@"test--%@", [NSThread currentThread]); // 子線程
}
@end

RunLoop流程

#pragma    RunLoop監(jiān)聽活動(dòng)枚舉值
/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
    kCFRunLoopEntry = (1UL << 0),            //狀態(tài)值:1,表示進(jìn)入RunLoop
    kCFRunLoopBeforeTimers = (1UL << 1),    //狀態(tài)值:2,表示即將處理NSTimer
    kCFRunLoopBeforeSources = (1UL << 2),    //狀態(tài)值:4,表示即將處理Sources
    kCFRunLoopBeforeWaiting = (1UL << 5),    //狀態(tài)值:32,表示即將休眠
    kCFRunLoopAfterWaiting = (1UL << 6),    //狀態(tài)值:64,表示從休眠中喚醒
    kCFRunLoopExit = (1UL << 7),            //狀態(tài)值:128,表示退出RunLoop
    kCFRunLoopAllActivities = 0x0FFFFFFFU    //表示監(jiān)聽上面所有的狀態(tài)
};

為主線程RunLoop添加觀察者

#pragma 1.創(chuàng)建一個(gè)監(jiān)聽對(duì)象
    /**
     *  參數(shù)1:    告訴系統(tǒng)如何給Observer對(duì)象分配存儲(chǔ)空間
     *  參數(shù)2:    需要監(jiān)聽的類型
     *  參數(shù)3:    是否需要重復(fù)監(jiān)聽
     *  參數(shù)4:    優(yōu)先級(jí)
     *  參數(shù)5:    監(jiān)聽到對(duì)應(yīng)的狀態(tài)之后的回調(diào)
     */
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {

        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"進(jìn)入RunLoop");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"即將處理Timer");
                break;
            case kCFRunLoopBeforeSources:
                NSLog(@"即將處理Source");
                break;

            case kCFRunLoopBeforeWaiting:
                NSLog(@"即將休眠");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"從休眠中喚醒");
                break;
            case kCFRunLoopExit:
                NSLog(@"退出RunLoop");
                break;
            case kCFRunLoopAllActivities:
                NSLog(@"監(jiān)聽上面所有狀態(tài)");
                break;
            default:
                break;
        }

    });
#pragma 2.給主線程的RunLoop添加監(jiān)聽
    /**
     *  參數(shù)1:    需要監(jiān)聽的RunLoop對(duì)象
     *  參數(shù)2:    給指定的RunLoop對(duì)象添加的監(jiān)聽對(duì)象
     *  參數(shù)3:    在哪種模式下監(jiān)聽
     */
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopCommonModes);
#pragma 3.釋放Observer
    CFRelease(observer);
    //輸出:       從休眠中喚醒
    //輸出:       即將處理Timer
    //輸出:       即將處理Source
    //輸出:       即將處理Timer
    //輸出:       即將處理Source
    //輸出:       即將休眠
    //輸出:       從休眠中喚醒
    //輸出:       即將處理Timer
    //輸出:       即將處理Source
    //輸出:       即將休眠
    //.....

九、沙盒機(jī)制

iOS8.3以后蘋果禁止了對(duì)沙盒路徑的訪問

每個(gè)應(yīng)用都只能訪問當(dāng)前沙盒目錄下面的文件

  • app 應(yīng)用文件
    • Documents 保存應(yīng)用程序的數(shù)據(jù)文件。
  • Library 保存默認(rèn)設(shè)置或其它狀態(tài)信息。
    • Caches 保存緩存文件。
    • Preferences 保存偏好設(shè)置文件,Plist文件。
  • tmp 保存各種臨時(shí)文件。
NSHomeDirectory()                                      //獲取沙盒目錄
[[NSBundle mainBundle] bundlePath];                    //獲取沙盒目錄.app文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentsPath = [paths objectAtIndex:0];    //獲取沙河目錄Documents文件夾
NSTemporaryDirectory()                                //獲取沙河目錄tmp文件夾

十、iOS數(shù)據(jù)存儲(chǔ)

(1)Plist文件

(2)SQLite數(shù)據(jù)庫

(3)CoreData數(shù)據(jù)庫

SQLite數(shù)據(jù)庫增刪改查

支持類型:integer real, text, blob

使用單引號(hào)來環(huán)繞文本值。如果是數(shù)值,請(qǐng)不要使用引號(hào)。

#pragma    創(chuàng)建表
create table 表名 (字段1, 字段2, 字段3);
create table if not exists (字段1, 字段2, 字段3);
#pragma    插入
insert into 表名 (字段1, 字段2, 字段3) values (值1, 值2, 值3);
#pragma    更新
update 表名 set 字段1 = 值1, 字段2 = 值2, 字段3 = 值3;
update 表名 set 字段1 = 值2 where 字段1 = 值1;
#pragma    刪除
delete from 表名
#pragma    查詢
select * from 表名;                                       //所有查詢
select * from 表名 where 字段 = 值;                        //具體查詢
select * from 表名 where 字段 like 值;                     //模糊查詢
select * from 表名 order by 字段1 ASC, 字段2 DESC;         //排序查詢(ASC升序, DESC降序)
#pragma    統(tǒng)計(jì)
select count(*) from 表名;
select count(*) from 表名 where 字段 like 值;
select max(字段) from 表名;
select min(字段) from 表名;
select avg(字段) from 表名;
select sum(字段) from 表名;

 

來自:http://www.jianshu.com/p/3dcb1f7f14e5

 

標(biāo)簽: 代碼 數(shù)據(jù)庫 搜索

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android應(yīng)用瘦身,從18MB到12.5MB

下一篇:iOS中書寫代碼規(guī)范35條小建議: