2015-12-04 // OC_9练习

2015-12-04 19:06:49

//
// main.m
// OC_9练习
//
// Created by YIem on 15/12/4.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

import "Person.h"

import "ARCMOdel.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {
    

// 创建对象

    Person *p1 = [[Person alloc] initWithName:@"qwe" arr:@[@"asd"] age:10];
    [p1 release];
    // 遍历构造器
    Person *p2 = [Person personWithName:@"zxc" arr:@[@"rty"] age:20];
    // ARCModel
    ARCMOdel *m = [[ARCMOdel alloc] init];
    [m release];
    
   
    
    
    
    
}
return 0;

}



//
// ARCMOdel.h
// OC_9练习
//
// Created by YIem on 15/12/4.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

// ARC/MRC 混编
// ARC下使用MRC: -fno-objc-arc
// MRC下使用ARC: -fobjc-arc

@interface ARCMOdel : NSObject

@property (nonatomic, copy)
NSString *string;
@property (nonatomic, retain)
NSDictionary *dic;

@end


//
// ARCMOdel.m
// OC_9练习
//
// Created by YIem on 15/12/4.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "ARCMOdel.h"

@implementation ARCMOdel

pragma mark - ARCsetter方法重写

  • (void)setString:(NSString *)string
    {
    if (_string != string) {

    _string = string;

    }
    }

  • (void)setDic:(NSDictionary *)dic
    {
    if (_dic != dic) {

    _dic = dic;

    }
    }

@end



//
// Person.h
// OC_9练习
//
// Created by YIem on 15/12/4.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, copy)
NSString *name;
@property (nonatomic, retain)
NSArray *arr;
@property (nonatomic, assign)
NSInteger age;
/// 初始化

  • (instancetype)initWithName:(NSString *)name

                         arr:(NSArray *)arr
                         age:(NSInteger)age;

    ///遍历构造器

  • (instancetype)personWithName:(NSString *)name

                           arr:(NSArray *)arr
                           age:(NSInteger)age;

    @end


//
// Person.m
// OC_9练习
//
// Created by YIem on 15/12/4.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Person.h"

@implementation Person

pragma mark - dealloc方法

  • (void)dealloc
    {

    // retain/copy的属性 必须写在dealloc中 让对应的成员变量release
    [_name release];
    [_arr release];
    [super dealloc];

    }

pragma mark - 初始化

  • (instancetype)initWithName:(NSString )name arr:(NSArray )arr age:(NSInteger)age
    {
    // 使用setter方法赋值 把要复制的内容引用计数 +1 防止提前释放
    self = [super init];
    if (self) {

    self.name = name;
    self.arr = arr;
    self.age = age;

    }
    return self;
    }

pragma mark - 遍历构造器

  • (instancetype)personWithName:(NSString )name arr:(NSArray )arr age:(NSInteger)age
    {
    Person *p = [[Person alloc] initWithName:name arr:arr age:age];
    // 遍历构造器中 使用autorelease平衡alloc
    return [p autorelease];
    }

pragma mark - 重写setter

  • (void)setName:(NSString *)name
    {
    // 1.判断成员变量中的内容和外部传人的内容是否是同一个
    if (_name != name) {

    // 2.release成员变量中的原数据
    [_name release];
    // 3.赋值新数据的同时 retain/copy新数据
    _name = [name copy];

    }
    }

  • (void)setArr:(NSArray *)arr
    {
    if (_arr != arr) {

    [_arr release];
    _arr = [arr retain];

    }
    }

  • (void)setAge:(NSInteger)age
    {
    // 非对象直接赋值
    _age = age;
    }

@end

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »