views:

159

answers:

3

So from my understanding (one of) the biggest allures of Obj-C is the dynamic, message-passing runtime.

How's this and other features overall beneficial to Cocoa development? Why was Obj-C used for Cocoa dev and not C++/C?

Basically I'm trying to understand how such language features are actually beneficial in terms of when actually solving a particular problem in terms of OSX or iOS application.

A: 

My two cents:

  1. Single Inheritance: Reduces Coupling of classes yet, some (myself included) just think its a handicap.
  2. Has protocols: C++ doesn't.
  3. Messaging system: It's not per say an advantage, but it is different than the dot syntax use in C++.
  4. Strict super set of C: C++ is just mostly a super set of C.
  5. You can use Objective-C++ which gives you the power of C++ with Objective-C.
thyrgle
In C++ you would typically use multiple inheritance to implement an interface (aka protocol).
Ferruccio
A: 

I've been using C++ and Objective-C++ extensively. My take is that the messaging system solves a lot of issues. A lot of design patterns/hacks in C++ are trivial in Objective-C.

The one issue is that the messaging system (correct me if I'm wrong) is insecure in that the messages are passed as text. It also seems a lot easier to reverse engineer an objective-c program.

John Smith
Selectors include a textual name (the `initWithFoo:bar:baz:` part), but the arguments are passed the same as to a C function (because the method implementation *is* a C function). I'm not sure what you mean by “the messaging system … is insecure”. You're correct that Objective-C classes are very easy to reverse-engineer; all you need is class-dump.
Peter Hosey
Any examples of design patterns/hacks which are trivial in Obj-C over C++?
Maverick
Messages are not passed as text. Message dispatch consists of looking up pointers to code in method tables, pushing parameters on the stack and then calling a function pointer.
NSResponder
+4  A: 

Major pieces of the Cocoa frameworks are difficult (not impossible, of course) in less dynamic languages. Key-value coding (which makes use of dynamic dispatch and runtime class and hierarchy modification) are very hard to do in compile-time bound (method-calling) languages. Core Data's synthesis of attribute getters/setters is a similar example of Objective-C's dynamic messaging (and dynamic runtime) making things much easier.

As an example, Apple's Cocoa-Java bridge (which was written by some very smart folks) was deprecated right about the time Key-Value coding and observing were introduced as major patterns in the Cocoa MVC stack. Making the Java runtime play nicely with those new features was deemed too difficult to pull off in a reasonable effort.

Finally, Objective-C's dynamic messaging was designed specifically to solve the dependency problem in large projects. Whereas a compile-time bound language like C++ requires that dependent modules be recompiled together, Objective-C's runtime-bound messaging means that you don't always have to re-compile everything. At the time of NeXT's decision to use Objective-C over (the, at the time, immature C++), that was a major factor in developer sanity. It still makes large systems easier to manage (though, again, it's certainly not impossible in C++ or Java or ..., just much harder).

Barry Wark
The Cocoa-Java bridge's lifespan and the prevalence of KVO/KVC are entirely orthogonal....
bbum
@bbum, thanks for the insider knowledge. I'd heard (though never officially) that things like KVO were too hard in the Java bridge. Any chance you can enlighten us on the reasons for its deprecation?
Barry Wark