zoukankan      html  css  js  c++  java
  • 黑马程序员--Objective-C之--@property和@synthesize关键字

     

    ------IOS培训期待与您交流! -------

     

    property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。

    一、@property关键字

    1、property的语法格式:

    @property (参数1,参数2类型名字;

    这里的参数,主要有以下三种:

    (1)setter/getter方法(assign/retain/copy)

    (2)读写属性(readwrite/readonly)

    (3)atomicity(nonatomic)

    2、三种方式的使用

    (1)assign/retain/copy  代表赋值的方式

    (2)readonly关键字代表setter不会被生成, 所以它不可以和 copy/retain/assign组合使用。

    (3)atomicity的默认值是atomic,读取函数为原子操作。

    copy/reain/assign 在其中选择一个来确定属性的setter如何处理这个属性。NSObject对象采用这个中方式。

    一些特别的Object比如NSSstring使用copy

    assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。

    3、如何使用@property 

     

    /*没有使用@property的情况定义成员变量*/

    #import
    <UIKit/UIKit.h> @interface ViewController : UIViewController { NSObject *_obj; }
    - (void)viewDidLoad;
    @end 
    @implementation ViewController
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];      
        self.obj = nil;、  
    } 
    @end 

     

    此时编译器会报错。

     
    /*使用@property的情况定义成员变量*/

    #import
    <UIKit/UIKit.h> @interface ViewController : UIViewController { NSObject *_obj; } @property (nonatomic,retain) NSObject *obj; @end

    编译能通过,运行,崩溃,提示错误 reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480

    那就是我们没事实现setter方法。

    用@synthesize关键字实现getter 和setter。

        @implementation ViewController  
        @synthesize obj;  
        - (void)viewDidLoad  
        {  
            [super viewDidLoad];  
            self.obj = nil;  
        }  

    运行,程序运行正常。说明setter 起作用了。

    4、@property和@synthesize关键字 生成的代码

     
    #import <UIKit/UIKit.h>  
      
    @interface ViewController : UIViewController  
    {  
        NSObject *obj;  
    }  
    //@property (nonatomic,retain) NSObject *obj;  
    -(NSObject*)obj;  
    -(void)setObj:(NSObject*)newObj; @end
        @implementation ViewController  
        //@synthesize obj;  
        - (void)viewDidLoad  
        {  
            [super viewDidLoad];  
            self.obj = nil;  
        }  
          
        -(NSObject*)obj{  
            return obj;  
        }  
        -(void)setObj:(NSObject*)newObj{  
            if(obj != newObj){  
                [obj release];  
                obj = [newObj retain];  
            }  
        }  

    再运行,也能正常启动。说明自己写的getter 和setter替代了property。

    5、使用三种参数的对比

    @property (nonatomic,retain)NSObject *obj;
    
    @property (nonatomic,retain,readwrite) NSObject *obj;

     

    readwrite是默认行为,所以这两行代码等价

     

    @property (retain) NSObject *obj;
    
    @property (atomic,retain) NSObject *obj;

    atomic是默认行为,所以这两行代码是等价的。

     

    @property(atomic,assign)int number;        
    
    @property(atomic) int number;        
    
    @property int number;

     

    对int 来说,atomic assign都是默认行为,所以这三行是等价的。

     

    @property  NSObject *obj;

    这样写行吗?不行的,报警告

     


     

    只有int 等基础数据类型能这么写。对象必须加上赋值的类型。

     

    @property  (retain) NSObject *obj;

    这样就没问题了。何时使用assign、何时使用retain、copy后面再讲。

    二、@synthesize关键字

    //
    //  Person.m
    //  25_Property
    //
    //  Created by jiangwei on 14-10-12.
    //  Copyright (c) 2014年 jiangwei. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "User.h"
    
    //有时候我们不想定义属性为_开头的
    //这时候我们就可以使用@synthesize,来修改我们想要的属性名
    
    //这时候属性_userName变成了$userName
    
    @implementation User
    @synthesize userName = $userName;
    
    @end

    因为我们使用@property定义属性之后,如果我们想修改这个属性的名称,就可以使用@synthesize关键字来对属性名称进行修改

     

    @synthesize userName = $userName;
  • 相关阅读:
    最长公共子序列算法问题代码(使用JavaScript实现)
    硬币找零问题算法几种不同的代码实现方式(使用Python实现)
    基数排序(使用Python实现)
    桶排序(使用Python实现)
    减法要用 signed 型
    16系列和18系列的不同
    Proteus中常用元器件名字
    数值类型
    PIC单片机之时钟设置
    MOS管使PIC单片机不能正常运行
  • 原文地址:https://www.cnblogs.com/waterfox/p/4376848.html
Copyright © 2011-2022 走看看