Design and develop a Single View iPhone application in Objective-C that allows t
ID: 3745199 • Letter: D
Question
Design and develop a Single View iPhone application in Objective-C that allows the user to enter a set of integers separated by commas, sorts them, and shows the sorted integers separated by commas Example Input: 22,10,345,45,58 Output: 10,22,45,58,345 allows the user to enter a set of strings separated by commas, sorts them alphabetically, and shows the sorted strings separated by commas Example: Input: csci,swen,cinf,acct Output: acct,cinf,csci,swen * allows the user to enter a set of dates (mm/dd/yyyy format) separated by commas, sorts them in chronological order, and shows the sorted dates separated by commas Example: Input: 09/13/2015,08/15/2016,24/01/2003 Output: 24/01/2003,09/13/2015,08/15/2016 Hints You can use a text view to allow the user to enter the input text, and you can use another text view in read-only (i.e. non-editable) mode to show the output text You can use a segment control (or other similar UI controls) to allow the user to choose between integer/string/date modes for input textExplanation / Answer
Answer:
The followig code describes the use of NSmutable array,NSstring,NSnumber and how to store the inputs and sort them and display sorted list.
UITextField *input0;
UITextField *input1;
UITextField *input2;
UITextField *input3;
UITextField *input4;
NSMutableArray *textinputs = [@[input0, input1, input2,input3,input4] mutableCopy];
NSNumberFormatter *Nf = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
[textFields sortedUsingComparator: ^(id inp1, id inp2) {
NSString *textinput1 = ((UITextField *)inp1).text;
NSString *textinput1 = ((UITextField *)inp2).text;
NSNumber *inputunumber1 = [Nf numberFromString:textinput1];
NSNumber *inputnumber2 = [Nf numberFromString:textinput2];
return [inputnumber1 compare:inputnumber2];
}];
NSLog(@"The sorted list of inputs : %@", textinputs);
We can use different code blocks for sorting different kinds of inputs
For sorting dates :
NSArray *unsortedarr = [dataToDb allkeys];
NSArray *keys = [unsortedarr sortedArrayUsingComparator:^NSComparisonResult(id inp1, id inp2) {
NSDateFormatter *dformatter = [[NSDateFormatter alloc] init];
[dformatter setDateFormat:@"mm-dd-yyyy"];
NSDate *date1 = [dformatter dateFromString:(NSString*) inp1];
NSDate *date2 = [dformatter dateFromString:(NSString*) inp2];
return [date1 compare: date2];
}];
Assuming that the dictionary contains all dates and allkeys return the unsorted dictionary keys
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.