开发者

What is a safe way to check for a protocol response

开发者 https://www.devze.com 2022-12-30 14:30 出处:网络
Here\'s a scenario: A view controller pushes a new controller to the nav controller.This child controller creates a model that uses a NSURLConnection.When this connection fin开发者_开发问答ishes it w

Here's a scenario:

A view controller pushes a new controller to the nav controller. This child controller creates a model that uses a NSURLConnection. When this connection fin开发者_开发问答ishes it will make a call like the following:

[self.delegate modelDidFinishParsing:self];

What is the safe way to produce this code? Right now, I have this code and it crashes in a certain situation:

if ([self.delegate conformsToProtocol:@protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];

The situation when it crashes is when the view controller that owns the model is popped from the stack before the model finishes. Should I make the model an ivar so that the controller releases it in its own - (void)dealloc ?


In your check, you could make sure the delegate isn't nil

if (self.delegate && [self.delegate conformsToProtocol...]) [self.delegate modelDidFinishParsing:self];
0

精彩评论

暂无评论...
验证码 换一张
取 消