在标签字符串中显示NSMutableArray大小

时间:2012-10-13 13:13:39

标签: iphone ios nsmutablearray return

当我第一次加载屏幕时,我试图在主屏幕上的标签中显示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];
  }

1 个答案:

答案 0 :(得分: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”方法调用。