iOSキーワード

31779 ワード

一、constとマクロの違い(面接問題):
  • const :以前よく使われていた文字列定数は、一般的にはマクロ抽出ですが、アップルはマクロ抽出をお勧めしません.const定数の使用をお勧めします.
  • マクロはプリコンパイルで、コンパイル前に処理し、検査をしないで、コンパイルエラーを報告しないで、ただ置き換えて、大量のマクロを使用して、コンパイル時間が長くなりやすい.マクロはいくつかの関数を定義することができ、方法
  • constはコンパイルフェーズであり、コンパイルチェックが行われ、コンパイルエラーが報告されます.

  • 注意:多くのBlogはマクロを使うと言って、多くのメモリを消耗して、私のこの検証は多くのメモリを生成することはできません.マクロは定数を定義して、定数はすべて定数区に置いて、1つのマクロは1部のメモリしか生成しません. 
    //      :   
    #define XMGAccount @"account"
    
    #define XMGUserDefault [NSUserDefaults standardUserDefaults]
    
    //      
    static NSString * const account = @"account";
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //       
        //    
        [XMGUserDefault setValue:@"123" forKey:XMGAccount];
    
        //   const  
        [[NSUserDefaults standardUserDefaults] setValue:@"123" forKey:account];
    
    }
    

    二、const作用:制限タイプ
  • 1.constは、右側の変数(基本データ変数p、ポインタ変数*p)
  • を修飾するためにのみ使用される.
  • 2.constによって修飾された変数は読み取り専用である.
  • const
  • - (void)viewDidLoad {
        [super viewDidLoad];
    
        //     
        int a = 1;
    
        //      
        a = 20;
    
        // const    
        // const:      p
        //          ,const          b
        const int b = 20; // b:    
        int const b = 20; // b:    
    
        //       
        b = 1;
    
        // const:      *p, *   ,      .
        //       int       ,  a   
        int *p = &a;
    
        int c = 10;
    
        p = &c;
    
        //     p     ,
        //     p        
        *p = 20;
    
        // const             ,      *p1,
        //       
        const int *p1; // *p1:   p1:  
        int const *p1; // *p1:   p1:  
    
        // const      p1
        int * const p1; // *p1:   p1:  
    
    
        //    const  *p1    const   p1
        //       
        const int * const p1; // *p1:   p1:  
    
        int const * const p1;  // *p1:   p1:  
    
    }
    

    三、const開発で使用するシーン:
  • 1.メソッドパラメータが読み取り専用の場合
  • .定義読取り専用グローバル変数修飾グローバル変数=>グローバル読取り専用変数=>代替マクロ
  • @implementation ViewController
    
    //         
    NSString * const str  = @"123";
    
    //         ,  .
    - (void)test:(NSString * const)name
    {
    
    }
    
    //     ,         
    - (void)test1:(int const *)a{
    
    //    *a = 10;
    }
    
    //         
    - (void)test2:(int const)a{
    
    }
    
    
    @end
    

    四、staticとexternは簡単に使う(一つのものを使うには、まずその役割を理解する)
  • static :
  • ローカル変数の修飾:1.ローカル変数のライフサイクルを延長し、プログラムが終了してから破棄されます.2.ローカル変数は1つのメモリしか生成されず、一度だけ初期化されます. * staticに修飾されたローカル変数の割り当てメモリは何ですか?プログラムが実行されるとstatic修飾変数にメモリ
  • が割り当てられる.
  • グローバル変数を修飾する1.このファイルにのみアクセスでき、グローバル変数の役割ドメインを変更し、ライフサイクルは
  • を変更しません.
  • extern :
  • は、グローバル変数(グローバル静的変数を含む)の値を取得するためにのみ使用され、変数
  • を定義するために使用できません.
  • extern :
  • 現在のファイルでグローバル変数があるかどうか、見つからないかを検索してから、他のファイルを検索します.

  • //     :      ,      , extern    。
    int a = 20;
    
    // static      
    static int age = 20;
    
    - (void)test
    {
        // static      
        static int age = 0;
        age++;
        NSLog(@"%d",age);
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
    
        [self test];
        [self test];
    
        extern int age;
        NSLog(@"%d",age);
    }
    I
    

    五、staticとconstを併用する
  • staticとconstの役割:読み取り専用の静的変数
  • を宣言する
  • 開発使用シーン: でよく使用される文字列定数はstaticとconstを組み合わせた
  • を使用できます.
    //      static      ,      
    
    //              ,          。
    
    //           ,         ,     。
    
    //                  
    
    //              
    static const int a = 20;
    
    // staic const     :             
    
    // iOS staic const      ,      ,             ,           .
    
    //        key   ,   const  key,  key  ,     。
    static  NSString * const key = @"name";
    
    //    const   *key1,  *key1  ,key1     。
    
    static  NSString const *key1 = @"name";
    

    六、externとconstを併用する
  • 開発で使用されるシーン: でよく使用される同じ文字列定数で、externとconstを組み合わせて使用できます.
  • 理由:
  • staticとconstの組み合わせ:各ファイルに静的グローバル変数を定義する必要があります.
  • externとconstの組み合わせ:グローバル変数を定義するだけで、複数のファイルが共有されます.

  • 大域定数正規書き方:開発中はすべての大域変数を管理しやすく、通常GlobeConstファイルを作り、中には大域変数を専門に定義し、統一管理しなければならない.そうしないと、プロジェクトファイルは探しにくい.
  • GlobeConst.h
  • /*******************************  ****************************/
    
    extern NSString * const nameKey = @"name";
    
    /*******************************  ****************************/
    
  • GlobeConst.m
  • #import 
    
    /*******************************  ****************************/
    
    NSString * const nameKey = @"name";
    
    
    /*******************************  ****************************/
    
    
    
    
    
    

    nullable,nonnull,null_resettable,_Null_unspecified

    /*

     

      NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END nonnull

     

      (int,float),nil

     */

    /*

        ? 1. xcode , xcode

        Xcode7 2015 iOS9

        Xcode6 2014 iOS8

        Xcode5 2013 iOS7

        Xcode4 2012 iOS6

        1. iOS9: : ,

        : ,

        : swift,swift ,swift

        : ,

        ,

        null_resettable

     */

    /*

        nullable:1. ( ) 2. ( )

        nullable :

     

        nullable 1

        @property (nonatomic, strong, nullable) NSString *name; 

        nullable 2 *

        @property (nonatomic, strong) NSString * _Nullable name;

        nullable 3 xcode  

        @property (nonatomic, strong) NSString * __nullable name;

     

     */

    /*

     nonnull:1. ( ) 2. ( )

     nonnull :

     nonnull 1

     @property (nonatomic, strong, nullable) NSString *name;

     nonnull 2 *

     @property (nonatomic, strong) NSString * _Nonnull name;

     nonnull 3

     @property (nonatomic, strong) NSString * __nonnull name;

     

     */

    /*

     

     null_resettable:1. ( ) 2. ( ) 

     null_resettable :set ,get nil, , get

     null_resettable 1

     @property (nonatomic, strong, null_resettable) NSString *name;

     

     */

    /*

        _Null_unspecified:

     */

    、id instancetype __kindof

    // xcode5 instancetype

    // Xcode5 id (id: , )

    // instancetype: ( , )

    //__kindof Person *    __kindof

    ObjectType

     
      

    /*

        :  

        ? swift

     

        :1. 2. , ,

        : < >

     // iOS

     Person *p = [[Person alloc] init];

     p.language = ios;

     

        : , < >

     //

     @interface Person<__contravariant objecttype=""> : NSObject

     

     //

     @property (nonatomic, strong) ObjectType language;

       

        :1. ,

                2. ,

        

        :1.

     

        id

     

        ? , ? =>

     

        ?

        ? , , ,

     

        Person, (iOS,Java), Person, , Person Person

        . id

     

     

       

        :__covariant: ,

     

            __contravariant:

     

        : , , , , ,

     */

     
      

    class , superclass , super

      // class:

        // superclass:

        

        // super: , ,

        // : super , ,