Skip to Content
COCOA學習手冊--使用OBJECTIVE-C第三版
book

COCOA學習手冊--使用OBJECTIVE-C第三版

by Paris Buttfield-Addison, Jonathon Manning
June 2013
Intermediate to advanced
360 pages
6h 50m
Chinese
GoTop Information, Inc.
Content preview from COCOA學習手冊--使用OBJECTIVE-C第三版
Objective-C 物件導向編程
|
33
參照計數系統只有在物件的保留計數(retain count)為 0 時才會釋放該物件的記憶體。
ARC 透過監控其他物件何時參照此物件以及這些參照何時消失來管理保留計數。
當你有二或多個物件彼此相互參照,但卻沒有被應用程式其餘地方參照時,那樣是會有
問題的,這些物件無法被應用程式觸及,又因為各自都有被參照,因而不能夠被釋放。
為了解決這個問題,Objective-C 具有兩種不同類型的參照︰
強參照
strong reference
弱參照
weak reference
)。
強參照
(也稱作
擁有性參照
owning reference
)促使受參照之物件存留在記憶體中。
弱參照
仍然指向該記憶體,但不像強參照那樣被計數,那表示,ARC 不會遞增其參
照計數。
弱參照具有額外的好處,當受參照之物件被取消記憶體配置時,它會自動被設成
nil
,這表示,弱參照總是安全無虞的,因為它們要不是有效的物件,就是
nil
,絕
不會參照到未配置的記憶體。因為 Objective-C 允許傳送訊息給
nil
(什麼事都沒發
生),所以你的程式會繼續運作。
要宣告指向某物件的強參照,請在宣告特性時採用
strong
屬性。要宣告指向某物件的
弱參照,則使用
weak
屬性。
OS X Snow Leopard (10.6) iOS 4 以及更早期的版本並不支援弱參照。
如果你正在撰寫於那些平台上執行的軟體,請使用
assign
屬性,它的作
用等同於
weak
屬性,但
不會
在該物件被取消記憶體配置時讓該參照自動 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

網頁互動式資料視覺化:使用D3

網頁互動式資料視覺化:使用D3

Scott Murray
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
自然语言处理实战:从入门到项目实践

自然语言处理实战:从入门到项目实践

Sowmya Vajjala, Bodhisattwa Majumder, Anuj Gupta, Harshit Surana

Publisher Resources

ISBN: 9789862768136