IOS之学习笔记五(合成存取方法)

一、主要属性介绍

1、自动合成setter、getter方法

1)、接口部分@property指定属性  2)、实现部分@synthesize

如果

@syntheszie  widows = _windows

这里成员变量名是_windows,而不是windows


2、atomic(nonatomic)

这里主要是指存取方法为原子操作,实现线程安全,atomic是默认,保证线程安全,但是会导致性能降低,单线程我们一般考虑nonatomic


3、copy

用这个修饰了属性名,把副本值设置给类的属性,如果赋值的副本发生改变,但是类部的属性值不会改变


4、getter、setter

如果(getter = ff1, setter = ff2),会把默认的getter方法改为ff1, 会把默认setter方法改为ff2,我们调用的时候就是[对象 ff1]、[对象 ff2]


5、readonly、readwirte

readonly是指系统指合成getter方法,不合成setter方法

readwirte是默认的,都合成


6、retain

使用retain指示定义属性时,当莫个对象赋值给属性时,该属性原来所引用的对象引用计数减1,被赋值对象的引用计数加1

当一个对象的引用计数大于1时,该对象不该被回收。


7、strong、weak

strong:指被赋值对象持有强引用,不会自动回收

weak:使用弱引用指向被赋值对象,该对象可能被回收


 
二、测试demo

User.h

    #ifndef User_h
    #define User_h
    #import <Foundation/Foundation.h>
    @interface User : NSObject
    @property (nonatomic) NSString *name;
    @property (nonatomic) NSString *city;
    @property (nonatomic, copy) NSString *add;
    @property NSString *pass;
    @property NSDate *birth;
    @property NSDate *birth1;
    @end
    #endif /* User_h */

 User.m

    #import <Foundation/Foundation.h>
    #import "User.h"
     
    @implementation User
    @synthesize name = _name;
    @synthesize pass;
    @synthesize  birth;
    -(void) setName:(NSString *)name
    {
        self->_name = [NSString stringWithFormat:@"hello%@", name];
    }
    @end

main.m文件

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import "Person.h"
    #import "Apple.h"
    #import "User.h"
    #import "Args.h"
    #import "KVCPerson.h"
     
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            
            
            User *user = [User new];
            NSMutableString *name = [NSMutableString stringWithString:@"chencaifeng"];
            NSMutableString *city = [NSMutableString  stringWithString:@"hunan"];
            NSMutableString *addr = [NSMutableString stringWithString:@"luyunlu"];
            [user setName:name];
            [user setCity:city];
            [user setAdd:addr];
            [user setPass:@"hello"];
            [user setBirth:[NSDate date]];
            NSLog(@"name is %@, and pass is %@, birth is%@, city is%@, add is %@", [user name], [user pass], [user birth], [user city], [user add]);
            //我们把setName函数重写了,虽然name后面追加了字符串,但是后面打印值没有改变
            [name appendString:@"chenyu"];
            //由于这里属性没有加copy,city后面追加了字符串,所以后面打印也变了
            [city appendString:@"changsha"];
            //由于这里属性加了copy,由于这个addr后面值追加了,所以后面打印不会改变
            [addr appendString:@"kanyunlu"];
            NSLog(@"name is %@, and pass is %@, birth is%@, city is%@, add is %@", [user name], [user pass], [user birth], [user city], [user add]);
            
            //这里是用.操作
            user.add = @"hello";
            NSLog(@"user add is %@", user.add);
        }
    }


三、运行结果

    name is hellochencaifeng, and pass is hello, birth isFri Jul  6 19:51:04 2018, city ishunan, add is luyunlu
    name is hellochencaifeng, and pass is hello, birth isFri Jul  6 19:51:04 2018, city ishunanchangsha, add is luyunlu
    user add is hello

 




 


作者:chen.yu
深信服三年半工作经验,目前就职游戏厂商,希望能和大家交流和学习,
微信公众号:编程入门到秃头 或扫描下面二维码
零基础入门进阶人工智能(链接)