Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 622
Ben Baron Avatar asked Mar 17 '12 20:03

Ben Baron


People also ask

What is Nsobject in iOS?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.1+ tvOS 9.0+ watchOS 2.0+

What is Nsstring?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.

What is id in Obj C?

id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.


2 Answers

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.

like image 124
bbum Avatar answered Oct 21 '22 09:10

bbum


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.

like image 1
mattjgalloway Avatar answered Oct 21 '22 11:10

mattjgalloway