Introduction Objective-C for Mac or iOS
Why Objective-C used in the Mac operating system? because Steve Jobs (founder of Apple Inc.) create a company named NeXT were using Objective-C licensed from stepstone to make the NextStep operating system. Because NeXT was acquired by Apple Inc. then NextStep dimodif into Mac OS and of course built using Objective-C. Objective-C thus become the main programming language on the products MacIntosh.
Objective-C is made by Brad Cox and Tom Love 80s. His company was named stepstone. In terms of orders, Objective-C is a superset of C. This means that Objective-C command relatively similar but more than the language commands C. To make use Obejctive-C program is already available for Linux, Mac, and Window. Please search on Google if you're still confused.
It should be understood that Objective C is not a separate language such as C #, C++, or Java. Objective C is the C language, written with object oriented syntax. So if you type in C and compiled, then there would be no problem.
Objective-C is the main native programming language used to create a Mac application or programmatically in iphone (iOS) This language diextend of the C language using style of Smalltalk. All the syntax for non-object-oriented operation is similar to C, while for implementing object-oriented operation of Smalltalk style. So, should you have an adequate basis in the programming language C.
Maybe you will feel weird with objective-c of this structure, when viewed sure you feel confused, but do not worry because almost everyone has the same opinion.
NSString
In C, a string variable is usually declared as follows:
char *s = "this is a string";
With NSString, becomes:
NSString *s = @"this is a string";
NSString here is the string object.
@ "String" will create a string object, which can be directly diassign into a variable or become arguments in a function.
Method of Use
In the language of C or PHP, the calling method is usually done in the following ways:
addressBook.displayNames();addressBook.displayNamesWithPrefix( prefix );
Here's how the Objective-C:
[addressBook displayNames];[addressBook displayNamesWithPrefix: prefix];
If you look at the example above, the format of the overall use of the method can be summarized as follows:
[object method]; [object methodWithInput:input];
For a function that returns a value, the same format:
output = [object methodWithOutput]; output = [object methodWithInputAndOutput:input];
In addition to the calling function on a ojbect, we are also able to call functions in a class.
For example, the calling function strings in the NSString class, which returns a NSString object:
NSString* str = [NSString string];
Remember, all of the variables in Objective-C object pointer type so there should be a symbol of asterisk (*) on the right object type, except type "id" as it bertype pointers are predefined.
Nested Methods
We can also use a method / function as a parameter to another function, this is called a nested methods. Example:
function1 ( function2() )stringWithFormat ( format())
the result of a formatting function () becomes input / parameter in stringWithFormat ().
In Objective-C, the format of the following:
[NSString stringWithFormat:[prefs format]]
Multiple Arguments
The following example uses three pieces of arguments (strings, date and integer).
[ptr setStr:@"A new test..." andDate:[NSDate date] andInteger:99];
Previously, declared as follows:
-(void) setStr:(NSString *)str andDate:(NSDate *)date andInteger:(int)x;Implementation of the method setStr () that uses three arguments:
-(void) setStr:(NSString *)strInput andDate:(NSDate *)dateInput andInteger:(int)xInput{ [self setStr:strInput]; [self setDate:dateInput]; [self setX:xInput];}
You can look at the current methods setStr () is defined, we use the variable str, date and x. But in its implementation, we use a variable strInput, dateInput, and XInput. This is due to objective-c requires the name of a local variable (the method) should not be the same as the variable names defined.
Accessors
All the instance variable is private by default, so you must use accessors to get / set the value at the instance variable.
setter:
[photo setCaption:@"Day at the Beach"];
getter:
output = [photo caption];
Wherever you see the code in the "square brackets", you send a message to the object or class.
loading...
loading...

Comments
Post a Comment