变量:
语法:
type name
int highScore;
type name value
int highScore = 100;
1 int minutes = 60; 2 int hour = 24; 3 int dyas = 365; 4 int minutesInAYear = minutes * hours * days;
注:ObjectC还有Dynamic Typing变量
变量类型:
int
int highScore; unsugned int highScore; long int highScore;
(-2.1billion ~ 2.1billion) (0 ~ 4.3billion) 如果Compile 32-bit,范围同一般的highScore如果compile 64-bit的范围是8bytes
short int smallScroe
-32,767 ~ +32,767
注:int highScore 永远都是4bytes(hold 4 bytes or 32 bits)
long long int highScore 永远都是8bytes
float/double
type name value
float myFloat = 7.2f; (4 bytes) 加f结尾是因为写任何带小数的值,如果不加f结尾默认是double型
double myDouble = 7.2; (8 bytes)
ex:
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 // insert code here... 7 float myFloat = 7.2f; 8 double myDouble = 7.2; 9 NSLog(@"The value of myFloat is %f", myFloat); 10 //%f for decimal representation default will output 6 places -> 7.200000 11 NSLog(@"The value of myFloat is %e", myFloat); 12 //%e for exponential notation -> 7.200000e+00 13 NSLog(@"The value of myDouble is %f", myDouble); 14 //%f for decimal representation default will output 6 places -> 7.200000 15 16 } 17 return 0; 18 }
Cast:
int a = 25; int b = 2; float result = a/b; NSLog(@"The result is %f", result); //当int和float混用的时候,结果为12.000000,会舍去小数点后面的数值 int a = 25; int b = 2; float result = (float)a/b; NSLog(@"The result is %f", result); //我们需要用Cast,结果为12.500000
char
can hold more than one character(one byte)
char myChar = 'b',
NSlog(@"The char is %c", myChar);
//这些也符合注意7这里是char不是数值 'i' '7'
BOOL
BOOL isCompleted = YES;
//not bool, but BOOL
//not true or false, but YES or NO
NSlog(@"The char is %i", isCompleted);
//use %i output BOOL
//YES == 1, NO == 0
NSString
objectiveC增加的类型,他的位置在#import <Foundation/Foundation.h>这里,如下图。
语法如下:
type pointer name value
NSString *message = @"Hello";
这里实际是创建一个object message,message 指向一个NSString的一块内存, 内存里存储的是“@Hello”,message指向这块内存
注意:在objectiveC里只要创建object前面就要加*指向对象的指针
1 #import <Foundation/Foundation.h> 2 3 4 int main(int argc, const char * argv[]) 5 { 6 @autoreleasepool { 7 8 // insert code here... 9 NSString * message = @"Hello"; //变量message is a pointer to NSString 10 11 NSString * message; 12 message = @"Hello"; //分开写是这样 13 14 NSLog(@"The value of message is %@", message); 15 //%@ is placeholder of objects 16 //the string is objects 17 } 18 return 0; 19 }
Variable Scope
1 #import <Foundation/Foundation.h> 2 3 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 // insert code here... 10 for(int i = 0; i<10; i++){ 11 int foo = 55; 12 NSLog(@"The value of foo is %i", foo); 13 } 14 15 NSLog(@"The last value of foo was &i", foo); 16 //Use of undeclared identifier 'foo',foo只存在于for循环里 17 18 } 19 return 0; 20 }
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 int x = 10; 9 10 // insert code here... 11 for(int i = 0; i<10; i++){ 12 int foo = 55; 13 x++; //可以访问x 14 NSLog(@"The value of foo is %i", foo); 15 } 16 17 NSLog(@"The last value of x was %i", x); 18 //可以访问x 19 20 } 21 return 0; 22 }
1 #import <Foundation/Foundation.h> 2 3 void myFunction(int someValue){ 4 someValue = someValue + 10; 5 //Change the copy of foo not foo 6 NSLog(@"The value passed in was %i", someValue); 7 } 8 9 int main(int argc, const char * argv[]) 10 { 11 12 @autoreleasepool { 13 14 // insert code here... 15 for(int i = 0; i<10; i++){ 16 int foo = 55; 17 18 myFunction(foo); 19 //Can call myFunction. 20 //Pass in the copy of foo not foo itself 21 22 NSLog(@"The value of foo is %i", foo); 23 } 24 } 25 return 0; 26 }
Gloable Variable:
1 #import <Foundation/Foundation.h> 2 3 int bar = 10; 4 5 void myFunction(int someValue){ 6 bar++; //Globle bar, can call anywhere 7 NSLog(@"The value passed in was %i", someValue); 8 } 9 10 int main(int argc, const char * argv[]) 11 { 12 13 @autoreleasepool { 14 15 // insert code here... 16 for(int i = 0; i<10; i++){ 17 int foo = 55; 18 bar++; //Globle bar, can call anywhere 19 myFunction(foo); 20 NSLog(@"The value of foo is %i", foo); 21 } 22 } 23 return 0; 24 }
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 int foo = 10; 6 //int foo = 11; can not redefine the vaiable 7 @autoreleasepool { 8 9 // insert code here... 10 for(int i = 0; i<10; i++){ 11 int foo = 55; //can redefine the variable 12 NSLog(@"The value of foo is %i", foo); //Closest foo win 13 } 14 NSLog(@"The value of foo is %i", foo); //Closest foo win 15 16 } 17 return 0; 18 }
Enumerations
1 int main(int argc, const char * argv[]) 2 { 3 //int foo = 11; not create because re-write the same scope variable 4 @autoreleasepool { 5 // insert code here... 6 enum seatPreference { 7 window = 99, 8 aisle = 199, 9 middle = 399 10 }; 11 enum seatPreference bobSeatPreference = aisle; 12 enum seatPreference shawnPreference = window; 13 14 if(shawnPreference == window){ 15 //do something 16 } 17 18 NSLog(@"bob wants %i", bobSeatPreference); 19 } 20 return 0; 21 }
typedef
1 #import <Foundation/Foundation.h> 2 3 typedef enum {window = 99, aisle = 199, middle = 399} seatPreference; 4 //define a new type seatPreference 调用seatPreference前面不用加enum 5 6 int main(int argc, const char * argv[]) 7 { 8 //int foo = 11; not create because re-write the same scope variable 9 @autoreleasepool { 10 // insert code here... 11 seatPreference bobSeatPreference = aisle; 12 seatPreference shawnPreference = window; 13 14 if(shawnPreference == window){ 15 //do something 16 } 17 18 NSLog(@"bob wants %i", bobSeatPreference); 19 } 20 return 0; 21 }
Preprocessor directives
1 #import <Foundation/Foundation.h> 2 //preprocessor directive: mean it causes something 3 //simple to happen before our code compiled 4 //程序compile之前,copy Foundation.h and whatever it includes 替换#这行, 5 //等于加载了另外一个程序的语句 6 7 #define PI 3.141596 //define macro 8 9 int main(int argc, const char * argv[]) 10 { 11 @autoreleasepool { 12 13 // insert code here... 14 int a = PI + 500; 15 16 NSLog(@"%i", a); //同#import这里直接吧PI的值copy过来替换 17 a++; 18 NSLog(@"The max value of pre-defined macro of INT-32bit is %i", INT32_MAX); 19 } 20 #if DEBUG 21 NSLog(@"I am from Debug Mode"); //Before compile xcode检查如果是DEBUG Mode则留着这行做compile,如果不是则在compile时删除这行 22 #endif 23 24 return 0; 25 }
输出:用来输出道Console,可以用来做Debug
1 NSLog(@"There are %i minutes in a year.", minutesInAYear);
因为ObjectC从C发展而来,那时还没有String类型,所以要在“”前面加上@,代表这是ObjectC定义的。记住,需要“something”的时候前面要加@
一些程序语言我们会写: “There are” + minutes + “in a year.”
ObjectiveC:我们先写成 @“There are in a year.” 中间加入place holder:%i变成 @“There are %i in a year.” placeholder(i)前面要有@,告诉ObjectiveC什么类型的数据要放在这里。接下来加上需要和placeholder换的数据。@“There are %i in a year.” , minutesInAYear
ex:
@"There are in a day year.", minutes, days
注:%i integer
%f floating-point
%c single characters
Condition Code:
1 if( a >90){ 2 //code goes here 3 //.... 4 } 5 6 if(b<80){ 7 //code goes here 8 //.... 9 } 10 11 if(c == 90){ 12 //code goes here 13 //.... 14 } 15 16 if(c != 100){ 17 //code goes here 18 //.... 19 }
ex:
1 int category = 47; 2 if(category ==40){ 3 //do one things 4 } 5 else{ 6 if(category == 41){ 7 //do one things 8 }else{ 9 if(category == 42){ 10 //do one things 11 }else{ 12 //etc... 13 } 14 } 15 } 16 }
Condition Code:
上面的写法太繁琐,换成switch语句
1 //create a variable 2 int category = 42; 3 4 switch(category){ 5 case 40: 6 NSLog(@"It's a category 40"); 7 break; 8 case 41: 9 NSLog(@"It's a category 41"); 10 break; 11 case 42: 12 NSLog(@"It's a category 42"); 13 case 43: 14 case 44: 15 NSLog(@"It's a category 43, or 44"); 16 break; 17 default: 18 NSlog(@"I don't know what it was!"); 19 break; 20 }
因为category=42,进入第19句输出42,后因为没有break,继续执行22句。
注:case 43, case 44任何一个进入都执行22句。
没有break就继续执行下面的语句。
Operator:
+: Addision
-: Substraction
*: Multiplication
/: Division: 注: int year = 2003; reminder = year/4 //reminder = 500
=: Assignment
+=,-=,*=,/=
Comparision:
if (a == b) {... if (a != b) {... if (a > b) {... if (a < b) {... if (a >= b) {... if (a <= b) {...
Logical And/Or:
if (a == b && c == d) {... if (a == b || c == d) {... if ( (a > b) && (c < d) ) {...
Modulus:
int Year = 2003; int remainder = Year % 4; //remainder is 3
Increment/Decrement:
a = a + 1; a = a - 1; a += 1; a -= 1; a++; ++a; a--; --a;
上面结果都一样。 这里++a和a++都是a+1,没有区别。
最好执行++a,a++在自己的语句中,这样就没有区别。如果和别的语句混搭(if statement or NSlog statement),会不一样。
Prefix/Postfix:
prefix的例子:
1 int a =5; 2 NSlog(@"The value of a is %i", ++a); 3 //首先a在内存中是5, ++a在NSlog之前做,a此时为6,执行NSlog
postfix的例子:
int a = 5; NSlog(@'The vlaue of a is %i", a++); //对于postfix:先excute NSlog语句,当语句执行完后才a+1
Ternary Operation:
相当于小型的if statement
ex:
int player1; int player2; ... ... //sometimes later int highScore; if(player1>player2){ highScore = player1; } else{ highScore = player2 }
用Ternary Operation可以缩减为一句话
condition ? true : false
int player1; int player2; int highScroe = (player1 player2) ? player1 : player2;
Loop:
while do:
1 int a = 0; 2 while(a<10){ 3 NSlog(@"The value of a is %i", a); a++; 4 }
do while:
1 int a = 0; 2 do{ 3 NSlog(@"The Value is %i", a); 4 a++; 5 } while(a<10)
一般对于while do语句我们常用的变体是for loop:
1 for(int i = 0; i<10; i++){ 2 NSlog(@"The vaule of a is %i", a); 3 }
Break/Continue:
1 for( int i = 1; i < 5000; i++){ 2 if(i % 5 ==0){ 3 continue; 4 } 5 NSlog(@"The vaule of the index is %i", i); 6 } //当其中那次循环是5的倍数则返回到Top 继续做判断
Break/Continue:
语法:
int myFuntion(int x, int y) void myFuntion(int x, int y) void myFuntion()
{ { {
.... .... ...
return x + y; //void不需要return关键字 //parameter可以为空
} } }
注:函数名用carmel case写法
ex: main主函数会自动被程序调用,其他函数需要先定义后自己调用
1 #import <Foundation/Foundation.h> 2 void myFunction(){ 3 for(int i =0; i<5000; i++){ 4 if(i % 5 ==0){ 5 continue; //jump back to the top loop -> i<5000 6 } 7 NSLog(@"The value of the index is %i", i); 8 } 9 } 10 11 int main(int argc, const char * argv[]) 12 { 13 14 @autoreleasepool { 15 16 // insert code here... 17 myFunction(); 18 } 19 return 0; 20 }
1 #import <Foundation/Foundation.h> 2 3 void myFunction(); 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 // insert code here... 10 myFunction(); 11 } 12 return 0; 13 } 14 void myFunction(){ 15 for(int i =0; i<5000; i++){ 16 if(i % 5 ==0){ 17 continue; //jump back to the top loop -> i<5000 18 } 19 NSLog(@"The value of the index is %i", i); 20 } 21 }
注意:myFunction()的定义需要在调用之前写,如果放在函数调用后面会出错。如果非要保持在后面,需要在上面加void function()
可以选择一块区域,右击选择extract,Xcode会为我们选择的语句自动extract成函数声明。