开发者

Objective-C's dynamic advantages in Cocoa development?

开发者 https://www.devze.com 2023-01-19 05:43 出处:网络
So from my understanding (one of) the biggest allures of Obj-C is the dynamic, message-passing runtime.

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 u开发者_如何学JAVAnderstand how such language features are actually beneficial in terms of when actually solving a particular problem in terms of OSX or iOS application.


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.

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).


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.


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.


The answer to your question is not technical, but historical.

Steve Jobs leaves Apple and founds NExT based on Objective C a new improved Object-Oriented dynamic typed language - a giant improve to C, then returns to Apple and Apple buys NExT and uses it's core to design MAC OS X. MAC OS X and iOS frameworks are thus built in Objective-C.

Now for the technical details is a different matter.

Dynamic typing for example brings up a rainbow of programmatic potential, but takes a great chunk of debugging work from compile time to runtime, i.e. since the language is flexible it let's you go away with running apps that not necessarily are concise and/or coherent.

On the other hand Java solves all the problems hunting Objective-C and gives you a non dynamic language with introspection - allowing you all the power of dynamic typing and keeping a heavily rigid set of rules that pretty much relieve you from 98% of your debugging time.

C and C++ are bad languages of the past
Objective-C is a good language from the past
Java / C# are modern languages

It's being a descent into hell to come from extensive Java / C# programming and having to deal again with sloppier compilers to work with ObjC, but hey, that's my penalty for wanting to develop for the iPhone.

0

精彩评论

暂无评论...
验证码 换一张
取 消