【问题标题】:How to create an NSMutableArray and assign a specific object to it?如何创建 NSMutableArray 并为其分配特定对象?
【发布时间】:2013-09-03 20:08:12
【问题描述】:

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

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

这是我目前所拥有的数组:

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

【问题讨论】:

    标签: ios objective-c nsmutablearray mkannotation


    【解决方案1】:

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

    创建一些实例

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

    然后我们可以添加它们

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

    或者

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

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

    【讨论】:

    • 我不明白 mutableCopy 方法。我见过它,它是表亲——可变对象的不可变副本。复制的目的是什么?为什么经常出现在可变数组和字典中?
    • @noobsmcgoobs 如果你有一个不可变对象并且你想修改它,你需要创建一个新对象,可以使用方便的方法或通过制作可变副本并编辑它(通常如果您想要进行多项修改)。您可能会使用不可变的副本来记录您所获得的东西,并保护自己免受有人在背后更改该对象...
    【解决方案2】:

    嗯:

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

    【讨论】:

      【解决方案3】:
      NSMutableArray *array = [NSMutableArray alloc] init];
      [array addObject:myObject];
      

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

      【讨论】:

        【解决方案4】:

        试试这样的

          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];
        

        【讨论】:

          【解决方案5】:

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

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

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

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

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

          希望这能回答您的问题

          【讨论】:

            【解决方案6】:
            TruckLocation *loc1=...;
            TruckLocation *loc2=...;
            NSMutableArray *truckArray=[[NSMutableArray alloc]initWithObjects:loc1,loc2];
            

            您可以在初始化时添加对象。通过这个,您可以在分配代码中添加对象。您也可以避免使用addObject 编写更多的步骤。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-09-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多