iOSキーワード
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作用:制限タイプ
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開発で使用するシーン:
@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
: 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 ,
// , 。
// , , 。
//
//
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を組み合わせて使用できます./******************************* ****************************/
extern NSString * const nameKey = @"name";
/******************************* ****************************/
#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 , ,