1

我试图在我的主屏幕上的标签中显示NSMutableArray屏幕第一次加载时的大小,然后每次我点击添加按钮时,但我收到诸如“未使用表达式结果”之类的错误。我尝试了几个选项,但仍然没有成功...请告诉我您的意见,谢谢!:)

int arraySize;
NSMutableArray *arrRaceCars;

- (void)viewDidLoad
   {
[super viewDidLoad];
arrRaceCars = [[NSMutableArray alloc]init];
arraySize = [self numberOfObjectsInArray:arrRaceCars]; // call for a method that should return the number of objects in array

self.lblCarsCount.text = @"%d cars in the race", &arraySize;
}

// ...part of the Add button validation; in case that everything is OK, the code below should add an object to the array and change the display of number of cars in the array in the label

else
{
    self.carType = [segmentedSelectCar titleForSegmentAtIndex:segmentedSelectCar.selectedSegmentIndex];
    self.carName = self.txtCarName.text;
    self.carSpeed = [self.txtCarSpeed.text intValue];

    car* newCar = [[car alloc]initCarWithName:carName carType:carType carMaxSpeed:carSpeed];
    NSLog(@"Car type is: %@, Car name is: %@, Car speed is: %d", self.carType, self.carName, self.carSpeed);

    [arrRaceCars addObject:newCar];

    arraySize = [self numberOfObjectsInArray:arrRaceCars];

    self.lblCarsCount.text = @"%d cars in the race", &arraySize; // this is the problematic line      
    [self alertMessage:@"addNewCar" :@"Your car has been added!" :nil :@"OK" :nil];
}

// this is the method that should return the number of objects within the array 
-(int) numberOfObjectsInArray : (NSMutableArray*) arrayToCheck
  {
     return [arrayToCheck count];
  }
4

1 回答 1

3

代替

self.lblCarsCount.text = @"%d cars in the race", &arraySize; // this is the problematic line      

你应该使用:

self.lblCarsCount.text = [NSString stringWithFormat: @"%d cars in the race", [arrRaceCars count]]

第一行代码编译,但编译器不知道你想用某种格式做一个字符串(“表达式结果未使用警告”),这就是为什么你应该把显式的“ stringWithFormat”方法调用放在那里。

于 2012-10-13T13:22:33.170 回答