-
Notifications
You must be signed in to change notification settings - Fork 4
Description
数据类型
-
与js不同,oc中除了基本数据类型,还包括
指针数据类型。指针数据类型存放内存地址信息,指向内存的相应位置。
-
js通常定义变量只需要用var来定义,不需要指定具体数据类型,常用的方式是:
var a=0;
var b="str";而oc中在定义变量时,需要指定数据类型(常用的包括int、double、float、char、id等),比如:
int a=0; // 整型
float b=3.14f; // 浮点型
id c; // 指针类型另外,通过在这些数据类型前面添加限定词:long、long long、short、unsigned、signed 还可以改变数据类型的值域。
oc中类的声明与实现
// 类的声明,一般声明在.h文件中
@interface DemoClass:NSObject
{
//实例变量声明
int a;
}
- (id)initWithA:(int) _a;
@end
// 类的实现,一般声明在.m文件中
@implementation DemoClass
- (id) initWithA:(int) _a{
[super init];
}
@end
// 类的初始化
DemoClass *demo=[[DemoClass alloc] init];// alloc和初始化方法一般不可或缺,alloc先分配内存再初始化相当于js的new
oc中的方法声明与调用
oc的方法主要有两种,一种是类方法,还有一种是实例方法。类方法是不需要实例化即可被调用的方法,实例方法是需要实例化才能使用的方法,在js中也有相似的例子
function Class=function(){};
Class.classMethod=function(){}; // 类方法声明
Class.prototype.method=function();//示例方法声明
Class.classMethod(); // 类方法调用
(new Class()).method(); // 示例方法调用oc中的类方法和示例方法的声明和实现
@interface DemoClass:NSObject
// - 实例方法声明,
- (void) method: (int) argument; //void 为类的返回值类型,int为参数类型,该方法名为method:
// 类方法声明
+ (void) method2: (int) argument;
@end
DemoClass *demo=[[DemoClass alloc] init];
[demo method:0];// 调用实例方法
[DemoClass method2:0];// 调用类方法
类属性定义、获取和修改
oc中的类同样拥有属性,使用@property和@synthesize可以让编译器自动生成设置器和访问器。
@interface DemoClass:NSObject{
int argument;//声明属性
}
@property (nonatomic) int argument;//让编译器自动生成设置器和访问器
// nonatomic为属性类型,还有readonly、readwrite、assign、retain、copy、atomic
@end
@implementation DemoClass
@synthesize argument;//让编译器自动生成设置器和访问器
@end
// 属性设置器和访问器
DemoClass *demo=[[DemoClass alloc] init];
demo.argument=10;//设置器
NSLog(@"argument is :%d",demo.argument);//访问器
类目与协议
类目是对现有类的扩展,不需要继承,而且会成为原始类的一部分,可以像其他实例方法一样调用,而且这些类目方法一样可以被继承。
不过类目不能添加新的实例变量,而且如果类目覆盖原始类的现有方法,则会导致super消息的断裂,所以一般不建议覆盖现有方法。
类目就好像js中为Array扩展原型方法。
类目的定义:
@interface DemoClass (Cate) // 如果不带Cate ,则为匿名类目,也叫延展,相当于私有方法
- (void)method;
@end
// 实现
@implementation DemoClass (Cate)
- (void)method
{
// TODO
}
@end协议就好比接口,用于规定实现了该接口的类的接口规范(比如什么方法必须有,什么方法可选)
协议的定义
@protocol DemoProtocol <NSObject>
@required
- (void)method1;
@optional
- (void)method2;
@end
// 其他类对`DemoProtocol`协议的实现
@interface DemoClass : NSObject <DemoProtocol>
@end
//必须对method1进行实现
@implementation DemoClass
- (void)method1
{
// TODO
}
@endIOS生命周期
applicationWillResignActive(程序即将失去焦点)->
applicationDidEnterBackground(程序即将进入后台)->
applicationWillEnterForeground(程序即将进入前台)->
applicationDidBecomeActive(程序获得了焦点)->
applicationWillTerminate(程序即将退出)
实例练习
一个高富帅买了一辆中介介绍的30万的lexus。
demo已经上传到git:https://github.com/QQVIPTeam/practice-of-ios/tree/master/Class1/demo/Class1Demo1
第一章作业
做一个100秒倒计时的程序,注意考虑,程序进入后台的情况,大家按需加入其他功能。
完成后的作业已经上传到git:https://github.com/QQVIPTeam/practice-of-ios/tree/master/Class1/demo/timer
第一章作业二
做一个 App 状态的记录器,记录上面讲到的 iOS 生命周期(哪个时间进行了什么操作,如下示意),并显示到手机屏幕中。
13:23:01 --> applicationWillResignActive
13:41:22 --> applicationDidEnterBackground
13:43:51 --> applicationWillEnterForeground
13:50:11 --> applicationDidBecomeActive
13:55:21 --> applicationWillTerminate作业示例已上传到这里:https://github.com/QQVIPTeam/practice-of-ios/tree/master/Class1/work/record