【发布时间】:2015-03-20 15:50:15
【问题描述】:
我正在使用外部 SDK(通过在我的项目中包含他们的 xcode 项目)。 SDk 在 Objective-c 中正常工作,但是当我切换到 swift 时,我遇到了以下问题。
每当我实现参数类型为协议的委托方法时,xcode 突然给该类的对象声明错误,该类声明全局,即不在任何函数中。如果我评论那个特定的委托方法,我不会收到任何错误,它会成功编译/执行。
请检查以下 swift 代码,后跟我的 # cmets
//CustomView is subclass of UIView
var customview : CustomView = CustomView() // #1 error as : Use of undeclared type CustomView
@IBAction func showCustomView(sender: AnyObject)
{
// CustomView configurations
}
#pragma CustomView Delegates
func CustomViewShown(view: AnyObject!) /// #2 delegate work properly
{
}
func CustomView(view: AnyObject!, didFailWithError error: NSError!)
// #3 if I keep this method uncommented it gives error to #1 line
// if I commented this method all other works fine without error.
{
}
令人惊讶的是,上述所有委托和 SDK 都适用于 Objective-C,但不适用于 swift。
根据我的小研究,我得出的结论是, 我们不能在swift中使用相同的类名和方法名,即在我的例子中是CustomView。如果我使用 CustomView 来声明对象,我不能使用它作为方法名。
所以请有人验证一下,我是否正确?以及这个问题的解决方案是什么。
【问题讨论】:
标签: ios objective-c xcode swift ios8