Core Foundation框架(2)命名规范,内省

命名规则中最重要的就是Create Rule,官方说明如下:

There is an important distinction between Get, and Copy and Create, in names of functions that return values. If you use a Get function, you cannot be certain of the returned object’s life span. To ensure the persistence of such an object you can retain it (using the CFRetain function) or, in some cases, copy it. If you use a Copy or Create function, you are responsible for releasing the object (using the CFRelease function). For more details, see Memory Management Programming Guide for Core Foundation.

即如果是通过Get方式返回的对象(而不是指针,对应的Cocoa中Get函数返回的时指针),你不需要负责release这个对象,但是不保证对象的生命周期。如果是Create 或者是Copy方式生成的对象,则保证生命周期,但是需要负责Release.

有的函数名字叫做createCopy,这就说明第一个参数需要传入一个分配器。

有的函数需要支持多态,比如CFCopyDescription,CFShow等等,这是需要传入参数是CFType类型才能保证可以多态。

一般函数定义如下:

CFStringRef CFCopyDescription(CFTypeRef cf);

void CFShow(CFTypeRef obj);

而CFTypeRef仅仅定义为:typedef const void * CFTypeRef;

原文地址:https://www.cnblogs.com/CharlieSu/p/4609810.html