Thursday, April 03, 2008

.NET to Cocoa: + before an Objective-C method is equivalent to the static keyword in C#

I have been reading a lot of Cocoa and Objective-C documentation lately, and there are a lot of methods that begin with a +, and I didn't know what it meant. Here's an example:

+ (id)timeZoneWithName:(NSString *)aTimeZoneName

If you are coming from .NET and C#, then the + is equivalent to the static keyword (does that make the + in Objective-C a keysymbol?). The equivalent in C# would be:

public static Object TimeZoneWithName(String aTimeZoneName)

Meaning that +/static methods are at the class level, not the instance. In Cocoa/Obj-C, you would use the above class method like this:

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Some Timezone Name"];