Weak References in Mac Cocoa Programming

By: Aaron Hillegass Emailed: 1646 times Printed: 2115 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Sometimes, you will want to have an object pointer that should point to that object as long as it exists, but you do not want that pointer to prevent the garbage collector from deallocating the object. In this case, you can use a weak reference:

__weak NSFont *favoriteFont;

The garbage collector will feel free to deallocate an object, even if favoriteFont is currently pointing to the object. If favoriteFont is pointing to an object when it is deallocated, favoriteFont is automatically set to nil.

There are also collections (e.g., NSMapTable, NSHashTable, and NSPointerArray) that can hold objects weakly. Also, in a garbage-collected application, the notification center has weak references to the observers.


Cocoa Home | All Cocoa Tutorials | Latest Cocoa Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Great! Concise, helpful, a real life-saver. Google helped me to get here, and I swear I would NEVER GUESS that a site called "java-samples" contains such great help in Cocoa.

View Tutorial          By: Motti Shneor at 2012-07-31 14:29:37

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Aaron Hillegass
Printing in Mac Cocoa Programming
Web Services in Mac Cocoa Programming
Weak References in Mac Cocoa Programming
Garbage Collector in Cocoa Programming in Mac
Using NSOpenGLView in Cocoa Programming in Mac
Multithreading versus Multiprocessing in Mac OS X.
Life Cycle of a Cocoa Application
Frameworks in Cocoa Programming
Objects, Classes, Methods, and Messages in Cocoa Programming
Programming Language used in Cocoa Programming
What is Cocoa? A brief history of Cocoa.
Tools for Cocoa Programming

More Tutorials in Cocoa
Listing of files in a tar file in Mac OS X.
Listing of files in a zip file in Mac OS X.
Multithreading versus Multiprocessing in Mac OS X.
Using NSOpenGLView in Cocoa Programming in Mac
Garbage Collector in Cocoa Programming in Mac
Weak References in Mac Cocoa Programming
Web Services in Mac Cocoa Programming
Printing in Mac Cocoa Programming
Tools for Cocoa Programming
What is Cocoa? A brief history of Cocoa.
Programming Language used in Cocoa Programming
Objects, Classes, Methods, and Messages in Cocoa Programming
Frameworks in Cocoa Programming
Life Cycle of a Cocoa Application

More Latest News
Most Viewed Articles (in Cocoa )
Objects, Classes, Methods, and Messages in Cocoa Programming
What is Cocoa? A brief history of Cocoa.
Listing of files in a zip file in Mac OS X.
Weak References in Mac Cocoa Programming
Tools for Cocoa Programming
Frameworks in Cocoa Programming
Life Cycle of a Cocoa Application
Listing of files in a tar file in Mac OS X.
Multithreading versus Multiprocessing in Mac OS X.
Garbage Collector in Cocoa Programming in Mac
Programming Language used in Cocoa Programming
Printing in Mac Cocoa Programming
Using NSOpenGLView in Cocoa Programming in Mac
Web Services in Mac Cocoa Programming
Most Emailed Articles (in Cocoa)
Multithreading versus Multiprocessing in Mac OS X.
Tools for Cocoa Programming
Life Cycle of a Cocoa Application
Weak References in Mac Cocoa Programming
Web Services in Mac Cocoa Programming
Printing in Mac Cocoa Programming
Programming Language used in Cocoa Programming
Objects, Classes, Methods, and Messages in Cocoa Programming
Frameworks in Cocoa Programming
Listing of files in a tar file in Mac OS X.
Listing of files in a zip file in Mac OS X.
Using NSOpenGLView in Cocoa Programming in Mac
Garbage Collector in Cocoa Programming in Mac
What is Cocoa? A brief history of Cocoa.