第一章:程序是一系列指令
1.1:程序命名规则
最重要的一条规则是你不能使用Objective-C语言中的保留字(也就是在Objective-C语言中已经有了
特殊含义的单词)。使用简明的单词组成变量名,比如pictureWidth,通常是安全的。为了保证变
量名的可读性,推荐在其中使用大写字母,这条规则可以让你程序减少错误。
下一条规则是一个变量名不能以数字开头,但数字可以出现在变量名中。另外,使用下划线
“_”也是可以的。
1.2:支持的运算符
+ 加法运算
- 减法运算
/ 除法运算
* 乘法运算
1.3:支持的数据类型
一些常见的余数运算结果:
21 % 7 = 0 30 % 2 = 0 50 % 9 = 5
22 % 7 = 1 31 % 2 = 1 60 % 29 = 2
23 % 7 = 2 32 % 2 = 0
24 % 7 = 3 33 % 2 = 1
27 % 7 = 6 34 % 2 = 0
这些数字可以放在手边备用,但注意,这个运算符只对整数有效。
1.4:程序运行
第二章:注释
记得以双斜杠开头。
// This is a comment
在Xcode中,注释以绿色显示。如果注释很长或者分行,还可以用这个符号把注释括起来:/* */。
/* This is a comment
extending over two
lines
*/
第三章:函数
是一个函数(function),函数有自己的名称,你可以通过函数名找到并运行这个函数。
函数是一个十分重要的概念,一段程序中至少包含一个被称为主函数(main( ) function)的函数。
当开始运行程序时,主函数的任务是告诉编译器从哪里开始执行程序。
一个简单函数的定义
float circleArea(float theRadius)
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
这个函数返回的数值(也就是变量theArea的值)是一个单
精度数
并不是所有的函数都要求参数。即使没有参数也要保留一个空的小括号。
int throwDice()
{
int noOfEyes;
// Code to generate a random value from 1 to 6
return noOfEyes;
}
void beepXTimes(int x);
{
// Code to beep x times.
return;
}
float pictureSurfaceArea(float theWidth, float theHeight)
{
// Code here
}
主函数的定义形式:
int main()
{
return 0;
}
main( )函数也要返回一个整数值,所以它也需要“return”语句。它以返回0表示行数运行正常
函数声明(function declaration)语句
调用自定义函数,要在main函数之前进行函数声明
方式为:
float circleArea(float theRadius);
调用函数需要知道因素:
写道
函数中的代码只在函数中有效。这是Objective-C的一个重要特点。
第四章:在屏幕上输出
例子:
int main(int argc, const char * argv[])
{
NSLog(@"julisdfdlsjfj");
return 0;
}
字符串定义:
除了字符串本身,NSLog( )函数还会附加显示其它信息,比如当时的日期、时间和应用程序名称等
NSLog(@""); //叫做空字符串,也就是说字符串长度为零
NSLog(@" ");
//不是空字符串,尽管看起来它里面没有内容,实际上它里面包含有一个空格,所以这个字符串的长度为1
字符串转义:
带参数输出:
NSLog(@"The value of the integer is %d.", integerToDisplay);
在小括号里有一个字符串和一个变量名,中间用逗号“,”则隔开。字符串中包含一些有趣的信息:“%d”。类似反斜杠,百分号“%”也有特殊含义。如果后面跟一个“d”(十进制数的简写),执行程序以后在“%d”的位置会插入逗号后面的数值
要显示一个单精度数则用“%f”代替 “%d”。
多个参数输出:
NSLog(@"The integer value is %f, whereas the float value is %f.", x, pi);
使用正确的符号对应正确的变量类型十分重要。如果你把第一个弄错了,那么第二个值也不会正确显示!
如何让程序知道NSLog( )函数
必须告诉编译器去引入一个包含NSLog( )函数功能的底层库文件。其形式如下:
#import <Foundation/Foundation.h>
这个语句行必须是程序的首行。
完整的程序代码
#import <Foundation/Foundation.h>
float circleArea(float theRadius)
float rectangleArea(float width, float height);
int main(int argc, const char * argv[])
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = rectangleArea(pictureWidth, pictureHeight);
circleSurfaceArea = circleArea(circleRadius);
NSLog(@"Area of circle: %10.2f.", circleSurfaceArea);
NSLog(@"Area of picture: %f. ", pictureSurfaceArea);
return 0;
}
float circleArea(float theRadius) // first custom function
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
float rectangleArea(float width, float height)
{
return width*height;
}
第五章:编译和运行一个程序
第六章:条件语句
1:if
int age = 42;
if (age > 30)
{
NSLog(@"age is older than thirty.");
}
NSLog(@"Finished.");
2:if ...else...
int age = 42;
if (age > 30)
{
NSLog(@"age is older than thirty.");
}
Else
{
NSLog(@"age is not older thirty.");
}
NSLog(@"Finished.");
比较符号:
注意:
“等于”用两个等号“= =”表示。
“=”是赋值符号,它会把一个特定值赋给变量