gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:16:39 27 4
gpt4 key购买 nike

我试图在第一次加载屏幕时,然后在每次单击“添加”按钮时,在主屏幕上的标签中显示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];
}

最佳答案

代替

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”方法调用的原因。

关于iphone - 在标签字符串中显示NSMutableArray大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873436/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com