Why did Apple previously typedef reference (pointer) types but not now?
Asked Answered
R

2

7

I've been wondering why Apple uses data types in Core Foundation that are typedef'd to a pointer type while in Cocoa they are not.

As an example, you would reference a UIColor object like UIColor * while a reference to a CGColor object would be CGColorRef? Or NSURL * and CFURLRef? Why not just always use CGColor * and CFURL *? Or conversely, why no UIColorRef or NSURLRef types, since you never access a UIColor or NSURL directly anyway?

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

Specifically, is there some reason Apple had a habit of doing this in their older frameworks, but stopped doing it in Cocoa? Is it simply a matter of style?

Rosalia answered 17/3, 2012 at 20:46 Comment(3)
Cocoa (as NeXTStep) is actually older than Core Foundation.Lemos
Ahh I didn't realize that. I always assumed it was the newer one.Rosalia
And actually now that you mention it, I did realize it was part of NeXTStep, I guess I was thinking newer as part of Apple's frameworks.Rosalia
V
8

What Matt said, but there is a bit more to it.

The typedefs in the C based APIs also allow the implementation details to be hidden. For example, you can have the following without ever defining the __CFURL structure in a public header.

typedef __CFURL *CFURLRef;

Objective-C has long had these kinds of features in the form of categories and, recently added, the ability to move instance variable declarations out of the header file. Expect that, over time, you will see all instance variables removed from the public header files in the SDK.

Note that the Cocoa frameworks long, long, pre-dated CoreFoundation.

As for why id is used instead of id *, that dates back to when Objective-C was first created in the early 1980s. Specifically, the notion of the language was that you would build "software integrated circuits" that could be "plugged together" like real ICs. The goal was to keep the C bits around as implementation details and, ideally, not exposed in your APIs.

As for why you end up with NSString * instead of NSString, that is largely exactly because of the C underpinnings of the language. I wrote a fairly detailed answer to a slightly different SO question that is relevant.

You'll probably also find this answer relevant, too.

Vite answered 17/3, 2012 at 21:36 Comment(1)
Ah nice @Vite :-). I was even going to say in my answer "but please wait for @Vite to speak"! I even tried to find an answer I swear you wrote about id vs id* before, but totally failed. Great answer here though.Seasick
S
1

The reason for NSURL* vs CFURLRef is pretty much that it's just coding style. Cocoa is an Objective-C API and the general style in Objective-C is to not have a typedef whereas Core Foundation is a C API and the general style of it is to use a typedef. It's pretty much down to coding style.

id vs id* - I am not entirely sure with that, but my guess is it's historical and they just wanted to have the base "object" to be without the *. I don't know for sure the history of that, though. But again it'll just be a style thing.

Seasick answered 17/3, 2012 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.