Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 2.3

COLUMN

The Core Foundation

Learn to use a useful suite of Mac OS X services

Issue: 2.3 (December/January 2003)
Author: Thomas Reed
Author Bio: Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.
Article Description: No description available.
Article Length (in bytes): 9,311
Starting Page Number: 30
Article Number: 2313
Resource File(s):

Download Icon 2313.zip Updated: 2013-03-11 19:07:57

Related Web Link(s):

http://www.tildesoft.com/Plugins.html
http://developer.apple.com/documentation/
http://www.tildesoft.com/Plugins.html
http://developer.apple.com/documentation/

Excerpt of article text...

In Mac OS X, Apple provides a useful set of objects and utility routines to handle basic data that nearly every application will use. This group of services is called the Core Foundation. The Core Foundation defines classes for holding strings, numbers, Booleans, arrays, dictionaries, and other data types. Some of the provided routines create and parse property list data, create and parse generic XML data, and interact with bundles. The practical value of these routines is great, and a discussion of the entirety of the Core Foundation is far beyond the scope of a single article. In this article, I will focus on implementing a few of these data types and routines in REALbasic, primarily for the purpose of reading and writing property list data. This column assumes you are familiar with using declare statements to call OS routines. If you are not, this topic was covered in the Advanced Techniques column of the first issue of REALbasic Developer.

Those with knowledge of the Core Foundation may already be aware of Kevin Ballard's CFPlugin (http://www.tildesoft.com/Plugins.html), which implements many of the Core Foundation classes. I would be irresponsible not to bring this plugin to your attention, as it makes much of the work I'll be talking about in this article unnecessary. I use this plugin in my own applications and highly recommend it. However, I know that many people prefer to avoid plugins whenever possible; these people can rest assured that the Core Foundation is still within reach!

All Core Foundation classes are opaque data structures, with their contents accessible only through defined functions. Since these data types may sometimes be used interchangeably, it is sometimes necessary to determine the type of an object you receive from an outside source. For example, an entry in a Core Foundation dictionary can be any type of object. When you retrieve an object from a dictionary, you should find out if the object is a string before you treat it like one!

Fortunately, the CFGetTypeID function makes it easy to determine the type of any generic Core Foundation object. Its result is an integer that identifies the object's type. The value of these object IDs should not be relied on, as the IDs may change in the future. Instead, the number should be compared to the result of routines like CFStringGetTypeID and CFArrayGetTypeID.

Also common to all Core Foundation objects is a reference counting system for disposing of unused objects. This system is very similar to REALbasic's reference counting, though the programmer must take more responsibility for the objects. The CFRetain method serves to increment an object's reference count, while the CFRelease method decrements the count. When the object's reference count reaches zero, it is released automatically. When creating a new Core Foundation object, its reference count will initially be 1. The creator has a reference to the object and has been given responsibility for it. When finished with the object, it is up to the creator to release it.

Ownership of the object can be passed on; the new owner typically retains the object and the old owner releases it, in that order. Note that if the old owner releases the object first, the object will be destroyed and the reference given to the new owner will be invalid. Using an invalid reference will cause your program to crash! (This actually happened to me several times during the development of the sample project; it is very easy to do.)

...End of Excerpt. Please purchase the magazine to read the full article.