staticNSString* twoShow =@"againShow";//靜態(tài)變量作用域這個.m文件可以被修改
staticNSDateFormatter* datashow =nil;//我們希望這個類不只是作用于某個類的某一個作用區(qū)間而是作用于整個類的地方乳蓄,這個時候我們可以采用靜態(tài)變量
staticNSStringconst* showString =@"test1";
staticconstNSString* constString =@"test2";
staticNSString*consttestString =@"test3";
staticconstintp =4;
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superview DidLoad];
twoShow=@"WWWW";
datashow=[[NSDateFormatter alloc]init];
datashow.dateFormat=@"yyyy-MM-dd HH:mm:ss";//設(shè)置轉(zhuǎn)換成字符串的格式
datashow.timeZone= [NSTimeZonelocalTimeZone];//設(shè)置時間為ipone的當前時區(qū)
[self getNowDateFor];
[self gofasetShow];//驗證靜態(tài)變量
[self test2];//驗證靜態(tài)常量
[self test3];
NSLog(@"nameShow:%@",nameShow);
NSLog(@"nnnnnnnn:%@",twoAgainShow);
}
-(void)test3{
//showData = @"wwwww";不可修改的全局常量
}
-(void)test2{
//p = 6;錯誤的寫法
showString=@"wwww";
constString=@"ppppp";
//testString = @"wwwwww";不可改的靜態(tài)常量
}
-(void)getNowDateFor
{
NSDate* date = [NSDatedate];
NSLog(@"%@",[datashowstringFromDate:date]);
}
-(void)gofasetShow
{
NSDate* date = [NSDatedate];
NSLog(@"%@",[datashowstringFromDate:date]);
}
以上是代碼的測試以及部分說明 下面說一下extern的簡單用法 extern分為全局變量和 全局常量
extern nstring * const test = @“托爾斯泰”跨细;不可變的
extern nsstring ?*test = @“aaa”可變的
在一個類的.h中聲明
#import
externNSString* nameShow =@"pppppttt";
externNSString*consttwoAgainShow =@"nihua";
在另一個類中.h中聲明
#import
externNSString* nameShow;
externNSString*consttwoAgainShow;
@interfaceViewController :UIViewController
@end
接下來 就可以在該類的.m中獲取變量的值舞萄。第一個類相當于聲明 第二個類相當于窗口,在這里獲取聲明的值,然后可以在.m 中直接獲得這個值。