ios 如何创建 NSMutableArray 并为其分配特定对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18600626/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:58:13  来源:igfitidea点击:

How to create an NSMutableArray and assign a specific object to it?

iosobjective-cnsmutablearraymkannotation

提问by novicePrgrmr

I am just getting into Obj C, and I am looking to create an array of MKAnnotations.

我刚刚进入 Obj C,我希望创建一个 MKAnnotations 数组。

I have already created the MKAnnotation class called TruckLocationthat contains the name, description, latitude, and longitude.

我已经创建了TruckLocation包含名称、描述、纬度和经度的 MKAnnotation 类。

Here is what I have so far for the array:

这是我到目前为止对数组的内容:

NSMutableArray* trucksArray =[NSMutableArray arrayWithObjects: @[<#objects, ...#>]  nil];

回答by Wain

Yore trying to combine 2 different syntaxes for similar but different things. You also don't seem to have any instances of your annotations.

过去试图将 2 种不同的语法组合起来用于相似但不同的事物。您似乎也没有任何注释实例。

Create some instances

创建一些实例

TruckLocation *a1 = ...;
TruckLocation *a2 = ...;

Then we can add them

然后我们可以添加它们

NSMutableArray *trucksArray = [NSMutableArray arrayWithObjects:a1, a2, nil];

Or

或者

NSMutableArray *trucksArray = [@[a1, a2] mutableCopy]

This is a shorter and more modern form but you need to make it mutable as it will create an immutable instance.

这是一种更短、更现代的形式,但您需要使其可变,因为它将创建一个不可变的实例。

回答by elio.d

Well:

好:

NSString *a = @"a";
NSMutableArray *array = [NSMutableArray arrayWithObjects:a,nil];
//or
NSMutableArray *array = [[NSMutableArray alloc]init]; //alloc

[array addObject:a];

回答by Juan Catalan

NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:myObject];

Where myObject is the object of your custom class.

其中 myObject 是您的自定义类的对象。

回答by Vinodh

Try something lke this

尝试这样的事情

  NSMutableArray *annotationArray=[[NSMutableArray alloc]init];

        CLLocationCoordinate2D shopPosition = CLLocationCoordinate2DMake(allShopInfoObject.shopLatitudeValue, allShopInfoObject.shopLongitudeValue);

    MapAnnotation *mapAnnotation = [[MapAnnotation alloc] initWithCoordinates:shopPosition andTitle:allShopInfoObject.shopName andShopId:allShopInfoObject.shopId subTitle:@""];

        [annotationArray addObject:mapAnnotation];
[self.mapView addAnnotations:annotationArray];

回答by soorej babu

TruckLocation *loc1=...;
TruckLocation *loc2=...;
NSMutableArray *truckArray=[[NSMutableArray alloc]initWithObjects:loc1,loc2];

you can add objects at the time of initialization. by this you can add objects within the allocation code. also you can avoid more number of steps for writing with addObject.

您可以在初始化时添加对象。通过这种方式,您可以在分配代码中添加对象。您还可以避免使用addObject.

回答by Macrosoft-Dev

Suppose you initialize the objects and assign values the following way:

假设您按以下方式初始化对象并赋值:

TruckLocation *truckLocationOne = [[TruckLocation alloc]initWithAnnotation:annotation
           reuseIdentifier:annotationIdentifier];
truckLocationOne.name = @"name";

TruckLocation *truckLocationTwo = [[TruckLocation alloc]initWithAnnotation:annotation
           reuseIdentifier:annotationIdentifier];
truckLocationTwo.name = @"name";

These objects would be added to the array temp in the following way:

这些对象将通过以下方式添加到数组 temp:

1) NSMutableArray temp = [[NSMtableArray alloc]initWithObjects:truckLocationOne,truckLocationTwo,nil];

1) NSMutableArray temp = [[NSMtableArray alloc]initWithObjects:truckLocationOne,truckLocationTwo,nil];

2) NSMutableArray temp = [[NSMtableArray alloc]init]; [temp addObject:truckLocationOne]; [temp addObject:truckLocationTwo];

2) NSMutableArray temp = [[NSMtableArray alloc]init]; [temp addObject:truckLocationOne]; [temp addObject:truckLocationTwo];

Hope this answers your query

希望这能回答您的疑问