1.NSLog函数使用方法
/*
NSLog和printf 区别
1)NSLog会自动换行,printf不会自动换行
2)NSLog能打印时间等项目信息,printf不能打印信息
3)NSLog函数里的参数是NSString 对象,printf的参数是char类型的指针
*/
int main(int argc, const char * argv[]) {
@autoreleasepool {
// printf("Hello,咸鸭蛋\n");
// printf("Hello,咸鸭蛋\n");
// NSLog(@"Hello,咸鸭蛋");
// NSLog(@"Hello,咸鸭蛋");
//C基本类型
int a = 10;
float f = 22.2f;
double d = 33.33;
char ch = 'B';
char *str = "adds";
char *strCN = "教主";
//NSString %@格式输出,是OC中特有的字符串格式
//@"" 作用:将C语言字符串转化为OC字符串
NSString *str2 = @"小白";
NSString *str3 = @"adgds";
//1.OC可以向下兼容C,
//NSLog可以以%s的格式打印ch类型的纯英文字符串
// printf("%d, %.2f, %.2f, %c, %s\n", a, f, d, ch, str);
// NSLog(@"%d, %.2f, %.2f, %c, %s",a, f, d, ch, str);
//2.OC中NSLog函数不支持以%s的格式打印ch类型的纯中文字符串
// printf("strCN = %s\n", strCN);
// NSLog(@"strCN = %s", strCN);
//3.printf函数不能以%@的格式输出,NSString类型的中文字符串
// printf("str2 = %@\n", str2);
// NSLog(@"str2 = %@",str2);
//4.printf函数不能以%@的格式输出,NSString类型的字符串,无论是中文的还是英文的
printf("str3 = %@\n", str3);
NSLog(@"str3 = %@", str3);
}
2.OC中注释及@符号的使用
#import <Foundation/Foundation.h>
void test();
/**
* 这是test函数吗?
*/
void test(){
printf("这是test函数");
}
3.OC和C差异学习(一)
/*
1.源文件的差异
2.数据类型的差异
3.关键字差异
4.流程控制语句差异
for(int i = 0, i < 10, i++){
}
增强for循环,作用快速遍历数组,字典
类型 指针 数组/字典
for (type *object in collection) {
执行语句
}
in前类型和数组元素类型一致
5.函数定义和声明
定义C语言函数
int max(int x ,int y);
int max(int x ,int y){
return x > y ? x : y;
}
OC中不叫函数,叫方法
方法的声明
- (int)max:(int)x andY:(int)y;
方法的实现
- (int)max:(int)x andY:(int)y{
return x > y ? x : y;
}
- (void)test; //对象方法
+ (void)test; //类方法
方法名和变量名书写规范:
驼峰命名法
NSString *xiaoHaiName;
*/
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *xiaoName;
//OC数组,数组元素为字符串
NSArray *arr = @[@"小啦", @"小草", @"小瓶盖"];
for(NSString *str in arr){
NSLog(@"str = %@", str);
}
}
return 0;
4.OC和C差异学习(二)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int a = 1;
NSLog(@"qerzc");
@try {
int b = a / 0;
}
@catch (NSException *exception) {
}
@finally {
}
}
return 0;
}
5.类的声明和实现
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
int _speed;
NSString *_color;
int _wheelCount;
}
@end
@implementation Car
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"Hello, World!");
}
return 0;
}
6.创建一个对象并访问成员变量
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int _speed;
NSString *_color;
int _wheelCount;
}
@end
@implementation Car
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *c = [Car new];
c->_color = @"蓝色";
c->_speed = 120;
c->_wheelCount = 4;
NSLog(@"速度:%d, 颜色:%@, 轮子数:%d", c->_speed , c->_color, c->_wheelCount);
}
return 0;
}
7.无参方法声明实现及调用
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int _speed;
NSString *_color;
int _wheelCount;
}
- (void)run;
- (void)stop;
@end
@implementation Car
- (void)run{
NSLog(@"BMW跑在乡间的小道上,速度为:%d", _speed);
}
- (void)stop{
NSLog(@"咯吱,车停了");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *c = [Car new];
c->_speed = 120;
c->_color = @"蓝色";
c->_wheelCount = 4;
[c run];
[c stop];
}
return 0;
}
8.有参方法声明实现及调用
/*
1.C语言中的有参有返回值函数
int sum(int a, int b);
int sum(int a, int b){
return a + b;
}
2.OC中有参有返回值方法格式
1)带一个参数
方法声明
- (返回值类型)方法名:(参数类型)参数名;
方法实现
- (返回值类型)方法名:(参数类型)参数名{
}
2)带多个参数
方法声明
- (返回值类型)方法名1:(参数类型)参数名1 方法名2:(参数类型)参数名2;
- (返回值类型)方法名1:(参数类型)参数名1 and方法名2:(参数类型)参数名2;
- (返回值类型)方法名1:(参数类型)参数名1 :(参数类型)参数名2;
注意:方法名的命名没有硬性要求,可以加and 或者什么都不写只有一个冒号,同一个方法的声明和实现一定要方法名一致
方法实现
- (返回值类型)方法名1:(参数类型)参数名1 方法名2:(参数类型)参数名2{
}
3. OC中sum方法声明
- (int)sum:(int)a b:(int)b;
- (int)sum:(int)a andB:(int)b;
OC中方法的实现
- (int)sum:(int)a b:(int)b{
}
- (int)sum:(int)a andB:(int)b{
}
4.OC方法注意:
1)方法类型:- 开头是对象方法 ,+ 开头是类方法
2)返回值类型:int
3)方法名:sum: b:
sum: andB:
4)参数类型:int int
5)参数名:a b
*/
//练习:
//实现一个计算器类:Calculator
//属性:_num1 _num2
//行为:sum
//类的声明
@interface Calculator : NSObject
{
//定义实例变量
@public
int _num1;
int _num2;
}
//无参方法声明
- (int)sum; //加法
//有参方法的声明
- (int)sum:(int)a andB:(int)b;
@end
//类的实现
@implementation Calculator
//无参方法的实现
- (int)sum{
return _num1 + _num2;
}
//有参方法的实现
- (int)sum:(int)a andB:(int)b{
return a + b;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建对象
Calculator *cal = [Calculator new];
//给对象实例变量赋值
// cal->_num1 = 12;
// cal->_num2 = 4;
//调用无参方法
// int sum = [cal sum];
//调用有参方法
int sum = [cal sum:12 andB:6];
NSLog(@"sum = %d", sum);
}
return 0;
}
9.类的练习
#import <Foundation/Foundation.h>
typedef enum{
kColorBlack, kColorWhite, kColorGold
}Color;
@interface IPhone : NSObject
{
@public
Color _color;
float _size;
NSString *_cpu;
}
- (void)aboutMyPhone;
- (void)callPhone:(NSString *)telNum;
- (void)sendMessage:(NSString *)telNum andContent:(NSString *)content;
@end
@implementation IPhone
- (void)aboutMyPhone{
NSLog(@"颜色:%d, 大小:%.2f, cpu:%@", _color, _size, _cpu);
}
- (void)callPhone:(NSString *)telNum{
NSLog(@"给 %@ 打电话", telNum);
}
- (void)sendMessage:(NSString *)telNum andContent:(NSString *)content{
NSLog(@"给 %@ 打电话,电话内容是:%@", telNum , content);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
IPhone *ip = [IPhone new];
ip->_color = kColorGold;
ip->_size = 7.5f;
ip->_cpu = @"A30";
[ip aboutMyPhone];
[ip callPhone:@"10086"];
[ip sendMessage:@"10086" andContent:@"你好,请问联通客服电话是多少?"];
}
return 0;
}