Saturday, December 11, 2010

Love/Hate Objective C

I recently realized that the learning curve of Java on Android compare to Objective C on iOS is a lot better. The documentations are robust anyway let's just cut the chase this post is about the beauty of Objective C .

OBJECTIVE C for starter

1.) NSLog is a function that
-- Can take one or more parameters. The first parameter is generally the string that is to be printed to the console. The @ symbol in front of the string tells the compiler that this is an Objective-C type string and not a C++ string. The @ symbol is typically used in front of all your strings for iPhone apps. If you do not use the @ symbol, you will probably get a compiler error.
2.) % is called the modulus operator.
This operator returns the remainder of its two operands.

3.) @ tells the O/S that is an objective C file

Classes Terminologies

4.) Pointers are like business card you don't see the real person yet you have the idea that you can reach him anytime.

5.) Two types of class methods
a.) class-method this are static function that uses the + sign (synonymous to public static function myMethod() in php )
+ used for messages that can be used before the object is created. (pseudo static )
b.) instance-method this are the default function of a class denoted by the - sign ( synonymous to private function myMethod() in php)
-(float) Power:(float)width:(float)height; in C/C++ you would write it like this float Power( float width , float height)

- (id)initWithName:(NSString *)newName atFrequency:(double)newFreq {
public function WithNameatFreq(new Name , new Freq)
char getName(char name)
{
}
@interface classname : superclassname {
// instance variables
}
+classMethod1;
+(return_type)classMethod2;
+(return_type)classMethod3:(param1_type)param1_varName;
-(return_type)instanceMethod1:(param1_type)param1_varName :(param2_type)param2_varName;
-(return_type)instanceMethod2WithParameter:(param1_type)param1_varName andOtherParameter:(param2_type)param2_varName;
@end

Note:
Class members declared PUBLIC can be accessed everywhere.
Members declared PROTECTED can be accessed only within the class itself and by inherited and parent classes.
Members declared as PRIVATE may only be accessed by the class that defines the member.
A private method is a method which is not inherited by subclasses. A private variable cannot be used outside the class
A public method is a method which can be called by any object of the class
A protected method can be called by any subclass within its class, but not by unreleated classes.

How to create a new Objective C class project

1.Choose MACOSX application
2.Select Command Line Tool ,ensure that Foundation is chosen for the type
3.If you want to add new objects Add New File
then choose Objective-C class from the MACOSX group make sure the Subclass
of dropdown is set to NSObject
4.How to initialize a class

myObject = [[MyClass alloc] init];
--
HelloWorld* myObject = [[HelloWorld alloc] init];
[myObject printGreeting];
[myObject release]; return 0;

self = meaning successful
nil = meaning failure

Happy coding! :)

0 comments:

Post a Comment