ios 如何在Objective C中初始化一个空的可变数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10224762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize an empty mutable array in Objective C
提问by crashprophet
I have a list of objects (trucks) with various attributes that populate a tableview. When you tap them they go to an individual truck page. There is an add button which will add them to the favorite list in another tableview. How do I initialize an empty mutable array in Cocoa?
我有一个包含填充 tableview 的各种属性的对象(卡车)列表。当您点击它们时,它们会转到单个卡车页面。有一个添加按钮可以将它们添加到另一个 tableview 中的收藏夹列表中。如何在 Cocoa 中初始化一个空的可变数组?
I have the following code:
我有以下代码:
-(IBAction)addTruckToFavorites:(id)sender:(FavoritesViewController *)controller
{
[controller.listOfTrucks addObject: ((Truck_Tracker_AppAppDelegate *)[UIApplication sharedApplication].delegate).selectedTruck];
}
回答by Jakub
回答by Sangram Shivankar
Basically, there are three options:
基本上,有以下三种选择:
First
第一的
NSMutableArray *myMutableArray = [[NSMutableArray alloc] init];
Second
第二
NSMutableArray *myMutableArray = [NSMutableArray new];
Third
第三
NSMutableArray *myMutableArray = [NSMutableArray array];
回答by Janmenjaya
You can also initialize in this way
也可以这样初始化
Another way for Objective Capart from the answer of @NSSam
除了@NSSam 的回答之外,Objective C 的另一种方式
NSMutableArray *myMutableArray = [@[] mutableCopy];
For Swift
对于斯威夫特
let myArray = NSMutableArray()
OR
或者
let myArray = [].mutableCopy() as! NSMutableArray;
回答by Agisight
I use this way to initialize an empty mutable array in Objective C:
我使用这种方式在 Objective C 中初始化一个空的可变数组:
NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
回答by fbernardo
NSMutableArray *arr = [NSMutableArray array];
回答by jtbandes
listOfTrucks = [NSMutableArray array];
gives you a new mutable array.
listOfTrucks = [NSMutableArray array];
给你一个新的可变数组。
回答by Samet DEDE
NSMutableArray *arr = [NSMutableArray new];
回答by GeRyCh
In modern Objective - C it could be even more shorter:
在现代 Objective - C 中,它可能更短:
NSArray *array = @[];