Difference between revisions of "Objective-C"
From Wikicliki
(→NSLog) |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Objective-C is a superset of C language and it is called objective-c because it is a object-oriented language. | + | Objective-C is a superset of C language and it is called objective-c because it is a object-oriented language. It is used to build Cocoa and Cocoa Touch apps. |
+ | |||
+ | On 2 March 2013, I attended a slightly haphazard course "Become a NSZombie, Introduction to iOS" at Plug-In@Blk71. | ||
+ | |||
+ | http://30d.me/challenge/ios/ | ||
=== What one needs in order to program in Objective-C === | === What one needs in order to program in Objective-C === | ||
Line 5: | Line 9: | ||
* Compiler (Xcode) | * Compiler (Xcode) | ||
* Program to design the interface (Interface Builder - which is now built into Xcode) | * Program to design the interface (Interface Builder - which is now built into Xcode) | ||
− | * Debuggers (Instruments) | + | * iOS simulator (simulate apps) |
+ | * Debuggers (Instruments - monitor performance) | ||
=== Get Xcode === | === Get Xcode === | ||
+ | * [https://developer.apple.com/xcode/ Download xcode] | ||
== Basics == | == Basics == | ||
=== Data types in Objective-C === | === Data types in Objective-C === | ||
− | * int (integer) | + | * int (integer) [ObjC NSNumber] |
− | * unsigned int | + | * unsigned int [ObjC NSNumber] |
− | * float (floating point number) | + | * float (floating point number) [ObjC NSNumber] |
− | * double (double precision) | + | * double (double precision) [ObjC NSNumber] |
− | * char (a, b, c) | + | * char (a, b, c - single character) |
* string ("a string") | * string ("a string") | ||
* bool (true/false) | * bool (true/false) | ||
+ | * arrays (NSArray) | ||
+ | * key value pair (eg: courseName = iOS) | ||
+ | |||
+ | <pre>operators still apply as expected | ||
+ | == check if equal | ||
+ | || or | ||
+ | && and | ||
+ | </pre> | ||
+ | |||
+ | === Objective C Classes === | ||
+ | Instead of calling a method you pass a message to objects. | ||
+ | A class is a collection of objects with similar behaviour and properties. Objects of same class have simlar behaviour and properties. | ||
+ | |||
+ | Most common classes: | ||
+ | * NSInteger | ||
+ | * NSNumber | ||
+ | * NSString, NSMutableString (you can modify or add things to mutables) | ||
+ | * NSSet, NSMutableSet | ||
+ | * NSArray, NSMutableArray | ||
+ | * NSDictionary, NSMutableDictionary | ||
+ | |||
=== Initialisation === | === Initialisation === | ||
* int myInteger; | * int myInteger; | ||
Line 30: | Line 57: | ||
String Formatters for the trace/nslog: | String Formatters for the trace/nslog: | ||
+ | ==== Hello World ==== | ||
+ | NSLog(@"Hello World!"); | ||
+ | ==== Commenting ==== | ||
+ | |||
+ | <pre> | ||
+ | //commenting | ||
+ | |||
+ | /* a really long | ||
+ | comment */ | ||
+ | </pre> | ||
+ | |||
+ | ==== Debugging Variables ==== | ||
<pre>NSLog(@" a %% b = %i", c) | <pre>NSLog(@" a %% b = %i", c) | ||
c = b % a;</pre> | c = b % a;</pre> | ||
Line 67: | Line 106: | ||
* http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-1/ | * http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-1/ | ||
* http://cocoadevcentral.com/d/learn_objectivec/ | * http://cocoadevcentral.com/d/learn_objectivec/ | ||
+ | |||
+ | [[Category:Programming]] |
Latest revision as of 02:37, 12 July 2013
Objective-C is a superset of C language and it is called objective-c because it is a object-oriented language. It is used to build Cocoa and Cocoa Touch apps.
On 2 March 2013, I attended a slightly haphazard course "Become a NSZombie, Introduction to iOS" at Plug-In@Blk71.
Contents
What one needs in order to program in Objective-C
- Source Code Editor (Xcode)
- Compiler (Xcode)
- Program to design the interface (Interface Builder - which is now built into Xcode)
- iOS simulator (simulate apps)
- Debuggers (Instruments - monitor performance)
Get Xcode
Basics
Data types in Objective-C
- int (integer) [ObjC NSNumber]
- unsigned int [ObjC NSNumber]
- float (floating point number) [ObjC NSNumber]
- double (double precision) [ObjC NSNumber]
- char (a, b, c - single character)
- string ("a string")
- bool (true/false)
- arrays (NSArray)
- key value pair (eg: courseName = iOS)
operators still apply as expected == check if equal || or && and
Objective C Classes
Instead of calling a method you pass a message to objects. A class is a collection of objects with similar behaviour and properties. Objects of same class have simlar behaviour and properties.
Most common classes:
- NSInteger
- NSNumber
- NSString, NSMutableString (you can modify or add things to mutables)
- NSSet, NSMutableSet
- NSArray, NSMutableArray
- NSDictionary, NSMutableDictionary
Initialisation
- int myInteger;
- int myInteger = 42;
- int myInteger, myInteger2, myInteger3;
Operators
- operators - as normally expected except there is also "modulo"
- % - result will be the remainder from the integer division of the 1st by the 2nd
- (only for int or long, insert some caveat here about floats and doubles and using fmod)
- eg: int moduloResult = a % b;
NSLog
String Formatters for the trace/nslog:
Hello World
NSLog(@"Hello World!");
Commenting
//commenting /* a really long comment */
Debugging Variables
NSLog(@" a %% b = %i", c) c = b % a;
int myNum = 7; NSString *myString = @"Dog"; NSLog(@"The number is %i and the string is %@.", myNum, myString);
If you want a % sign you have to put in %%.
NSLog(@"%@", [NSNumber numberWithInt:i]); %@ Object %d, %i signed int %u unsigned int %f float/double %1.2f to specific number of decimals %x, %X hexadecimal int %o octal int %zu size_t %p pointer %e float/double (in scientific notation) %g float/double (as %f or %e, depending on value) %s C string (bytes) %S C string (unichar) %.*s Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second) %c character %C unichar %lld long long %llu unsigned long long %Lf long double