NSNumber: What is the point ?

The first time you see NSNumber your probably say to yourself “What is the point of NSNumber? I already have int, float, double etc”. Although this may be true, there are many occasions when you actually need to use NSNumber.

Adding a number to an array

NSArray (NSMutableArray etc) does not allow you to add primitive types to it. This is because an NSArray is simple a set of pointers to the Objects you add to it. This means if you want to add a number to the array, you first have to wrap it in a NSNumber.

NSArray *numbers = [NSArray arrayWithObject:[NSNumber numberWithInteger:2]]; 

Number type conversion

NSNumber allows you to easily convert the type of a number e.g. from an int to a float

NSNumber *number = [NSNumber numberWithInteger:2];

float floatValue = [number floatValue];

Perform selector calls

Once you have used Objective-C for a while you may come across performSelector. This allows you to carry out advanced techniques like performing a selector after a given delay.

[magicObject performSelector:@selector(performMagicWithNumber:) withObject:[NSNumber numberWithInteger:2] afterDelay:1.0];  

This the equivilant of doing the following without a delay:

[magicObject performMagicWithNumber: [NSNumber numberWithInteger:2]];

Persisting Objects (CoreData)

Archiving Objects is a lot easier than archiving primitive types, as NSNumber inherits from NSObject you can use various archiving and de-archiving methods on them. CoreData actually requires you to use NSNumber for persistent storage. One thing that may not be obvious at first glance, is that CoreData (and Objective-C) considers BOOL as a NSNumber.

Summary

You may write the best selling application on the Appstore and never use NSNumber, but if you need to persist your objects or you need access to the various performSelector calls, NSNumber is obviously the way to go.

Make your iPhone vibrate

One thing that took me a while to find out, was how to make an iPhone vibrate. In the end I found out that it is in the audio APIs, and it is just the one line of code.

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Categories

Ever thought “Oh I wish Apple had included method X for class Y”, well that is what categories are for.

Categories are normally used for 2 things.

So lets look at something that is extremely popular at the moment ….. Twitter, and in paticular the 140 charachter limit.

Apple (for obvious reasons) don’t include Twitter related method calls on there string class NSString.

At the moment you would have to write everywhere in you code, something the resembles the following

NSString *twitterString = @"Objective-C rocks, I never want to look at Java again";

if ([[twitterString] length] < 141) {
 // Post to twitter
}

Now this is good, but what if twitter changed their character limit, you would have to go an edit your code everywhere, where you used this check. So we are going add a method to the NSString class:

- (BOOL)isUnderTwitterCharachterLimit;

This will make are code look like this

NSString *twitterString = @"Objective-C rocks, I never want to look at Java again";

if ([twitterString isUnderTwitterCharachterLimit]) {
 // Post to twitter
}

You now need an interface and implementation file, Im going to call them NSStringTwitterCategory.h and NSStringTwitterCategory.m

The interface file (NSStringTwitterCategory.h) is extremely small:

#import <Foundation/Foundation.h>

@interface NSString (Twitter)
- (BOOL)isUnderTwitterCharachterLimit;
@end

Instead of defining a new class in the format:

@interface NewClass: SuperClass {

}

You extend an existing class using the format:

@interface ClassYourExtending (CategoryName)

You can call the category whatever you want, but obviously you want to make it clear.

You then declare the methods like you always do.

The implementation file (NSStringTwitterCategory.m) is also not very complex:

#import "NSStringTwitterCategory.h"

@implementation NSString (Twitter)

- (BOOL)isUnderTwitterCharachterLimit {

    if ([self length] < 141)
    {
        return YES;
    } else {
        return NO;
    }
}
@end

Again you extend the class in the same way as you did in the header file:

@implementation NSString (Twitter)

And you simple define the method as you normally would, nothing strange or fancy. If you want to use this category you just need to import the header file where you want to use it.

This is obviously a very simple example, but I hope it shows you how to use categories in Objective-C.

Does a NSString contain a substring?

Here is a little tip on how to tell if a string contains another string, using the underused data type NSRange.

NSRange gives the starting location and the length of a given value, and is often used with arrays and strings. On this occasion we will use it to find the range of a substring within another string. If the range has a location, it contains the given substring. The following code does just that.

NSRange textRange = [string rangeOfString:substring];

if (textRange.location != NSNotFound) {
    // Does contain the substring
}

Making this a case insensitive compare is also very trivial, and can be done by lowercasing both strings

NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];

if (textRange.location != NSNotFound) {
    // Does contain the substring
}

Hello and Welcome

Welcome to my blog about Cocoa programming and all things Apple. I’ll warn you this is going to get very geeky :)

Page 9 of 9