-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign-retain-strong-weak-and-copy.html
189 lines (180 loc) · 10.8 KB
/
assign-retain-strong-weak-and-copy.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Jinhuan Li" />
<meta name="copyright" content="Jinhuan Li" />
<meta name="keywords" content="iOS, interview, iOS, retain, assign, strong, weak, copy" />
<title>IOS tips #2: Assign, Retain, Strong, Weak, and Copy · Likers
</title>
<link href="http://cdn-images.mailchimp.com/embedcode/slim-081711.css" rel="stylesheet" type="text/css">
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="http://likers.github.io/theme/css/style.css" media="screen">
<link rel="stylesheet" type="text/css" href="http://likers.github.io/theme/css/solarizedlight.css" media="screen">
<link rel="shortcut icon" href="http://likers.github.io/theme/images/favicon.ico" type="image/x-icon" />
<link rel="apple-touch-icon" href="http://likers.github.io/theme/images/apple-touch-icon.png" />
<link rel="apple-touch-icon" sizes="57x57" href="http://likers.github.io/theme/images/apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon" sizes="72x72" href="http://likers.github.io/theme/images/apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon" sizes="114x114" href="http://likers.github.io/theme/images/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon" sizes="144x144" href="http://likers.github.io/theme/images/apple-touch-icon-144x144.png" />
<link rel="icon" href="http://likers.github.io/theme/images/apple-touch-icon-144x144.png" />
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://likers.github.io/"><span class=site-name>Likers</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="http://likers.github.io">Home</a></li>
<li ><a href="http://likers.github.io/categories.html">Categories</a></li>
<li ><a href="http://likers.github.io/tags.html">Tags</a></li>
<li ><a href="http://likers.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="http://likers.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page_header span10 offset2">
<h1><a href="http://likers.github.io/assign-retain-strong-weak-and-copy.html"> IOS tips #2: Assign, Retain, Strong, Weak, and Copy </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span8 offset2 article-content">
<h2>Retain VS Strong</h2>
<h3>Retain</h3>
<ul>
<li>
<p><code>retain</code> is required when the attribute is a pointer to an object. The setter generated by <code>@synthesize</code> will retain (aka add a retain count to) the object. </p>
</li>
<li>
<p>You will need to release the object when you are finished with it. By using retain it will increase the retain count and occupy memory in autorelease pool.</p>
</li>
<li>
<p>methods like "alloc" include an implicit "retain"</p>
</li>
</ul>
<h3>Strong</h3>
<ul>
<li>
<p><code>strong</code> is the ARC version of <code>retain</code>, or we can say <code>strong</code> = <code>retain</code></p>
</li>
<li>
<p><code>strong</code> <strong>owns</strong> the object, it will keep it untill you are done with it, then ARC will release it for you. Before iOS 4, youhave to use <code>retain</code> and manually <code>release</code> it, otherwise the object will always retain the memory. </p>
</li>
</ul>
<h2>Strong VS Weak</h2>
<h3>Weak</h3>
<ul>
<li>
<p><code>weak</code> <strong>doesn't own</strong> the object. A <code>weak</code> reference is a reference that you do not retain. Or in another word, a <code>weak</code> reference is a reference that does not protect the referenced object from release by ARC</p>
</li>
<li>
<p>We generally use <code>weak</code> for <code>IBOutlets</code> (UIViewController's Childs). This works because the child object only needs to exist as long as the parent object does.</p>
</li>
<li>
<p>We should use <code>weak</code> to avoid the potential of retain cycle.</p>
</li>
</ul>
<h3>Strong</h3>
<ul>
<li>
<p>The difference between <code>strong</code> and <code>weak</code> is that an object will be deallocated as soon as there are no strong pointers to it. Even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.</p>
</li>
<li>
<p>a very good explain from <a href="http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5">stackoverflow</a></p>
</li>
</ul>
<blockquote>
<p>Perhaps an example is in order.</p>
<p>Imagine our object is a dog, and that the dog wants to run away (be deallocated).</p>
<p>Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.</p>
<p>Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.</p>
<p>As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.</p>
</blockquote>
<h2>Copy VS Assign</h2>
<h3>Copy</h3>
<ul>
<li>
<p><code>copy</code> means make a new copy of that object when you use it. Use this if you need the value of the object <strong>as it is</strong> at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to <strong>release</strong> the object when you are finished with it because you are retaining the copy.</p>
</li>
<li>
<p><code>copy</code> is suggested when you define <code>NSString</code>, <code>NSArray</code> and <code>NSDictionary</code> properties. Beacuse these objects all have the mutable version child objects like <code>NSMutableString</code> and <code>NSMutableArray</code>, <code>NSMutableDictionary</code>. It is possible to assign a mutable string to a regular string. When this happens, if you don't use <code>copy</code> for that unmutable string and someone else could change the value of it. So we have to use <code>copy</code> to make a new copy to keep the value from change. </p>
</li>
<li>
<p><code>copy</code> is also suggested when define a block as property. According to Apple's document <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW12">Objects Use Properties to Keep Track of Blocks</a></p>
<blockquote>
<p>You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior.</p>
</blockquote>
</li>
</ul>
<h2>Assign</h2>
<ul>
<li><code>assign</code> is somewhat the opposite to copy. When calling the getter of an <code>assign</code> property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (<code>float</code>, <code>int</code>, <code>BOOL</code>...)</li>
</ul>
<aside>
<hr/>
<nav>
<ul class="articles_timeline">
<li class="previous_article">« <a href="http://likers.github.io/nonatomic-vs-atomic.html" title="Previous: IOS tips #1: Nonatomic VS Atomic">IOS tips #1: Nonatomic VS Atomic</a></li>
<li class="next_article"><a href="http://likers.github.io/set-cornerradius-for-part-of-a-uiview.html" title="Next: IOS Code Collection#1: Set cornerRadius for part of a UIView's corners">IOS Code Collection#1: Set cornerRadius for part of a UIView's corners</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<h4>Published</h4>
<time pubdate="pubdate" datetime="2016-01-11T00:00:00-06:00">Jan 11, 2016</time>
<h4>Last Updated</h4>
<div class="last_updated">2016-01-11 00:00:00-06:00</div>
<h4>Category</h4>
<a class="category-link" href="/categories.html#iOS-ref">iOS</a>
<h4>Tags</h4>
<ul class="list-of-tags tags-in-article">
<li><a href="/tags.html#interview-ref">interview
<span>3</span>
</a></li>
<li><a href="/tags.html#iOS-ref">iOS
<span>7</span>
</a></li>
</ul>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
</body>
</html>