Ios 为什么viewDidLoad中声明的数组在另一个方法中使用时会收到未使用的变量警告?

Ios 为什么viewDidLoad中声明的数组在另一个方法中使用时会收到未使用的变量警告?,ios,objective-c,nsmutablearray,unused-variables,Ios,Objective C,Nsmutablearray,Unused Variables,对于我在viewDidLoad中声明的trucksArray数组,我收到一个未使用的变量警告。我不明白为什么,因为我正在viewController.m的另一个方法中使用它 我对目标c非常陌生,所以如果这是一个非常直截了当的问题,我提前道歉 方法如下: - (void)viewDidLoad { [super viewDidLoad]; TruckLocation *a1 = [[TruckLocation alloc] initWithName:@"test truck 1"

对于我在viewDidLoad中声明的
trucksArray
数组,我收到一个未使用的变量警告。我不明白为什么,因为我正在
viewController.m
的另一个方法中使用它

我对目标c非常陌生,所以如果这是一个非常直截了当的问题,我提前道歉

方法如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    TruckLocation *a1 = [[TruckLocation alloc] initWithName:@"test truck 1" address:@"41 Truck Avenue, Provo, Utah" coordinate:CLLocationCoordinate2DMake(40.300828, 111.663802)];

    TruckLocation *a2 = [[TruckLocation alloc] initWithName:@"test truck 2" address:@"6 Truck street, Provo, Utah" coordinate:CLLocationCoordinate2DMake(40.300826, 111.663801)];

    NSMutableArray* trucksArray =[NSMutableArray arrayWithObjects: a1, a2, nil];
}
我使用数组的方法是:

- (void)plotTruckPositions:(NSData *)responseData {
    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }

    NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
    NSMutableArray *trucksArray = [root objectForKey:@"trucksArray"];

    for (NSArray *row in trucksArray) {
        NSNumber * latitude = [row objectAtIndex:1];
        NSNumber * longitude = [row objectAtIndex:2];
        NSString * truckDescription = [row objectAtIndex:2];
        NSString * address = [row objectAtIndex:3];

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        TruckLocation *annotation = [[TruckLocation alloc] initWithName:truckDescription address:address coordinate:coordinate] ;
        [_mapView addAnnotation:annotation];
    }
}
-(无效)绘图结构位置:(NSData*)响应数据{
对于(在_mapView.annotations中的id注释){
[_MapViewRemoveAnnotation:annotation];
}
NSDictionary*root=[NSJSONSerialization JSONObject WithData:responseData选项:0错误:无];
NSMutableArray*trucksArray=[根objectForKey:@“trucksArray”];
用于(卡车阵列中的NSArray*行){
NSNumber*纬度=[行对象索引:1];
NSNumber*经度=[行对象索引:2];
NSString*truckDescription=[行对象索引:2];
NSString*地址=[row objectAtIndex:3];
CLLocationCoordinate2D坐标;
坐标.纬度=纬度.双值;
coordinate.longitude=longitude.doubleValue;
TruckLocation*注释=[[TruckLocation alloc]initWithName:truckDescription地址:地址坐标:坐标];
[_MapViewAddAnnotation:annotation];
}
}

您正在使用两个完全不同的数组,这两个数组碰巧都被称为
trucksArray


在viewDidLoad方法中,您正在创建的数组没有存储在任何位置,因此它超出范围,并在方法返回后释放。是否要将其分配给实例变量?

在viewDidLoad中,您正在创建NSMutableArray的实例,该实例将在viewDidLoad方法结束后解除分配。在第二种方法中,创建一个完全不同的NSMutableArray。如果要在某个地方创建并在其他地方使用NSMutableArray,则应创建实例变量或属性以保留对该NSMutableArray的引用

@property (nonatomic) NSMutableArray *trucksArray;

如果您将变量声明为标题内的全局变量(带花括号),则只需分配它们,无需重新声明,只需分配它们(等号),即省略前面的“TruckLocation*”。

哦,非常感谢。我刚刚进入iOS开发,我觉得这个小项目对我来说太深了。我想这就是你学习的方式。谢谢每个人都从某个地方开始,祝你好运=)@nearyprgrm仅供参考-这与iOS开发无关。这是关于变量范围的一个基本问题。这在任何面向对象编程语言(甚至许多非面向对象语言)中都是正确的。我建议你找一本关于Objective-C的好教程。在你深入研究iOS开发之前,先学习该语言的基础知识是很重要的。享受。