Get maximum value from NSMutableArray [duplicate] - ios

This question already has answers here:
Getting Max Date from NSArray, KVC
(2 answers)
Closed 8 years ago.
How can I get a maximum value from an NSMutableArray. Values are stores as NSString in array as below
for(int counter=0; counter<[datesArray count];counter++)
{
glass = [NSString stringWithFormat:#"%f",[[dbhandler todayData:currentDate] floatValue]];
[graphGlassesArray addObject:glass];
}
Array name is graphGlassesArray.

You can get the max value from an array using:
NSNumber* max = [graphGlassesArray valueForKeyPath:#"max.self"];
float maxFloat = [max floatValue]

You should also try to use Key Value Coding (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html). To obrain the max in a NSArray you can do [array valueForKeyPath:#"#max.attribute"]. Try to look at this link http://nshipster.com/kvc-collection-operators/.

float max = 0.f;
for(int counter=0; counter<[datesArray count];counter++)
{
glass = [NSString stringWithFormat:#"%f",[[dbhandler todayData:currentDate] floatValue]];
[graphGlassesArray addObject:glass];
if(max < [[dbhandler todayData:currentDate] floatValue]) {
max = [[dbhandler todayData:currentDate] floatValue];
}
}
Try with this

Try by sorting the mutable array
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO selector:#selector(localizedCompare:)];
NSString *maxValue = [[graphGlassesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]] objectAtIndex:0];
[desc release];

Related

is there any way to add integers from an array without take elements to outside [duplicate]

This question already has answers here:
ios - How get the total sum of float from a NSMutableArray
(3 answers)
Closed 8 years ago.
I want to sum of currency from NSMutableArray. Ex: I have an arrayA (1,234.56 , 2,345.67) and after sum items in array, I want result show: 3,580.23 to put it on the Label. Is there the way to implement this?
Thanks
If the values are stored as NSNumber objects, you can use the collection operators. For example:
NSArray *array = #[#1234.56, #2345.67];
NSNumber *sum = [array valueForKeyPath:#"#sum.self"];
If you want to format that sum nicely using NSNumberFormatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *result = [formatter stringFromNumber:sum];
NSLog(#"result = %#", result);
If your values are really represented by strings, #"1,234.56", #"2,345.67", etc., then you might want to manually iterate through the array, converting them to numeric values using the NSNumberFormatter, adding them up as you go along:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSArray *array = #[#"1,234.56", #"2,345.67"];
double sum = 0.0;
for (NSString *string in array) {
sum += [[formatter numberFromString:string] doubleValue];
}
NSString *result = [formatter stringFromNumber:#(sum)];
NSLog(#"result = %#", result);
The simplest way is this:
NSMutableArray *array = [NSMutableArray arrayWithArray:#[#(1234.56), #(2345.67)]];
double sum = [[array valueForKeyPath: #"#sum.self"] doubleValue];

How to replace the NSDictionary value from another value in Objective C? [duplicate]

This question already has answers here:
Replace a value in NSDictionary in iPhone
(5 answers)
Closed 7 years ago.
I have a NSMutableArray below like this
(
{
Realimg = "<UIImage: 0x13951b130>, {800, 600}";
"bid_accepted" = 1;
"bid_amount" = 100;
"bid_amount_num" = 100;
"bid_cur_name" = USD;
"bid_cur_symb" = $;
"bid_currencycode" = 4;
"bid_date" = "05 Nov 2015";
"bid_msg" = testing;
"bid_user_id" = 2;
"nego_id" = 612;
"pas_count" = 5;
"ride_address" = "Sample Address";
"ride_cover_img" = "uploadfiles/UploadUserImages/2/mobile/21426739600s-end-horton-plains.jpg";
"ride_id" = 149;
"ride_name" = "Ride to the World's End";
"ride_type_id" = 0;
"ride_type_name" = Travel;
"ride_user_fname" = Nik;
"ride_user_id" = 2;
"ride_user_lname" = Mike;
}
)
What I want to do is, replace the "bid_currencycode" = 4; by a different value.I want to replace 4 by 5. How can I do this? Please someone help me.
Thank you
You can use the following code for doing the same:
// Getting the dictionary from your array and making it to a mutable copy
NSMutableDictionary *dict = [yourArray[index] mutableCopy];
// Changing the specified key
[dict setObject:#(5) forKey:#"bid_currencycode"];
// Replacing the current dictionary with modified one
[yourArray replaceObjectAtIndex:index withObject:dict];
Here:
yourArray is a NSMutableArray
index is a valid index (index of object, that you need to change the value)
Refer the below coding
NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
//your array
dic =[yourArray objectAtIndex:0];
[dict removeObjectForKey:#"bid_currencycode"];
[dict setObject:#“5” forKey:#"bid_currencycode"];
[yourArray replaceObjectAtIndex:0 withObject:dict];
0 is (yourArray index number) You can change according your array index number

How get the total sum of currency from a NSMutableArray [duplicate]

This question already has answers here:
ios - How get the total sum of float from a NSMutableArray
(3 answers)
Closed 8 years ago.
I want to sum of currency from NSMutableArray. Ex: I have an arrayA (1,234.56 , 2,345.67) and after sum items in array, I want result show: 3,580.23 to put it on the Label. Is there the way to implement this?
Thanks
If the values are stored as NSNumber objects, you can use the collection operators. For example:
NSArray *array = #[#1234.56, #2345.67];
NSNumber *sum = [array valueForKeyPath:#"#sum.self"];
If you want to format that sum nicely using NSNumberFormatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *result = [formatter stringFromNumber:sum];
NSLog(#"result = %#", result);
If your values are really represented by strings, #"1,234.56", #"2,345.67", etc., then you might want to manually iterate through the array, converting them to numeric values using the NSNumberFormatter, adding them up as you go along:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSArray *array = #[#"1,234.56", #"2,345.67"];
double sum = 0.0;
for (NSString *string in array) {
sum += [[formatter numberFromString:string] doubleValue];
}
NSString *result = [formatter stringFromNumber:#(sum)];
NSLog(#"result = %#", result);
The simplest way is this:
NSMutableArray *array = [NSMutableArray arrayWithArray:#[#(1234.56), #(2345.67)]];
double sum = [[array valueForKeyPath: #"#sum.self"] doubleValue];

Change numbers to text iOS [duplicate]

This question already has answers here:
How do I convert an integer to the corresponding words in objective-c?
(7 answers)
Closed 8 years ago.
I am not sure how to explain this but I need something like this and I'm not quite sure how to do it. I have a textfield where the user enters a number. For example "200". I got a label that must show "Two Hundred"(The number entered in Words )
Any ideas on how to do this?
Thanks in advance sorry for my bad english
try this:
//textField.text is 200
NSInteger someInt = [textField.text integerValue];
NSString *numberWord;
NSNumber *numberValue = [NSNumber numberWithInt:someInt];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
numberWord = [numberFormatter stringFromNumber:numberValue];
NSLog(#"numberWord= %#", numberWord); // Answer: two hundred
yourLabel.text = numberWord;
You can use a NSNumberFormatter It can convert an NSNumber into its word representation.
NSNumber* number = #100;
NSString* textNumber;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
textNumber = [numberFormatter stringFromNumber:number];
Here's something totally different. A bit more complex and weird, but you can fiddle around with it if you seek more manual control over the output. This is set up to serve a value up to 100,000 (to demonstrate a conditional set up).
Have fun:
int theInteger = 1,123;
int convert1000 = 0;
int convert100 = 0;
int convert10 = 0;
int convert1 = 0;
NSString * wordThousand = #"Thousand";
NSString * wordHundred = #"Hundred";
NSArray *wordsArraySingleDigit = [[NSArray alloc] initWithObjects:#"Zero",#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",nil];
NSArray *wordsArrayDoubleDigit = [[NSArray alloc] initWithObjects:#"Ten",#"Eleven",#"Twelve",...,#"Ninety Eight", #"Ninety Nine",nil];
convert1000 = (theInteger - (theInteger % 1000))/1000;
convert100 = ((theInteger - (convert1000 * 1000)) - ((theInteger - (convert1000 * 1000)) % 100))/100;
convert10 = ((theInteger - (convert1000 *1000)-(convert100 *100)) - ((theInteger -(convert1000 *1000) - (convert100 *100)) % 10))/10;
convert1 = (theInteger - (convert1000 *1000)-(convert100 *100) - (convert10 *10));
if (theInteger > = 10000 && < 100000){
NSString * convertedThousand = [wordsArrayDoubleDigit objectAtIndex : convert1000];
convertedThousand = [NSString stringWithFormat: #“%# %#“,convertedThousand,wordThousand];
NSLog (convertedThousand);
}
if (theInteger >= 1000 && <= 10000){
NSString * convertedThousand = [wordsArraySingleDigit objectAtIndex : convert1000];
convertedThousand = [NSString stringWithFormat: #“%# %#“,convertedThousand,wordThousand];
NSLog (convertedThousand);
}
NSString * convertedHundred = [wordsArraySingleDigit objectAtIndex : convert100];
convertedHundred = [NSString stringWithFormat: #“%# %#“, convertedHundred,wordHundred];
NSLog (convertedHundred);
NSString * convertedTen = [wordsArraySingleDigit objectAtIndex : convert10];
convertedTen = [NSString stringWithFormat: #“%#“, convertedTen];
NSLog (convertedTen);
NSString *convertedOne = [wordsArraySingleDigit objectAtIndex : convert1];
NSLog (convertedOne);
Hope this helps or gives you a starting point for an alternative approach.
I think you should account for every possibility and make a set of IF statements, for example, if the first digit of the number is 3, write "three", if the number has 4 digits, display "thousands"... of course you can organize your code to make the process easy.

Can't store all data from NSArray into NSMutableDictionary? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What's the problem with this code?? I am trying to put the data from an NSArray to a NSMutableDictionary but I do not want to first split the initial nsarray into two and then send the data to the nsdcitionary.
The problem is that when I NSLog de mutabledictionary it returns me just the 1 item which happens to be the last data from the NSArray.
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableDictionary *dict = [NSMutableDictionary new];
for (int i = 0; i < [arrayFinal count ]; i = i + 2) {
[dict setObject:[arrayFinal objectAtIndex:i] forKey:#"hora"];
[dict setObject:[arrayFinal objectAtIndex:i+1] forKey:#"preco"];
}
The result is:
2013-09-04 20:27:33.732 separa[1438:c07] {
hora = "13:30";
preco = "2.15";
}
Any help will be appreciated.
for (int i = 0; i < [arrayFinal count ]; i = i + 2) {
[dict setObject:[arrayFinal objectAtIndex:i+1] forKey:[arrayFinal objectAtIndex:i]];
}
You need each key to point to an array of values. Something like this:
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableArray *horas = [NSMutableArray new];
NSMutableArray *precos = [NSMutableArray new];
for (int i = 0; i < [arrayFinal count]; i += 2) {
[horas addObject:arrayFinal[i]];
[precos addObject:arrayFinal[i + 1]];
}
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[#"hora"] = horas;
dict[#"preco"] = precos;
You're going to need to separate the two (hours and prices) into their own NSMutableArrays, then store each array as one of the keys, something like this:
NSMutableArray *hora = [NSMutableArray alloc] init];
NSMutableArray *preco = [NSMutableArray alloc] init];
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableDictionary *dict = [NSMutableDictionary alloc] init];
for (int i = 0; i < [arrayFinal count ]; i = i + 2)
{
[hora addObject:[arrayFinal objectAtIndex:i];
[preco addObject:[arrayFinal objectAtIndex:i+1];
}
[dict setObject:hora forKey:#"hora"];
[dict setObject:preco forKey:#"preco"];
Probably not exactly the way I'd do it, but I think it is the concept you're looking for.

Resources