Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [103]

By Root 863 0
percentage.

The most basic formatter will implement two methods:

- (BOOL)getObjectValue:(id *)anObject

forString:(NSString *)aString

errorDescription:(NSString **)errorPtr

This message is sent by the control (such as a text field) to the formatter when it has to convert aString into an object; aString is the string that the user typed in. The formatter can return YES and set anObject to point to the new object. A return of NO indicates that the string could not be converted, and the errorPtr is set to indicate what went wrong. Note that errorPtr is a pointer to a pointer, as is anObject. That is, it is a location where you can put a pointer to the string.

- (NSString *)stringForObjectValue:(id)anObject

This message is sent by the control to the formatter when it has to convert anObject into a string. The control will display the string that is returned for the user (Figure 26.2).

Figure 26.2. NSFormatter

Often the object that is created from the string is also a string. For example, you might have a TelephoneNumberFormatter that properly inserts the parentheses and dashes into a telephone number.

A Basic Formatter


In this chapter, you will write your own formatter class. You will create a formatter that allows the user to type in the name of a color, and the formatter will in turn create the appropriate NSColor object. Then you will set the background of the BigLetterView with that color object. Figure 26.3 shows what the application will look like when you are done.

Figure 26.3. Completed Application

Create ColorFormatter.h


Open the TypingTutor project in Xcode and create a new Objective-C class that is a subclass of NSFormatter. Name it ColorFormatter.

In ColorFormatter.h, add an instance variable:

#import

@interface ColorFormatter : NSFormatter {

NSColorList *colorList;

}

@end

Save the file.

Edit the XIB File


Open MainMenu.xib. Drop a color well and a text field onto the window (Figure 26.4).

Figure 26.4. Add Color Well and Text Field

Bind the value of the color well to the TutorController’s inLetterView.bgColor (Figure 26.5).

Figure 26.5. Bind Value of Color Well to bgColor of inLetterView

Bind the value of the text field to the same key path (Figure 26.6).

Figure 26.6. Bind Value of Text Field to bgColor of inLetterView

Drop an NSObject in the editor. Set its class to be ColorFormatter (Figure 26.7).

Figure 26.7. Create an Instance of ColorFormatter

Control-click on the text field to bring up its Connection panel. Set the formatter outlet to point to the ColorFormatter object (Figure 26.8).

Figure 26.8. Set the Text Field’s Formatter Outlet

NSColorList


For this exercise, you will use an NSColorList, a dictionary of color objects that maps a name to an instance of NSColor objects. Several color lists come standard with Mac OS X. In particular, the color list named “Apple” includes many of the standard colors, such as “Purple” and “Yellow.”

NSColorList is not a particularly useful class, but it makes this exercise very elegant. We will not spend much time discussing it.

Searching Strings for Substrings


When you have a string dakakookookakoo and are searching through it for a shorter string, such as ka, the result will be an NSRange. The location is the first letter of the matching substring in the longer string. The length is the length of the substring.

Of course, there are a couple of options that you might want to set. For example, you might want to do a case-insensitive search. Or you might want to do a backward search (from the end of the string instead of the beginning). To search backward for the string KA in abbakachakaza in a case-insensitive manner, you would use the following code:

NSRange aRange;

NSString *big = @"abbakachakazazzz";

NSString *small = @"KA";

aRange = [big rangeOfString:small

options:(NSCaseInsensitiveSearch | NSBackwardsSearch)];

After this code executes, aRange.location would be 9, and aRange.length would be 2.

If

Return Main Page Previous Page Next Page

®Online Book Reader