最近学习OC,所以在博客写下自己的笔记.
OC的基本:
1 id 相当于java的Object 代表任何对象. 所以id是关键字,不能用作变量!!
2 oc中使用"."符号是调用 int a =object.a 是调用的是 [object getA];
object.a=3; object.a是调用object.setA方法!!
3 @property :
Objective-C语言关键词,与@synthesize配对使用。xcode4.5以及以后的版本,@synthesize可以省略.
功能:让编译器自动编写一个与数据成员同名的方法声明来省去读写方法的声明。要在头文件声明变量后,在声明@property,再回到.m文件使用注解 @synthesize . 假如在oc里面.m中有@synthesize a , 但在.h头文件中没有这个属性 和没有@property a,它会自动生成的.还有关于@property标准写法再下面贴出的例子有.
4 关于oc 的类的初始话 . oc的构造器方法是 init , 而不像java用类名的名字作为成员方法.假如我的类中有一个ID的属性,初始化它,并赋值给ID ,如:
-(id)initWithID:(int)newID{
//判断self是否为空
if(self = [super init]){
_ID = newID;
return self;
}
//返回null
return nil;
}
由上面看出,oc没有this 关键字.
4 oc的内存管理:(简单的入门)
1 方法 alloc 申请内存 ,假如要初始化一个类要怎么调用
Student *stu = [[Student alloc] init];
先申请内存,然后调用初始化方法.
2 方法 retainCount 计数器,当计数器为0时,说明这个类已经释放掉了
3 release 释放内存 ,当retainCount为0时释放内存
4 retain 使计数+1 .
以上4个方法的综合例子 例子:
Book.h:
//
// Book.h
// clazz4_4_6
//
// Created by qiang on 13-4-12.
// Copyright (c) 2013年 qiang. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject{
int _ID;
Book *_book;
}
@property int ID;
@property Book *book;
-(void)print;
-(void)dealloc;
-(void)initWithID:(int)newID;
@end
Book.m
//
// Book.m
// clazz4_4_6
//
// Created by qiang on 13-4-12.
// Copyright (c) 2013年 qiang. All rights reserved.
//
#import "Book.h"
@implementation Book
@synthesize ID=_ID;
@synthesize book=_book;
-(void)print{
}
-(id)initWithID:(int)newID{
if(self = [super init]){
_ID = newID;
return self;
}
return nil;
}
@end
Student.h:
//
// Student.h
// class3_4_2
//
// Created by qiang on 13-4-2.
// Copyright (c) 2013年 qiang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Book.h"
@interface Student : NSObject {
Book *_book;
int _ID;
}
@property int ID;
@property Book *book;
@end
student.m
//
// Student.m
// class3_4_2
//
// Created by qiang on 13-4-2.
// Copyright (c) 2013年 qiang. All rights reserved.
//
#import "Student.h"
@implementation Student
//在4.2以下必须要写
@synthesize ID=_ID;
@synthesize book = _book;
-(void)setBook:(Book *)book{
//返回book 计数器+1
_book = [book retain];
}
//dealloc是当retainCount等于0的时候自己调用的
-(void)dealloc{
//当你存在成员对象的时候,必须要调用所有成员对象变量的release
[_book release];
[super release];
NSLog(@"Student %i dealooc",_ID);
}
@end
main:
//
// main.m
// clazz4_4_6
//
// Created by qiang on 13-4-10.
// Copyright (c) 2013年 qiang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *stu = [[Student alloc] init];
Book *book = [[Book alloc]init]; //计数器 1
NSLog(@"Book after alloc:%zd",[book retainCount]);
stu.book = book;//通过retain 计数器为2
NSLog(@"Book after setBook:%zd",[book retainCount]);
[book release]; //release 计数器为1 如果计数器为0 book则个就不能用
NSLog(@"Book after release:%zd",[book retainCount]);
//这时是book是stu创建的 当stu销毁时,
[stu release]; //当studeng
}
return 0;
}
运行结果:
2013-04-13 00:31:55.782 clazz4_4_6[3716:303] Book after alloc:1
2013-04-13 00:31:55.784 clazz4_4_6[3716:303] Book after setBook:2
2013-04-13 00:31:55.785 clazz4_4_6[3716:303] Book after release:1
2013-04-13 00:31:55.785 clazz4_4_6[3716:303] Student 0 dealooc