zoukankan      html  css  js  c++  java
  • ios的@property属性和@synthesize属性

           当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property和@synthesize属性,@property用在 .h 头文件中用作声明,@synthesize用在.m 文件中用于实现。

           如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,传统的写法是:

    Student.h

    //  Student.h
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    {
        int age;
        int no;
    }
    
    //age的getter和setter方法声明
    - (int)age;
    - (void)setAge:(int)newAge;
    
    //no的getter和setter方法声明
    - (int)no;
    - (void)setNo:(int)newNo;
    
    @end

    Student.m

    //
    //  Student.m
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    
    //age的getter和setter方法的实现
    - (int)age
    {
        return age;
    }
    -(void)setAge:(int)newAge
    {
        age = newAge;
    }
    
    //no的getter和setter方法的实现
    - (int)no
    {
        return no;
    }
    - (void)setNo:(int)newNo
    {
        no = newNo;
    }
    
    @end

    main.m

    //
    //  main.m
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Student.h" 
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            // insert code here...
            Student *stu = [[Student alloc] init];
            stu.age = 100;//这句相当于setter方法
            NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法
            
            [stu release];
            
        }
        return 0;
    }

    ************************************************************************************

    用@property和@synthesize的写法是:
    Student.h

    //
    //  Student.h
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    {
        int age;
        int no;
    }
    
    //当编译器遇到@property时,会自动展开成getter和setter的声明
    @property int age;
    @property int no;
    
    
    @end

    Student.m

    //
    //  Student.m
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    
    //@synthesize 会自动生成getter和setter的实现
    //@synthesize 默认会去访问age,no,height同名的变量,,
    //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,
    //因此Student.h 中的这几个变量也可以省略不写。
    @synthesize age,no;
    
    @end

    main.m

    //
    //  main.m
    //  property
    //
    //  Created by Rio.King on 13-8-25.
    //  Copyright (c) 2013年 Rio.King. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Student.h"
    
    int main(int argc, const char * argv[])
    {
        
        @autoreleasepool {
            
            // insert code here...
            Student *stu = [[Student alloc] init];
            stu.age = 100;
            NSLog(@"age is %i", stu.age);
            
            [stu release];
        }
        return 0;
    }

    几点说明:
    1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age的私有成员变量。

    2.视频教学中建议变量名用"_"前缀作为开头,但我看big Nerd 那本书里是不用的,个人也比较习惯 big Nerd 的那种写法,所以变量名就不加前缀了。

    语法简介:

    格式: 声明property的语法为:@property (参数1,参数2) 类型 名字; 如:
    C代码
    @property(nonatomic,retain) UIWindow *window;

    其中参数主要分为三类: 
    读写属性: (readwrite/readonly)
    setter语意:(assign/retain/copy)
    原子性: (atomicity/nonatomic) 

    各参数意义如下: 
    readwrite: 产生settergetter方法
    readonly: 只产生简单的getter,没有setter。
    assign: 默认类型,setter方法直接赋值,而不进行retain操作
    retain: setter方法对参数进行release旧值,再retain新值。
    copy: setter方法进行Copy操作,与retain一样
    nonatomic: 禁止多线程,变量保护,提高性能
    (详见:http://justcoding.iteye.com/blog/1444548

  • 相关阅读:
    HLG 1522 子序列的和【队列的应用】
    POJ 3273 Monthly Expense【二分】
    HDU 4004 The Frog's Games 【二分】
    POJ 2001 Shortest Prefixes【第一棵字典树】
    POJ 2823 Sliding Window【单调对列经典题目】
    HDU 1969 Pie 【二分】
    POJ 3125 Printer Queue【暴力模拟】
    POJ 3250 Bad Hair Day【单调栈】
    字典树【模板】
    验证码 Code
  • 原文地址:https://www.cnblogs.com/thinksasa/p/3441551.html
Copyright © 2011-2022 走看看