关于static与#define的实例.

2018-07-26  本文已影响0人  品评

关于static与#define的实例.

#import <Foundation/Foundation.h>

#define XMGAccount @"account"
#define XMGAccount1 @"account1"
static NSString * const account = @"account";
static NSString * const account1 = @"account1";
static const NSString *account4 = @"account";

int main(int argc, char *argv[]) {
    @autoreleasepool {
    
        NSLog(@"XMGAccount %p",XMGAccount);
        NSLog(@"XMGAccount1 %p",XMGAccount1);
        NSLog(@"account %p",account);
        NSLog(@"account1 %p",account1); 
        NSString *account2 = @"account";
        NSString *account3 = @"account";
        NSLog(@"account2 %p",account2);
        NSLog(@"account3 %p",account3);
        NSLog(@"account4 %p",account4);
        
    }
}

输出为

2018-07-25 19:05:58.444 Test[29791:2541706] XMGAccount 0x10641d048
2018-07-25 19:05:58.445 Test[29791:2541706] XMGAccount1 0x10641d068
2018-07-25 19:05:58.445 Test[29791:2541706] account 0x10641d048
2018-07-25 19:05:58.445 Test[29791:2541706] account1 0x10641d068
2018-07-25 19:05:58.445 Test[29791:2541706] account2 0x10641d048
2018-07-25 19:05:58.445 Test[29791:2541706] account3 0x10641d048
2018-07-25 19:05:58.445 Test[29791:2541706] account4 0x10641d048

通过上面的实例发现字符串里面的数据如果是一样的不管实例化的对象是什么都是同一个地址.

上一篇下一篇

猜你喜欢

热点阅读