I am working on a project where I download a few values (stored as float in my database) into my array and sum them. I can NSLog the numbers but Unfortunately I am having the hardest time to sum those values in my array.
Adding some code
DETAILS.H
#import <Foundation/Foundation.h>
@interface Details: NSObject
@property (nonatomic, strong)NSNumber *min;
//@property (nonatomic, strong) NSString *min;
@end
just the part of the code where I put the values in the array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
tempDetails *downloadTemps = [[tempDetails alloc] init];
downloadTemps.min = jsonElement[@"min"];
// Add this question to the locations array
[_locations addObject:downloadTemps];
}
View controller code
for (Details *sav in _importArray )
{
NSLog(@"High :- %@ ", sav.min); //THIS LIST ALL MY VALUES CORRECTLY
}
NSMutableArray *newArray = [_importArray mutableCopy];
int totalSum = 0;
for(int i=0; i<[newArray count];i++)
{
totalSum = totalSum + [[newArray objectAtIndex:i] intValue];
}
NSLog(@"Total:%d",totalSum);
In the view controller I get the error [Details intValue]: unrecognized selector sent to instance , I am assuming I am getting this error because min is not declared right, But I am unsure how to do it other wise. Any help offered would be appreciated.
Thank you
You are asking for the selector intValue
, but you should be asking for the selector min
.
totalSum = totalSum + [[[newArray objectAtIndex:i] min] intValue];
You were pretty close in the first block you posted:
for (Details *sav in _importArray )
{
NSLog(@"High :- %@ ", sav.min); //THIS LIST ALL MY VALUES CORRECTLY
}
Then:
int totalSum = 0;
for (Details *sav in _importArray )
{
totalSum += [sav.min intValue];
}
Incidentally, why are you asking for the intValue
of something that you initially wrote was a float?