I'm having a very frustrating problem with an application I'm developing, and I've narrowed it down to a single line of code. I've been able to write a tiny little application which demonstrates the problem...
The NIB file consists of a single window which has two NSTextFields and one button. The first field, "inField" allows text entry. The second field, "outField" has text placed in it by the "trim" action invoked by the button.
(I hope I'm making sense so far... the code below should help understand what I've written above).
The "trim" action is supposed to take the text from "inField", trim whitespace from the beginning and end of the text, and place the result in "outField".
In most cases this works, however if a single character (with no whitespace) is entered in "inField", the "trim" action results in an empty string!!!
The entire code of the test application is below...
Interface:
Code:
#import <Cocoa/Cocoa.h>
@interface Blah : NSObject
{
IBOutlet NSTextField *inField;
IBOutlet NSTextField *outField;
}
- (IBAction)trim:(id)sender;
@end
Implementation:
Code:
#import "Blah.h"
@implementation Blah
- (IBAction)trim:(id)sender
{
[outField setStringValue:
[[inField stringValue] stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]
]
];
}
@end
Any ideas on what I'm doing wrong, or is this a bug in the NSString method?