简体   繁体   English

协议作为快速冲突中的参数类型

[英]protocol as parameter type in swift conflicts

I am using external SDK(by including their xcode project in my project). 我正在使用外部SDK(通过将xcode项目包含在我的项目中)。 The SDk was working properly in objective-c, but when I switched to swift, I am getting following problem. SDk在Objective-c中正常工作,但是当我切换到swift时,我遇到了以下问题。

Whenever I implementing delegate method where parameter is of type protocol, xcode suddenly gives error to object declaration of that Class which declared globally ie not in any function. 每当我实现参数类型为协议的委托方法时,xcode都会突然给该类的对象声明产生错误,该类声明是全局声明的,即未在任何函数中声明。 If I comment that particular delegate method I will not get any error and it compile/executes successfully. 如果我评论该特定的委托方法,我将不会收到任何错误,并且可以成功编译/执行。

Please check the following swift code followed by my # comments 请检查以下快速代码,然后加上我的#条注释

//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.
        {

        }

Surprising thing is all the above delegate and SDK works fine for objective-C but not for swift. 令人惊讶的是,上述所有委托和SDK对于Objective-C而言都可以正常运行,但对于迅速而言却不是。

On the basis of my little research, I am concluding that, We can not use the Class name and method name same in swift, ie in my case its CustomView. 根据我的研究,我得出结论,我们不能迅速使用相同的类名和方法名,即我的情况是CustomView。 If I am using CustomView for declaring object, I can not use it as method name. 如果我使用CustomView声明对象,则不能将其用作方法名称。

so someone please verify that, I am correct or not ? 所以有人请确认我是正确的吗? and what is solution for this issue. 以及该问题的解决方案是什么。

It's essentially a name conflicting problem. 从本质上讲,这是一个名称冲突的问题。

Inside of your class declaration, CustomView is a method name, but not a class name. 在类声明中, CustomView是方法名称,而不是类名称。 So, basically, your assumption is correct. 因此,基本上,您的假设是正确的。

But, you have a workaround. 但是,您有一种解决方法。

Let's suppose CustomView is declared in the SDK. 假设在SDK中声明了CustomView And that is a framework named SomeSDK . 那就是一个名为SomeSDK的框架。 Then you can reference the CustomView like this: 然后,您可以像这样引用CustomView

import SomeSDK

class ViewController: UIViewController {

    var customview: SomeSDK.CustomView = SomeSDK.CustomView()

    func CustomView(view: AnyObject!, didFailWithError error: NSError!) {
    }
}

If you don't want to prefix SomeSDK. 如果您不想在SomeSDK.前缀SomeSDK. everywhere, you can typealias it: 到处都可以typealias

import SomeSDK

typealias SDKCustomView = CustomView // you can use `CustomView` here because there is no conflicting name.

class ViewController: UIViewController {

    var customview: SDKCustomView = SDKCustomView()

    func CustomView(view: AnyObject!, didFailWithError error: NSError!) {
    }
}

I may be wrong, but it seems in swift you can also explicitly call the init function. 我可能是错的,但是看来您也可以迅速调用init函数。

Instead of calling: 而不是调用:

var customview : CustomView = CustomView()

you can call: 您可以致电:

var customview : CustomView = CustomView.init()

This works in my Playground, let me know how it works out for you. 这在我的游乐场中有效,让我知道如何为您工作。 This would allow you to use your function named as it is. 这将允许您按原样使用函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM