Vimzy
Vimzy

Reputation: 1965

Confusion Regarding Protocol for Delegates

I have a few important questions for iOS development for protocols in terms of delegates. So when we say that an object A will conform to some object B, we are saying that A will provide implementation for the required methods of object B, correct? That makes sense. What doesn't make sense is some of the other methods of Object B that we use sometimes. For example, let's use the NSURLConnectionDelegate. One of the methods NSURLConnectionDelegate allows us to use is the following:

[NSURLConnection connectionWithRequest:request delegate:self];

But we never provided an implementation for the above method. So what is the above method a part of, the protocol? Or something else? What methods fall under being a part of an object's protocol vs a method from an object that we can simply just use, like the one I provided above. The one below we actually have to implement:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...}

So is the above a part of the protocol? I'm getting kind of lost here. I'm trying to get a proper understanding. Any help/insight would be greatly appreciated.

Upvotes: 0

Views: 39

Answers (2)

user3821934
user3821934

Reputation:

Be careful with your notation. An class can conform to a protocol by implementing the protocols required methods and zero or more of its optional methods.

The class in which you implement the delegate protocol is up to you. But to say that "One of the methods NSURLConnectionDelegate allows us to use is ... connectionWithRequest..." is confusing. That method is a class method which is a kind of convenience: it allows you to bypass explicit creation of an NSURLConnection object. I am not sure why you think it has anything to do with the delegate protocol. All it is doing is asking you to provide any object that implements the protocol, which it will use to report progress back to you.

I hope this helps you.

Upvotes: 1

Schemetrical
Schemetrical

Reputation: 5536

[NSURLConnection connectionWithRequest:request delegate:self];

This is a class method that takes a delegate as a parameter. The class method is like any other method and is implemented by NSURLConnection. This means that whichever class you put for the delegate parameter, that class must also conform to the NSURLConnectionDelegate protocol.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...}

This is a method from the UITableViewDataSource protocol. Anything that implements the UITableViewDataSource protocol must implement this method since its required.

Upvotes: 0

Related Questions