Search VG Five
Categories & Tags

Entries in objective-c (2)

Wednesday
06May2009

NSString - Comparing two strings in Objective-C

Comparing strings in Obj-C is not done the same way as comparing two integers, bools, or even characters. For example, the following code will fail to get the desired results:

if (someString == @"someText") { // do stuff }

This will never run, because you are comparing pointers and not the content of the two strings. Instead, use this:

if ([someString isEqualToString:@"someText"]) { // do stuff }

 

Thursday
23Apr2009

UISearchBar - Disabling autocorrection and capitalization

One of the most basic things you don't want when searching is for the search engine to correct your search on the fly. Google does a good job with asking you if you meant a similiar spelling or choice of words, but you still want to be sure that any search term you type is the one that gets sent in the first place.

On the iPhone, the autocorrection is turned on for all text entry fields by default. Here's how to turn both autocorrection and autocapitalization off:

UISearchBar.autocorrectionType = UITextAutocorrectionTypeNo;

UISearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

You will want to replace UISearchBar with the name of your object.

This is the default behavior that I (and I assume most users) expect when they start typing into a search bar. Of course, some programs may benefit from autocorrection.