Browsing articles tagged with " NSString"

How to Format an NSString

Dec 19, 2009   //   by Derek van Vliet   //   Development  //  2 Comments

It’s back to basics with today’s advent tip. I would be surprised if anything short of ALL developers have had a need to format strings at one time or another. iPhone app development is no exception.

String formatting allows you to build a string based on a template and varying types of values. For example, if I am keeping track of a player’s score as an integer and I want to display a string to the player that celebrates their score by saying “You scored 2600! Way to go!”, then I need string formatting to get the correct score in the middle of that string.

How to Do It

Below is an example of how to build the above NSString. This example formats a string with an integer in it using the %i format specifier.

int score = 2600;
NSString * scoreString = [NSString stringWithFormat:@"You scored %i! Way to go!",score];

Format Specifiers

But string formatting doesn’t stop at putting integers in strings. Below are some of the most common format specifiers for other value types:

Specifier Purpose
%i integer
%f float
%@ Objective-C object (commonly used to specify another NSString)
%x hexadecimal

String Comparison Using NSString

Dec 10, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

Photo-2a

One of the most common things a programmer needs to learn how to do when faced with a new API is how to compare one string to another to see if the two are the same or how they differ.

Because of this, many APIs have created some handy string comparison features, and the NSString class in objective-c is no exception. What follows are some invaluable tools for string comparison in objective-c.

The Is-Equal Method

The NSString class responds to a message and returns whether or not the NSString you pass in is equal or not:

NSString * str = @"oranges";
BOOL res = [str isEqualToString:@"apples"];

The Compare Method

The NSString class also has a method called compare, which gives you a bit more info in the return results. It can actually tell you the difference between the two strings; whether or not the string you passed in is:

  • the same,
  • in ascending sort order to the string you call the message on or
  • in descending sort order to the string you call the message on
NSString * str = @"oranges";
NSComparisonResult res = [str compare:@"apples"];
 
switch (res) {
	case NSOrderedAscending:
		// going up
		break;
	case NSOrderedSame:
		// even steven
		break;
	case NSOrderedDescending:
		// down i go
		break;
	default:
		break;
}

Insensitivity to Case

Finally, the NSString class also features a method for comparing strings without case sensitivity. This is useful for comparing strings with unpredictable case, such as user input. Like the compare method, it returns an NSComparisonResult enumerated value which allows you to see the equality or sort-order of the two strings.

NSString * str = @"APPLES";
NSComparisonResult res = [str caseInsensitiveCompare:@"apples"];
 
switch (res) {
	case NSOrderedAscending:
		// onward and upward
		break;
	case NSOrderedSame:
		// same old
		break;
	case NSOrderedDescending:
		// downfall
		break;
	default:
		break;
}

Our Games

Latest Tweets