Question

Here is my Problem, I was able to get the ArrayList (no of Hours) from JSON, The problem is I want to add all the Hours and store the same into a variable so that I can access for next page. Simply saying I want to get the total no of hours from the array. When keeping NSLog for Hours I am getting

Hours Total : (
    "13.5",
    "12.5"
)

I want to add 13.5 + 12.5 and store in a Variable

Was it helpful?

Solution

float sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];

Above will give you float value.

*Assumption: Time 13.5 means 13 hours and 30 minutes, and not 13 hours 50 minutes.

OTHER TIPS

Well, if you want to add 13.5 and 12.5 and store the result in a variable, what is the problem?

You will have items of type NSNumber* containing numbers, you can extract the values by calling doubleValue, you make a variable of type double, and add the numbers.

It is very simple. i think you are new in iOS Programming. But don't worry we are here to solve your questions.

You have array of Numbers as string in your Array List. Let assume that "HoursTotal" is your array of numbers. Now please follow the below steps:

  1. Get the First Object From Array using the this method:

    NSString *firstObject = [HoursTotal objectAtIndex:0];
    int firstValue = [firstObject intValue];
    
  2. Same process for getting second value from Object

    NSString *secondObject = [HoursTotal objectAtIndex:1];
    int secondValue = [secondObject intValue];
    

Now you can add the both int value using math operator in objective C.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top