The NSArray class is often used as a container for NSString objects, making it one of the most useful classes in the entire Foundation framework. In addition to providing indexed storage for objects, we can also sort the objects using either a predefined selector or one that we provide ourselves. In this blog, we’ll sort strings entered by the user in a text field and present the strings in sorted order in a text view. So let’s get started!
Start Xcode, select “Create a new Xcode project” and choose the Single View Application template. Name the project StringSorter, and make the following option choices:
Click Next, choose a location to save the project, and Click Create.
We’ll begin with ViewController.h. Make these changes:
@interface ViewController : UIViewController
@property (nonatomic, weak) IBOutlet UITextField *entry;
@property (nonatomic, weak) IBOutlet UITextView *sorted;
@property (nonatomic, strong) NSMutableArray *entryStrings;
- (IBAction)textEntered:(UITextField *)sender;
@end
Three properties are created here: a text field named entry and a text view named sorted, and an NSMutableArray (entryStrings) that will contain the strings entered in the text field. We’ve also declared an action method (textEntered:) to be fired each time the user finishes entering a string.
In ViewController.xib, add the text field and text view to the main view. Also add a UILabel to promt the user for entry:
Set the text view’s Editable property to unchecked in the Inspector:
Wire up the text field to the “entry” outlet property, and the text view to the “sorted” outlet property. As the user enters strings in the text field, we want to present those strings in sorted order in the text view, so wire the “Did End On Exit” event of the text field to the textEntered: method:
Finally, in the Attributes Inspector for the text field, set the Return Key to Done:
With all these settings in place, we are ready to implement the textEntered: method in ViewController.m:
@interface ViewController ()
@end
@implementation ViewController
@synthesize entry, sorted;
@synthesize entryStrings;
- (NSMutableArray *)entryStrings
{
if (!entryStrings) {
entryStrings = [[NSMutableArray alloc] initWithCapacity:5];
}
return entryStrings;
}
- (IBAction)textEntered:(UITextField *)sender
{
[sender resignFirstResponder];
[self.entryStrings addObject:self.entry.text];
NSArray *sortedStrings = [self.entryStrings sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
self.sorted.text = @"";
for (NSString *singleEntry in sortedStrings) {
self.sorted.text = [self.sorted.text stringByAppendingFormat:@"%@\n", singleEntry];
}
NSLog(@"%@", sortedStrings);
sender.text = @"";
}
After synthesizing the properties, we must make sure that the entryStrings object is instantiated. We do this by overriding the getter, and instantiating the property if it doesn’t already exist. In the textEntered: method, we first resign the first responder on the text field (which hides the keyboard). We then add the text in the text field to the entryStrings array.
The next line performs the sort. We create a new array named sortedStrings using the strings in the entryStrings array, sorted using the selector caseInsensitiveCompare:. This selector is available to all NSArrays.
We set the text of the text view to the empty string before displaying the new list of strings. Then we step through the sortedStrings array and print each string, followed by a newline. Finally, we clear the text in the entry text field.
Running the app gives the results shown:
Note that as each string is added, it is entered into its proper (sorted) place in the text view.
Source : edumobile[dot]org
0 comments:
Post a Comment