Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Objective-C equivalent to C#'s 'var' keyword?

Huge proponent of using the 'var' keyword in C# for cases where it's very clear. For instance, rather than this...

ThisIsMyReallyLongFooClassName foo = new ThisIsMyReallyLongFooClassName();

I can type this...

var foo = new ThisIsMyReallyLongFooClassName();

...and I still have a strongly-typed variable. The two are, for all intents and purposes, equal. The latter is just more readable (again, because it's clear. There are cases where it isn't and 'var' shouldn't be used. I don't want this to become a discussion of that however.)

I'm wondering if Objective-C has anything similar.

like image 626
Mark A. Donohoe Avatar asked Jan 12 '13 22:01

Mark A. Donohoe


People also ask

Is Objective-C same as C?

Syntactically, Objective-C is an extension of C. So, some portion of Objective-C is exactly the same as C. Your experience of C would help learning such aspect of Objective-C. But the core part of Objective-C programming is made of Object Oriented class system, which you cannot find in C.

Can I use C in Objective-C?

You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.

Is Objective-C as fast as C?

Objective-C is slower than C/C++. The reason being the runtime of Objective-C which dispatches methods lookups dynamically at runtime the same way as Smalltalk, from which it has taken over this execution model.

What is Objective-C similar to?

The best alternative is Python, which is both free and Open Source. Other great apps like Objective-C are JavaScript, Java, C++ and C (programming language). Objective-C alternatives are mainly Programming Languages but may also be Game Development Tools.


1 Answers

Yes and no.

You can use id foo = ... which will always work, but you lose the type information.

If you really want something equivalent, you could use auto foo = ... from C++11, but then you have to compile your file as Objective-C++, which has many other side effects.

Convention is to just spell out your types; it's annoying, but unlike C++, C#, Java where templates/generics can make typenames very long, it's usually manageable in Objective-C.

like image 85
cobbal Avatar answered Oct 13 '22 00:10

cobbal