Why did Apple previously refer to the typedef type (pointer), but not now?

I was wondering why Apple uses data types in Core Foundation, which typedef'd are pointer types, but they are not in Cocoa.

As an example, you are referencing a UIColor object, such as UIColor * , and the reference to the CGColor object will be CGColorRef ? Or NSURL * and CFURLRef ? Why not just use CGColor * and CFURL * ? Or, on the contrary, why are there no UIColorRef or NSURLRef , since you never ever get access to UIColor or NSURL ?

Or, for example, why is it id and not id * , since it is actually a pointer and can actually be typecast to void * ?

In particular, is there a reason Apple had the habit of doing this in its old framework, but stopped doing it in Cocoa? Is it just a matter of style?

+7
source share
2 answers

What Matt said, but there is a bit more.

typedefs in the C-based API also allow you to hide implementation details. For example, you may have the following without defining the __CFURL structure in the public header.

 typedef __CFURL *CFURLRef; 

Objective-C has long had such functions in the form of categories, and recently added the ability to transfer instance variable declarations from the header file. Expect that over time, you will see all instance variables deleted from the public header files in the SDK.

Note that Cocoa frames are long, long, predefined by CoreFoundation.

As to why id used instead of id * , this dates back to when Objective-C was first created in the early 1980s. In particular, the concept of language was that you would build “software integrated circuits” that could be “plugged in” like real IPs. The goal was to save the C bit as implementation details and, ideally, not display in your APIs.

As for why you end up with NSString * instead of NSString , it's largely because of the C-bases of the language. I wrote a fairly detailed answer to a slightly different SO question that matters.

You will probably also find this answer .

+8
source

The reason NSURL* vs CFURLRef is pretty much because it's just a coding style. Cocoa is the Objective-C API, and the general style in Objective-C is not to have a typedef, while Core Foundation is the C API, and the general style is to use typedef. This is pretty much up to the coding style.

id vs id* - I’m not quite sure about this, but I assume that it is historical, and they just wanted the base “object” to be without * . However, I do not know, of course, the history of this. But again, it will be just a style.

+1
source

All Articles