博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios programming (3rd) notes
阅读量:6145 次
发布时间:2019-06-21

本文共 2156 字,大约阅读时间需要 7 分钟。

Ownership

  • When an object  has an instance variable that  points to another object , the object  with the pointer is said to own the object  being pointed to.
  • When a method(function) has a local variable that points to an object, that method is said to own the object being pointed to.

Recall that a collection object, like an NSMutableArray, holds pointers to objects instead of acutally containing them. These pointers convey ownership: an array owns the objects it points to.

Because objects own other objects, which can own other objects, the destruction of a single object can set off a chain reaction of loss of ownership, object destruction, and freeing up of memory.

Let's add some code so that we can see this destruction as it happens.

NSObject implements a dealloc method, which is sent to an object when it is about to be destroyed. We can override this method to print something to the console when an object is destoryed.

- (void) dealloc{    NSLog(@"Destroyed: %@", self);}

A variable can also be declared using the __unsafe_unretained attribute. Like a weak reference, an unsafe unretained reference does not take ownership of the object it points to. Unlike a weak reference, an unsafe unretained reference is not automatically set to nil when the object it points to is destroyed. This makes unsafe unretained variables, well, unsafe. __unsafe_unretained exists primarily for backwards compatibility. 

Be safe. Change this variable back to __weak.

__weak BNRItem * container;

property default attributes:

atomatic,readwrite,and assign

so we normally declare property like this

@property (nonatomic) BNRItem* pd;

a sample

//object @property (nonatomic, strong) BNRItem *containedItem;@property (nonatomic, weak) BNRItem *container;@property (nonatomic, strong) NSString *itemName;@property (nonatomic, strong) NSString *serialNumber;//primitive assign@property (nonatomic) int valueInDollars;@property (nonatomic, readonly, strong) NSDate *dateCreated;

copy

@property (nonatomic, copy) NSString *itemName;

denotes

- (void) setItemName:(NSString * ) str{    itemName = [str copy];}

 

 

 

 

转载于:https://www.cnblogs.com/grep/archive/2012/07/13/2589640.html

你可能感兴趣的文章
【算法笔记】多线程斐波那契数列
查看>>
java8函数式编程实例
查看>>
jqgrid滚动条宽度/列显示不全问题
查看>>
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
angularjs表达式中的HTML内容,如何不转义,直接表现为html元素
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
[Usaco2015 dec]Max Flow
查看>>
javascript性能优化
查看>>
多路归并排序之败者树
查看>>
java连接MySql数据库
查看>>
转:Vue keep-alive实践总结
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Hadoop2.5.0 搭建实录
查看>>
实验吧 recursive write up
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
Oracle表分区
查看>>