Basic Language Constructs
+ sign for static methods - sign for methods that require objects
For instance in C++ we do the following
public: static void SomeMethod();
The objective-C equivalent would be
+(void) SomeMethod;
Similarly for non-static methods just change the + sign to -. They are called Instance Methods.
-(void) SomeNonStaticMethod;
You may also have noticed that the return type is enclosed in () parenthesis which was weird to me.
No private methods
Thats right, no method is private. You can have member variables or attributes private by using @private but methods cannot be private.
Method calling
In C++ we have, when we have to call a method; we do
myObj.MethodName();
In Objective-C, its totally changed.
[myObj MethodName];
Tada! No Constructor
Yes, there is no such thing as constructor. All you do is call a native/built-in method alloc to create an object with default properties.
[MyClass alloc]; //Here is the object being created
But Objective-C developers have come up with something called initializers, so immediately after the object is created, an init method is called. Keep in mind that init is something developer has to create as a method that does some initialization work.
[ [MyClass alloc] init];
Weird asterisk * that reminds you of pointer will get on your nerve
Yes, that asterisk is for pointers. Every object reference is stored in pointers.
So in C++ we create objects by simply writing the following code
MyClass MyObj = MyClass();
In Objective-C, you will have to write the following code
MyClass *MyObj = [ [ MyClass alloc ] init];
Project files
Objective-C has similar to .h/.cpp structure for class declaration and definition but its most often called .h/.m or .h/.mm (for Objective-C++)
Keyword interface is not what you think
interface keyword in Objective-C is used for class declarations.
C++ code
class MyClass { public: MyClass(); };
Objective-C will require you to write the following code for the above
@interface MyClass { // non-static variables go here } - (MyClass*) init; @end
Class definition or .m file
.m file or .cpp file counterpart in Objective-C would look something like this
@implementation MyClass { // Yes 'class' keyword is never seen -(MyClass*) init { if (self) { // initialize self here } return self; } @end
Thats all folks! There is more information on the topic which I intend to write in future blog posts. If you are a C++ developer and thinking about taking a nosedive into Objective-C, above mentioned things should be enough for you to brace for.
I would love to hear your comments. They help me understand how useful my postings are.
I am using the Orwell version of Dev-C++ and it works very well and NOT only for 30 lines of code ! For sure it depends from what website you will download the package: be sure to have a clean file and you will download in very few seconds and you will work happily. Thanks~ Charlotte from offshore software development team
ReplyDeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
ReplyDeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Hello dear! Thanks for providing this information .I hope it will be fruitful for me. Thank you so much and keep posting
ReplyDeleteAre you planning to create an On Demand Car Services App? The App Ideas is leading web and Mobile App development. We provide the best IT Services at best rates. Contact us now!