Borkware Miniblog

August 19, 2007

Objective-C is Stupid (but I still like it)

Filed under: irc, programming — Mark Dalrymple @ 6:47 pm

One of my guilty pleasures is hanging out on Mac programming IRC channels (#macsb and #macdev on Occasionally an interesting discussion erupts over some esoteric topic, but for the most part it’s ordinary questions about the basics of Cocoa programming.

One misconception about Objective-C that comes up occasionally is the assumption that Objective-C does more than it does. In particular things like

“I compare two strings/objects/dictionaries with ==, and the test fails. Cocoa is broken!”
(quickly followed by the Mac sucks, Xcode blows goats, and so on)


“Can I concatenate two strings by using + ?”
(quickly followed by the Mac sucks, Xcode blows goats, ObjC2 should have gotten operator overloading, and so on)

Objective-C is a pretty old language, predating Java, Perl, Pythong, Ruby, and many of the modern features of C++. It is just C with a couple of extra knobs on it, with very little extra behavior added to the language. All of the magic is in the runtime which does message dispatching. There is no operator overloading in the language. Pretty much all the language lets you do is interact with integer and floating point values, and calculate addresses via pointers and offsets. In C and Objective-C, there is no implicit complex behavior hiding behind the operators.

Now, folks who come from VB (I presume), Java, scripting languages, and C++ expect things like the == operator to be an equality operator, knowing the contents of the objects being compared; or expect the + operator to be a concatenating operator, dynamically creating objects.

Think about what happens under the hood: comparing two strings compares each of the characters in the string (looping over the contents), or comparing two dictionaries would compare each of the elements in the dictionary (looping over the keys). Likewise, to concatenate two strings, you’ll probably be looping over the two, or allocating blocks and using a memcpy()-like operation. (and in general, using concatenation to build strings can bite you when you go to internationalize your application)

C and Objective-C don’t do anything that convenient. All == does is compare a small number of bytes for an address when dealing with pointers or an integer/float value. And when you come down to it, those are the same operation. In particular, no looping. All + does is add two values. In particular, no allocations.

So, when you’re dealing with Objective-C, and things don’t behave the way you think they should, consider what’s going on under the hood:

NSString *thing1 = ...;
NSString *thing2 = ...;
if (thing1 == thing2) {

This is just pure C right here (no @‘s or []‘s are involved, so the Objective-C part of the world doesn’t kick in. No ()‘s involved, so no function calls are happening. Therefore, only a small number of bytes are involved. Each string could be megabytes long, but the comparison will just be four bytes (eight bytes on a 64-bit system). So this is just testing whether one address is equal to another address. An “identity test”, vs a test for equivalency, which is what the programmer probably wanted in the first place. Assuming that the two strings are not pointing to the same @"literal NSString", these will have different addresses, and the comparison will be a false one. (it’s not my nose, it’s a false one)

Now this code:

if ([thing1 isEqualToString: thing2]) {

Does do an equivalency test. Since you’re actually invoking a method here, and methods are arbitrary code, this has the opportunity to loop through the strings and compare them character by character.

So the moral of this poorly-organized story? C is dumb about data. It only does a few blindingly simple things. Objective-C is a very thin layer on top of C, and adds nothing to the existing C syntax. If you’re getting unexpected results with the built-in operators, you’re probably assuming the base language is doing more than it should, and you should update your expectations appropriately.

And while I’m thinking about it, I’m quite glad Objective-C does not have operator overloading. The gotchas are just too subtle, and I don’t mind trading some extra typing or an explicit function or method call in exchange for not having to track down subtle changes in behavior when some class I’m using decides to overload an operator. Hint: in C++, how does operator&& change behavior when it’s overridden, vs the compiler default? You’d be surprised how many experienced C++ developers don’t know that bit of trivia.

If you really want operator overloading (it does make sense in some situations, like the canonical matrix libraries everyone seems to be writing, or at least using for examples of why overloading is a Good Thing), there’s always Objective-C++.


  1. And don’t forget, there’s also operator,() which can’t be overridden in any way that results in correct C-style semantics! (But can be overridden in ways some crazy folks find useful. The MetaKit database overrides it, for example…)

    Comment by Chris Hanson — August 19, 2007 @ 9:42 pm

  2. You can’t test equality in Java using ==. That only checks if it’s the same object. You have to use the strings equals-method.


    if you try abc == “test”, it will fail since they are two different objects

    Comment by K — August 21, 2007 @ 12:39 pm

  3. Thanks for the correction. I know java does the concatenation thing with “+”, and I figured it did more work for “==”.

    Comment by Mark Dalrymple — August 21, 2007 @ 4:21 pm

  4. Yeah, C++ overloading blows. Especially when it comes to stuff like pre/post increment etc. Oh, and the thing with && is that it short circuits when it’s bool – basically slipped in a little lazy eval.

    One thing that is nice, in a twisted kind of way, is overloading (), so you can make things that look like functions but are really data, Eg:

    Polynomial p = Polynomial(1, 2, 3) // x^2 + 2x + 3
    std::cout << p(2); // prints 11

    Comment by mgsloan — August 22, 2007 @ 1:54 am

  5. Actually, java doen’t have operator overloading, they’ve sorta hardcoded “+” only for strings.
    I don’t know why they did it, since, like you say, “+” works and “==” not, it’s a bit confusing.
    This caused even a bit of debate on java “coolness” with my professor when i took my first java class :).

    However i like it the c way, too :).

    Comment by Biappi — August 22, 2007 @ 11:48 pm

  6. + is shorthand String concat, added for convenience. Convenience is a pretty good reason to add something.
    .equals actually compares the content of strings. Since Strings are handled in a stringpool, doing an == would be pointless (see if you find the double entendre).
    Actually, for me, the hardest thing to get in Obj-C is why o why do I have to do so much work?
    Garbage collection, instant creation, memory allocation, the whole pointer thing, putting a class description in two different files, declaring an iterator, …
    And all those special signs, +-*@ are creeping me out.
    Ahem, sorry, some more whine with my cheese please… From Java to Obj-C isn’t as painless as I thought it would be. I have still a way to go before I can fully appreciate the benefits, I think.
    Here’s hoping that Obj-C 2.0 is as big a deal as some people are claiming it is.

    Comment by Koen van Hees — October 1, 2007 @ 9:17 am

  7. I agree with you. When I started programming in Objective-C, I had problems trying to concatenate strings in the wrong way. Also the == toke me sometime. But I love Objective-C. :)

    Comment by Fernando Valente — August 28, 2010 @ 4:09 am

  8. I thought I was the only person that typed Pythong for Python. It’s something about the motor memory of typing “ong”, e.g. song, long, bong, gong, tong, wrong, etc…

    Thanks for the post.

    Comment by AndrewC — March 27, 2011 @ 4:15 pm

    • Or just thong ;-)

      I guess Objective – C vs other languages which have differences and coolness (I’m more from Java), it is about how easy it is to get started and how you use it.

      For me (having taught many students C and Java), learning Objective – C is tough. I like to learn languages which have limited stuff (keywords, ways of doing things) so that I can learn fast and think less when doing something. I just means I am lazy to learn and lazy to think. It does imply less bugs in code. But the fact that Objective – C allows more control can’t be denied. It is just that I’m too lazy to learn it, so it is proving to be hard.

      Objective-C doesn’t really do the simple but amazing apple thing! But it sure should be a very powerful language judging my simply the keyword descriptions I am reading (interfaces, protocols, categories, extensions). Java guys, be warned, if I got it right, interfaces in Objective-C is not the interfaces in Java, rather, protocols in Objective-C are what interfaces in Java ;-)

      Comment by Ravindranath Akila — August 16, 2013 @ 4:40 am

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: