为什么我在此代码中出现“缺少参数参数”错误?

时间:2021-02-27 22:45:10

I have a error in Swift: "Missing argument for parameter 'bundle' in call"

我在Swift中有一个错误:“在调用中缺少参数'bundle'的参数”

Class Radio : UIViewController {
    let url = NSURL(string: "myurl")

    @IBAction func newButtonPressed(sender: AnyObject){
        var streamer = Radio.alloc().init(url!)
    }
}

I don't really understand this error so if someone can help.

我真的不明白这个错误,所以如果有人可以提供帮助。

2 个解决方案

#1


0  

You're trying to initialize your Radio class with a parameter which is url.

您正在尝试使用url参数初始化Radio类。

But this class doesn't have an init(url: NSURL) (or at least it's not shown in your question) so you can't.

但是这个类没有init(url:NSURL)(或者至少它没有在你的问题中显示)所以你不能。

You need to create an init(url: NSURL) and inside this init you also have to call the super init for UIViewController (even if you don't use it, as in my example) since your Radio class inherits from it.

你需要创建一个init(url:NSURL),在这个init中你还必须为UIViewController调用super init(即使你没有使用它,就像在我的例子中一样),因为你的Radio类继承了它。

This is why you get this error:

这就是您收到此错误的原因:

Missing argument for parameter 'bundle' in call

在调用中缺少参数'bundle'的参数

because Xcode suggests to use the init(nibName: String?, bundle: NSBundle?) initializer for the class since it's the only one available.

因为Xcode建议为类使用init(nibName:String?,bundle:NSBundle?)初始化器,因为它是唯一可用的初始化器。

So, keeping your code example as a base, you could do something like this to make it work:

因此,将代码示例作为基础,您可以执行以下操作以使其工作:

class Radio : UIViewController {

    var url = NSURL(string: "someDefaultURL")

    init(url: NSURL) {
        self.url = url
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("not implemented")
    }

    @IBAction func newButtonPressed(sender: AnyObject) {
        let newURL = NSURL(string: "myurl")
        let streamer = Radio(url: newURL!)
        // do something with `streamer`
    }
}

#2


0  

Type var streamer = Radio then type ( and wait for Xcode hint the constructor methods. I guess maybe this method will be var streamer = Radio(url:url!).

键入var streamer = Radio然后键入(并等待Xcode提示构造函数方法。我想这个方法可能是var streamer = Radio(url:url!)。

#1


0  

You're trying to initialize your Radio class with a parameter which is url.

您正在尝试使用url参数初始化Radio类。

But this class doesn't have an init(url: NSURL) (or at least it's not shown in your question) so you can't.

但是这个类没有init(url:NSURL)(或者至少它没有在你的问题中显示)所以你不能。

You need to create an init(url: NSURL) and inside this init you also have to call the super init for UIViewController (even if you don't use it, as in my example) since your Radio class inherits from it.

你需要创建一个init(url:NSURL),在这个init中你还必须为UIViewController调用super init(即使你没有使用它,就像在我的例子中一样),因为你的Radio类继承了它。

This is why you get this error:

这就是您收到此错误的原因:

Missing argument for parameter 'bundle' in call

在调用中缺少参数'bundle'的参数

because Xcode suggests to use the init(nibName: String?, bundle: NSBundle?) initializer for the class since it's the only one available.

因为Xcode建议为类使用init(nibName:String?,bundle:NSBundle?)初始化器,因为它是唯一可用的初始化器。

So, keeping your code example as a base, you could do something like this to make it work:

因此,将代码示例作为基础,您可以执行以下操作以使其工作:

class Radio : UIViewController {

    var url = NSURL(string: "someDefaultURL")

    init(url: NSURL) {
        self.url = url
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("not implemented")
    }

    @IBAction func newButtonPressed(sender: AnyObject) {
        let newURL = NSURL(string: "myurl")
        let streamer = Radio(url: newURL!)
        // do something with `streamer`
    }
}

#2


0  

Type var streamer = Radio then type ( and wait for Xcode hint the constructor methods. I guess maybe this method will be var streamer = Radio(url:url!).

键入var streamer = Radio然后键入(并等待Xcode提示构造函数方法。我想这个方法可能是var streamer = Radio(url:url!)。