OS-X Cocoa Quickie #2 => Know Objective-C

When working with Objective-C one key thing to keep in mind is the differences between the other “C”s like C, C++, etc. One thing that crops up sometimes is the C char type. Normally, this isn’t the most ideal thing to use. The better type to use is NSString.

[sourcecode language=”objc”]
// c string
char *foo;
foo = "This is a c string.";

// NSString
NSString *bar;
bar = @"This is an NSString.";

The way to straighten this out is to give a little conversion if you do run into some C char types.

[sourcecode language=”objc”]
const char *footoo = "blah blah";
// Convert the NSString from a c string.
NSString *foobar;
foobar = [NSString stringWithUTF8String:footoo];

// Convert the c string to an NSString.
footoo = [foobar UTF8String];

One of the other things that is a little catchy is the isEqual method.

The result of this test is yes if the receiver and anObject are equal and no otherwise. Basically, both pointers must be to the same memory locations. This however is not always equal, which can cause a bit of confusion.

There are a host of other things to keep an eye out for. So be sure to check out the specifics of how Objective-C works versus the other “C”s.