getting a warning setting up delegate for a custom protocol

本秂侑毒 提交于 2019-12-05 20:54:09

You are running into an issue caused by an ambiguity in how Objective-C finds a matching selector and dealing with an id reference.

UIStoryboardSegue destinationViewController returns an id. Your code then tries to call the setDelegate method on this id reference. Since there is no information about what this id actually references, it doesn't know which setDelegate: method you might mean (there are many). So the compiler scans through the list it knows of and picks one. In this case it chose the setDelegate: method from the NSFileManager class. Since self doesn't conform to the NSFileManagerDelegate protocol, you get the warning.

You could ignore the warning and your code will work fine in this case.

The better solution is to help the compiler by adding a cast:

[(SomeSecondCustomViewController *)segue.destinationViewController setDelegate:self];

This will let the compiler know which setDelegate: method you really mean.

BTW - Adding NSFileManagerDelegate to your class is not a valid solution even if it works at the moment. A simple reordering of some import statements could lead the compiler to make a different choice and your warning would return but complain about not conforming to some other protocol.

as it turns out, this is a bug / change in Xcode 6 beta. running this exact same code on Xcode 5.1.1 produces no warnings or errors. the problem is cause because in Xcode 6 the compiler is asking for type

(id<NSFileManager>)

for the delegate. in Xcode 5.1 the compiler is simply expecting

(id)

for the delegate type.

as rmaddy stated, by casting the type in

[(SomeSecondCustomViewController *)segue.destinationViewController setDelegate:self];

it did remove the warning, but this should be an unnecessary step and will chalk it up to a problem with Xcode.

Try explicitly typing (giving type to) the destinationVC, like this:

SomeSecondCustomViewController *vc = (SomeSecondCustomViewController  *)segue.destinationViewController;
vc.delegate = self;

@rmaddy provides the correct answer. This is a more detailed example for those of us who are lightly schooled in the ways of Objective-C.

I changed my code from:

    [[segue destinationViewController] setDelegate:self];
    UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
    self.flipsidePopoverController = popoverController;
    popoverController.delegate = self;

to:

    [(UIPopoverController *)segue.destinationViewController setDelegate:self];
    UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
    self.flipsidePopoverController = popoverController;
    popoverController.delegate = self;

and the warnings disappeared.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!