NSArray和NSDictionary只能存儲對象,而不能直接存儲任何基本類型的數據,如int,float或struct.但是你可以用對象來封裝基本數值。例如,將int型數據封裝到一個對象中,然后就可以將這個對象放入NSArray或NSDictionary中了。
代碼如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSNumber *numberInt=[NSNumber numberWithInt:1]; NSNumber *numberFloat=[NSNumber numberWithFloat:1.0]; NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:numberInt, numberFloat,nil]; NSLog(@"--array--%@",array); }
輸出:
2015-08-04 20:22:59.825 NSNumber[531:12193] --array--( 1, 1)
參考資料:《Objective-C基礎教程》
新聞熱點
疑難解答