I have 10 buttons, when clicks the buttons will increase the some amount of values. I want to put the values into NSMutableArray. But i don't want to add the values which depends on the button index, i want to add the values into the array as the same order (0 to ArrayCount) when clicks any button in randomly.
For Eg:
switch (btn.tag) {
case 0:
itemValue = itemValue + 15; //item value is 30
[amountArray addObject:itemValue];
break;
case 1:
itemValue = itemValue + 30; //item value is 60
[amountArray addObject:itemValue];
break;
case 2:
itemValue = itemValue + 25; //item value is 50
[amountArray addObject:itemValue];
break;
case 3:
itemValue = itemValue + 40; //item value is 80
[amountArray addObject:itemValue];
break;
case 4: //item value is 45
itemValue = itemValue + 15;
[amountArray addObject:itemValue];
break;
In this case, i have clicked the button in the order of the button index 4,3,2,1,0 and expected result array is
{45,80,50,60,30}.
If i have used something like this,
[amountArray replaceObjectAtIndex:btn.tag withObject:itemValue];
and the result is {30,60,50,80,45}.
But i want to the store the values as normally in the array as 0 to arrayCount. When clicks the button every time, it will increase some value, so shouldn't allow the duplicate values , have to change only values.
Thanks!
NSMutableSet will avoid dublicate.
so instead of using array just use NSMutableSet.
NSMutableSet *amountSet=[[NSMutableSet alloc]init];
[amountSet addObject:itemValue];
You are adding an int to array that is wrong you need to use
[amountArray addObject:[NSNumber numberWithInt:itemValue]];
Also you need to check if that value is available in array already by using the method of array
if([array indexOfObject:[NSNumber numberWithInt:itemValue]] == NSNotFound) {
//Add object here to mutable array
}
or not and later you need to sort array using sortDescripter or NSPredicate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.