Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.
Programming in Objective-C will not become obsolete any time soon because, thanks to its 20 years of existence, it has a large code base, a number of apps maintained, and third-party framework with Objective-C at its core. These solutions and libraries are unlikely to be rebuilt from scratch with a new language.
Objective-C and C# are very different languages both syntactically and from a runtime standpoint. Objective-C is a dynamic language and uses a message passing scheme, whereas C# is statically typed.
If you want to get work done, Objective-C is the way to go. Swift is the new kid on the block, but it's still a kid. If you want to get work done, Objective-C is the way to go. At the time of writing, Apple's software development kits are primarily written in C and Objective-C.
Short list of some of the major differences:
bool
, true
and false
, Objective-C uses BOOL
, YES
and NO
.void*
and nullptr
, Objective-C prefers id
and nil
.SEL
) as an approximate equivalent to function pointers.nil
, unlike C++ which will crash if you try to call a member function of nullptr
self
, and allows class initialisers (similar to constructors) to return a completely different class if desired. Contrast to C++, where if you create a new instance of a class (either implicitly on the stack, or explicitly through new
) it is guaranteed to be of the type you originally specified.int foo (void)
and int foo (int)
define an implicit overload of the method foo
, but to achieve the same in Objective-C requires the explicit overloads - (int) foo
and - (int) foo:(int) intParam
. This is due to Objective-C's named parameters being functionally equivalent to C++'s name mangling.alloc
message, or implicitly in an appropriate factory method).In my opinion, probably the biggest difference is the syntax. You can achieve essentially the same things in either language, but in my opinion the C++ syntax is simpler while some of Objective-C's features make certain tasks (such as GUI design) easier thanks to dynamic dispatch.
Probably plenty of other things too that I've missed, I'll update with any other things I think of. Other than that, can highly recommend the guide LiraNuna pointed you to. Incidentally, another site of interest might be this.
I should also point out that I'm just starting learning Objective-C myself, and as such a lot of the above may not quite be correct or complete - I apologise if that's the case, and welcome suggestions for improvement.
EDIT: updated to address the points raised in the following comments, added a few more items to the list.
While they are both rooted in C, they are two completely different languages.
A major difference is that Objective-C is focused on runtime-decisions for dispatching and heavily depends on its runtime library to handle inheritance and polymorphism, while in C++ the focus usually lies on static, compile time, decisions.
Regarding libraries, you can use plain C libraries in both languages - but their native libraries are completely different.
Of interest though is that you can mix both languages (with some limitations). The result is called Objective-C++.
Off the top of my head:
To me, the biggest difference is the model system. Obj-C lets you do messaging and introspection, but C++ has the ever-so-powerful templates.
Each have their strengths.
They're completely different. Objective C has more in common with Smalltalk than with C++ (well, except for the syntax, really).
As others have said, Objective-C is much more dynamic in terms of how it thinks of objects vs. C++'s fairly static realm.
Objective-C, being in the Smalltalk lineage of object-oriented languages, has a concept of objects that is very similar to that of Java, Python, and other "standard", non-C++ object-oriented languages. Lots of dynamic dispatch, no operator overloading, send messages around.
C++ is its own weird animal; it mostly skipped the Smalltalk portion of the family tree. In some ways, it has a good module system with support for inheritance that happens to be able to be used for object-oriented programming. Things are much more static (overridable methods are not the default, for example).
Objective-C is a more perfect superset of C. In C and Objective-C implicit casting from void*
to a struct pointer is allowed.
Foo* bar = malloc(sizeof(Foo));
C++ will not compile unless the void
pointer is explicitly cast:
Foo* bar = (Foo*)malloc(sizeof(Foo));
The relevance of this to every day programming is zero, just a fun trivia fact.
Obj-C has much more dynamic capabilities in the language itself, whereas C++ is more focused on compile-time capabilities with some dynamic capabilities.
In, C++ parametric polymorphism is checked at compile-time, whereas in Obj-C, parametric polymorphism is achieved through dynamic dispatch and is not checked at compile-time.
Obj-C is very dynamic in nature. You can add methods to a class during run-time. Also, it has introspection at run-time to look at classes. In C++, the definition of class can't change, and all introspection must be done at compile-time. Although, the dynamic nature of Obj-C could be achieved in C++ using a map of functions(or something like that), it is still more verbose than in Obj-C.
In C++, there is a lot more checks that can be done at compile time. For example, using a variant type(like a union) the compiler can enforce that all cases are written or handled. So you don't forget about handling the edge cases of a problem. However, all these checks come at a price when compiling. Obj-C is much faster at compiling than C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With