我需要帮助将此Objective-C方法重写为Swift。
目标C代码:
- (void)pushViewControllerAnimatedWithMember:(Member *)member withSelf:(id)controller
{
ZJMHomePageViewController *homePageViewController = [ZJMHomePageViewController new];
homePageViewController.member = member;
homePageViewController.delegate = controller;
[self.navigationController pushViewController:homePageViewController animated:YES];
}
SWIFT代码:
func pushViewControllerAnimatedWithMember(member: Member, withSelf controller: AnyObject?)
{
let homePageViewController : ZJMHomePageViewController = ZJMHomePageViewController.new()
homePageViewController.member = member
homePageViewController.delegate = controller // gives error as Not able to assign any object to delegate
self.navigationController?.pushViewController(homePageViewController,animated:true)
}
最佳答案
委托属性类型必须为AnyObject,以便可以通过它。基本上,控制器和委托必须为相同类型,否则在分配其值时需要添加强制类型转换
关于objective-c - 如何将Unknown class(Controller)分配为AnyObject来委托(delegate)其他某个类?在 swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31142808/