Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50641
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: iOS平台

2015-04-09 11:12:31

一.OC作用域

@public      全局可以访问

@protected   只能在当前类和子类的对象方法中访问

@private     只能在当前类的对象方法中才能直接访问
@package    同一个框架可以访问

@interface  @end之间声明的成员变量如果不做特别的说明,那么其默认是protected的。@implatation中声明的成员变量默认是private
用法如下

@interface ccc : NSObject{

    @private 

    int _age;

    int _no;



}


.property和synthesize 

当编译器遇到@propetry时,会自动展开成get,set方法的声明

.h: @property int age;  会展开 -(int)age;  -(void)setAge:(int)newage;的方法声明

synthesize 

.m: @synthesize age;会展开 -(int)age;  -(void)setAge:(int)newage;的具体实现语句

并且如果.h里没声明该变量,则会在.m里自动创建一个私有的该变量


@property 只在@interface里有效

@synthesize 只在@implementation里有效

@synthesize age = _age;//自动生成变量名字为_age,并且是私有的

编译器新特性,Xcode4.5以  写了@property,就不用写 @synthesize,会自动在.m文件里生成@synthesize age = _age;

@property @synthesize是编译器的特性,就是会自动展开成别的代码


.h里面写@property的话
 .m里不一定用些@synthesize,他俩没必要必须配对出现

如果已经手动实现了set或者get方法,@property @synthesize就会用手动实现的


@property (retain)Book *book; 

自动产生set方法中的内存管理代码,release旧值,retain新值  

基本数据类型不能写,因为内存管理对基本数据类型无效


@property (assign)int i; 相当于  

@property int i; 直接赋值,不考虑内存管理


@property 可填写多个参数 

@property (readonly,retain)Book *book;


readonly 只读  只生成get方法

默认的时readwrite


默认是atomic,提供多线程安全

你生成的get set方法可以有多个线程调用,给方法加锁,保证线程安全

nonatomic不考虑线程安全,禁止多线程,提高效率

iphone上几乎所有的的项目都是nonatomic,因为手机上内存吃紧


getter  指定get方法名

@property (nonatomic,getter = getbook)Book *book;

book的get方法名字变为 getbook


三.注释

#pragma mark -生命周期方法  //- 代表给方法分组

#pragma mark 构造方法


四.@class

在.h里用来替代#import
@class Book;告诉编译器Book是类,具体类里的方法不会知道

用import影响性能,效率低,而且如果import里面的东西变了,所有包换他的文件都要重新编译

如果出现互相包含的情况,也可以用
@class,如下代码

点击(此处)折叠或打开

  1. // B.h

  2. #import <Foundation/Foundation.h>
  3. @class A;

  4. @interface B : NSObject

  5. @property A *a;


  6. @end


点击(此处)折叠或打开

  1. // A.h

  2. #import <Foundation/Foundation.h>
  3. @class B;

  4. @interface A : NSObject

  5. @property B *b;


  6. @end

@class NSObject;

interface A : NSObject

这样写不会知道NSObject里面的方法,所以也继承不了他的方法


继承某个类,就要导入头文件

只是定义成员变量,属性的话用@class



五.自动释放池


自动释放池是OC里面的一种内存自动回收机制

一般可以将一些临时变量添加到自动释放池里,统一回收释放

当自动释放池销毁时,池里面的对象就会调用一次release方法


发送一条autorelease,就会把对象舔到最近的自动释放池中,

栈顶的释放池,栈是先进后出


//代表创建一个自动释放池

@autoreleasepool {

        


}

autorelease 不会改变计数器

下面是快速创建对象的方法

+(id)student{


    Student *stu = [[[Student allocinitautorelease];

    

    return stu;



}


+(id)studentWithAge:(int)no{

    

    Student *stu = [[[Student allocinitautorelease];

    

    stu.no = no;

    

    return stu;

    


}


自带的类不需要管理内存,类里面已经处理完了。
不要把大量循环操作里面创建对象的语句和大内存的变量放在自动释放池中间,这样会造成内存峰值上升

六.Category

可以动态的为某个类添加新方法,仅仅是方法

@interface Student (Stu)

代表Stu是Student的分类
分类只能扩展方法,这里面不允许写新的成员变量

使用与团队合作


代码如下:

点击(此处)折叠或打开

  1. // Student+Stu.h

  2. #import "Student.h"

  3. @interface Student (Stu)

  4. -(void)fun;


  5. @end

点击(此处)折叠或打开

  1. // Student+Stu.m

  2. #import "Student+Stu.h"

  3. @implementation Student (Stu)

  4. -(void)fun{

  5.     NSLog(@"fun方法");
  6. }


  7. @end


点击(此处)折叠或打开

  1. #import <Foundation/Foundation.h>
  2. #import "Student.h"
  3. #import "Student+Stu.h"

  4. int main(int argc, const char * argv[]) {
  5.     @autoreleasepool {
  6.         
  7.         Student *st = [[Student alloc] init];
  8.         
  9.         [st fun];
  10.         
  11.     }
  12.     return 0;

  13. }



可以给系统自带的一些类,添加一些方法。代码如下:



点击(此处)折叠或打开

  1. @implementation NSString (ST)

  2. +(void)ss{

  3.     NSLog(@"ss");

  4. }


  5. @end

@interface @implementation 在一个文件中可以重复出现,他只是起到声明和实现的作用,如下

点击(此处)折叠或打开

  1. @interface Student : NSObject

  2. -(void)fun;

  3. @end

  4. @interface Student (abc)

  5. -(void)fun1;


  6. @end


点击(此处)折叠或打开

  1. @implementation Student

  2. -(void)fun{

  3.     NSLog(@"fun");
  4. }

  5. @end

  6. @implementation Student(abc)

  7. -(void)fun1{
  8.     
  9.     NSLog(@"fun1");
  10. }


  11. @end



阅读(488) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~