Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
What is Objective-C
Objective-C is a strict superset of C, we are free to use C in an Objective-C file and it will compile fine.
What will I need?
Since I installed Xcode on my MAC, I already have GCC.
Compiling your code
Open the MAC terminal and go to the work directory.
>cd /Users/carl/work/objective-c
>vi First.m
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
>gcc First.m -o First
The system will compile the codes in First.m to machine codes into First.
Then I can run the codes with these commands>
>./First
I can get the println Hello World. I am a good student when I was in university while studying the C language.
That should be awesome to study objective-c from my understanding.
The Basics
int main(){
printf("Hello World\n");
return 0;
}
By convention, returning '0' signals to the calling program that our program finished execution without any errors.
Variables
int - for storing integers with no decimal point
char - for storing a character
float - for storing numbers with decimal points
double - same as float but double the accuracy
If we are not using variables, we are using constants. For instance, 123 + 2 = 125
Almost the same way of C language>
#include <stdio.h>
int main(){
int someNumber = 123;
printf("My number is %i \n", someNumber);
return 0;
}
%i integer, %f float, %e double, %c char
Conditionals
int main(){
if(1 == 1){
//...snip...
}
return 0;
}
Loops
for (x = 0; x < 9; x++){
printf("Count is: %i\n", x);
}
while ( x < 10){
x++;
printf("Count is: %i\n", x);
}
do {
x++;
printf("Count is: %i\n", x);
} while(x < 10);
Pointers
Pointers point to a location. Specifically, locations in computer memory. We have a variable named foo with value 123, we can also have a point points to foo.
int foo = 123;
int *ptr = &foo;
Wrapping Up
…snip…
Object Orientated Programming
Classes are made up of methods and attributes.
Interface and Implementation
Interface is a file that ends with a suffix of .h. Implementation is a file that ends with a suffix of .m.
Interface
@interface Car: NSObject
@property (weak, nonatomic) IBoutlet UILabel *display;
- (void) addGas;
@end
Car is extends from Object NSObject
The "-" indicates that this is an instance method, not a class method.
Implementation
#import "Car.h"
@implementation Car
- (void) addGas {
}
@end
Classes from Apple
NS is abbreviation for NextStep.
NSString immutable string
NSMutableString mutable string
NSArray immutable array
NSMutableArray mutable array
NSNumber
Pointers and Initializing
File --> New Project ---> Mac OS X--> Application ---> Command Line Tool ----> Type: Foundation
#import <Foundation/Foundation.h>
int main(int argc, constchar * argv[]) {
NSString *testString;
//pointer to NSString
testString = [[NSString alloc] init];
//have instance of NSString
testString = @"sillycat test String";
//@ a sign of NSString
NSLog(@"only string= %@",testString);
//%@ means an Objetive-C object
return 0;
}
Inheritance
NSObject ---> NSString ----> NSMutableString
---> NSArray -----> NSMutableArray
---> NSValue -----> NSNumber
References:
http://www.otierney.net/objective-c.html.zh-tw.big5
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/Introduction.html
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-1/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-2/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-3/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-4/
Objective C Book from Apple
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html