views:

124

answers:

3

I was looking at the docs for NSTableViewDataSource and they say that this protocol is only available on osx 10.6. How can this be? Isn't NSTableViewDataSource the object that contains all the data for your NsTableView?

+2  A: 

NSTableViewDataSource exists in 10.6 as a formal protocol, in older versions, the methods in NSTableViewDataSource are just delegate methods, but it works the same way: you implement the methods in your data source object.

Zydeco
+1  A: 

In 10.6, Apple formalized many delegate and data source informal protocols. Thus an informal protocol like

@interface NSObject (MyViewDataSource)

// data source methods

@end

became

@protocol MyViewDataSource
//data source methods
@end

The advantage is that the compiler can do some compile-time checking to warn you if you pass a non-conformant instance (i.e. doesn't implement NSTableViewDataSource as the data source to, e.g. an NSTableView and can warn you if you don't implement all the required methods of the protocol after declaring that a class implements the protocol in its @interface.

The @protocol(NSTableViewDataSource) doesn't exist prior to 10.6, so you'll have to declare it if you want to compile with the SDK for previous versions. You can do so and pass your data source object (which now implements a dummy protocol) as the data source in previous SDKs since it necessarily also conforms to the informal protocol defined in those previous versions.

Barry Wark
... and prior to 10.3, the table data source protocol was the *only* way to populate a table (well, the only sane way).
Joshua Nozzi
+4  A: 

To add what @Zydeco wrote, @protocols in Objective-C didn't have @optional attributes before 10.5. So, Apple couldn't use @protocol to define what are the delegate methods, because the delegate objects choose to implement only a part of delegate methods. Apple used something called an informal protocol in those olden days, which were just NSObject categories.

Now that they introduced the way to mark some methods as @optional in a @protocol, they decided to make every delegate a formal protocol in 10.6. This way there can be more compile-time checking. But the functionality has not changed.

See the official documentation on the protocols for more info.

Yuji
Right. The `@optional` keyword was introduced in Objective-C 2.0, which launched along with Leopard. For some reason, it took Apple until Snow Leopard to formalize the old informal protocols.
Alex