0%

数据类型和常量

int类型

有效数字:0,-100,158
无效数字:15,000

float和double类型

有效数字:3.,125.8,-.001,1.7e4(科学记数法)
NSLog打印:%f或%e或%g

char类型

有效字符:’a’,’;’,’0’,’\n’
NSLog打印:%c

限定符

long:扩展数字范围;NSLog打印long int用%li
long long
short:缩小数字范围
unsigned:无符号(正数)
signed:有符号

id类型

泛型,可以存储object的任何类型
NSLog打印:%p

算术表达式

运算符优先级

与其他语言一致:括号和一元减运算符 > 乘除 > 加减

整数算术

整数运算也只能获得整数,例:
int a = 25;
int b = 2;
a / b = 12;

模运算符

取余运算

整数和浮点转换

浮点 -> 整型 :去尾法,如12.5 - > 12
整型 -> 浮点:直接加点和若干个0
整数算术有小数:去尾法,如:25 / 2 = 12

类型转换运算符

可用于数字类型转换,如:
(float) 15 = 15.000000
(int) 29.55 = 29

可用于强制将id类型转为特定类型
id myNumber;
Fraction *myFraction;
myFraction = (Fraction *) myNumber;

赋值运算符

=可与+,-,*,/,%组合

文件扩展名

扩展 含义
.c C语言源文件
.cc, .cpp C++语言源文件
.h 头文件
.m Objective-C源文件
.mm Objective-C++源文件
.pl Perl源文件
.o Object (已编译的) 文件

基础实例

导入包/库

导入Foundation目录下的Foundation.h文件
import <Foundation/Foundation.h>

main函数

int main (int argc, const char *argv[]) {
@autoreleasepool {}
return 0;
}

打印

打印常量
打印 NSString对象 @”Hello, World!”
NSLog(@"Hello, World!");

打印变量
int sum;
sum = 50 +25;
NSLog(@'The sum of 50 and 25 is %i', sum);

类, 对象, 方法

对象:类的唯一表示
实例:类的具体表示
方法:在实例上执行的操作

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Program to work with fractions – class version
#import <Foundation/Foundation.h>

//---- @interface section ----
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

//---- @implementation section ----
@implementation Fraction {
int numerator;
int denominator;

}

-(void) print {
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}
@end

//---- program section ----
int main (int argc, char * argv[]) {

@autoreleasepool {
Fraction *myFraction;
// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
}

return 0;

}

类和方法的申明和调用

⚠️ OC是大小写敏感的

1、@interface section(预申明类和方法)

@interface section 描述类和方法
一般形式:

1
2
3
@interface NewClassName: ParentClassName
propertyAndMethodDeclrations
@end

示例代码:

1
2
3
4
5
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

第一行:申明了一个类Fraction,其父类为NSObject,这个父类在NSObject.h中被定义,此文件被包含在Foundation.h中
第二行: - 表示申明了一个实例方法(对类的实例执行的方法),+ 表示申明了一个类方法(对类本身执行的方法)
第三行: (void) 表示返回值为空
第四行: 申明了一个返回值为空,名叫setDenominator的实例方法,它的参数是d,类型为int
第五行:以@end结尾

2、@implementation section (具体申明类和方法)

@implementation section 描述数据和实施interface申明类和方法的实际代码
一般形式:

1
2
3
4
5
@implementation NewClassName {
memberDeclarations;
}
methodDefinitions;
@end

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@implementation Fraction {
int numerator;
int denominator;

}

-(void) print {
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}
@end

第一行:类后面的父类可跟,可不跟
第二、三行:申明实例变量(实例化一个类时被创建,和实例对应)
扩展的函数:在{}申明函数的具体内容
最后以@end结尾

3、program section 包含执行的程序代码 (实例化类并调用方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main (int argc, char * argv[]) {   

@autoreleasepool {
Fraction *myFraction;

// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
}

return 0;

}

实例化并初始化一个对象方法一

1、申明一个指针(即存储一个地址)myFraction,指针指向的地址,用于储存Fraction类的值
Fraction *myFraction;

2、发送消息给Fraction类,分配一个内存空间(将所有实例变量都置零),这里已经实例化完成myFraction
myFraction = [Fraction alloc];

3、发送消息给myFraction,初始化
myFraction = [myFraction init];

实例化并初始化一个对象方法二

合并以上代码
Fraction *myFraction = [[Fraction alloc] init];

实例化并初始化一个对象方法三

合并alloc和init
Fraction *myFraction = [Fraction new];

调用类和方法

一般形式:
[ ClassOrInstance method ];
如: [ yourCar new ];

[ ClassOrInstance method: argument ];
如: [ yourCar setSpeed: 55 ];

示例代码:

1
2
3
[myFraction setNumerator: 1];     
[myFraction setDenominator: 3];
[myFraction print];

第一行:向myFraction发送消息setNumerator,参数为1;然后向setNumerator发送消息,参数为1;之后会调用Fraction类的setNumerator方法,此时1存在变量n中,最后将此值存到实例变量numerator中

基本的访问器方法(setter和getter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@interface Fraction: NSObject
- (void) setNumerator: (int) n;
- (int) numerator;
@end

@implementation Fraction {
int numerator;
}

- (void) setNumerator: (int) n {
numerator = n;
}

- (int) numerator {
return numerator;
}

@end

安装node

一定要选择LTS长期支持的稳定版

安装npm

Hexo的
由于默认的npm镜像在国外,速度感人,甚至会终端长时间无响应,因此需要更换npm镜像源

一般使用淘宝镜像,办法有两种

1、创建一个全局新镜像命令

npm install -g cnpm –registry=https://registry.npm.taobao.org
⚠️ 这里有一个坑,在之后Hexo init时,依然使用了原npm,依然会导致安装缓慢

2、替换原npm镜像配置

npm config set registry https://registry.npm.taobao.org
推荐使用此方法

Hexo init

如果遇到初始化失败,需要删除整个Blog,重新init

欢迎来到 Hexo! 这是我的第一篇博客.
Hexo文档 documentation .
Hexo 问题反馈社区 troubleshooting
Hexo github 问题反馈 GitHub.

快速入门

创建一篇新博客

1
$ hexo new "My New Post"

更多信息: Writing

本地运行服务器

1
$ hexo server

更多信息: Server

生成静态文档

1
$ hexo generate

更多信息: Generating

部署到远程站点

1
$ hexo deploy

更多信息: Deployment

Hexo和Next超实用视频教程

在这里要感谢搭建CodeSheep和Mackxin的视频教程,帮助我搭建Hexo和使用Next主题
搭建Hexo: CodeSheep教程
使用Next主题: Mackxin教程