【问题标题】:Argument labels '(URL:)' do not match any available overloads参数标签 '(URL:)' 不匹配任何可用的重载
【发布时间】:2017-09-08 08:47:24
【问题描述】:

我正在尝试将我的代码转换为 Swift 3,但出现此错误:

参数标签“(URL:)”不匹配任何可用的重载

let viewController = VisitableViewController(URL: URL)

这是我的全部代码:

var window: UIWindow?
var navigationController = UINavigationController()
var session = Session()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window?.rootViewController = navigationController
    visit(URL: NSURL(string: "http://localhost:9292/")!)
    return true
}

func visit(URL: NSURL) {
    let viewController = VisitableViewController(URL: URL)
    navigationController.pushViewController(viewController, animated: true)
    session.visit(viewController)
}

这是 VisitableViewController :

import UIKit

open class VisitableViewController: UIViewController, Visitable {
    open weak var visitableDelegate: VisitableDelegate?

    open var visitableURL: URL!

    public convenience init(url: URL) {
        self.init()
        self.visitableURL = url
    }

    // MARK: Visitable View

    open private(set) lazy var visitableView: VisitableView! = {
        let view = VisitableView(frame: CGRect.zero)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    fileprivate func installVisitableView() {
        view.addSubview(visitableView)
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options: [], metrics: nil, views: [ "view": visitableView ]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: [ "view": visitableView ]))
    }

    // MARK: Visitable

    open func visitableDidRender() {
        self.title = visitableView.webView?.title
    }

    // MARK: View Lifecycle

    open override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        installVisitableView()
    }

    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        visitableDelegate?.visitableViewWillAppear(self)
    }

    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        visitableDelegate?.visitableViewDidAppear(self)
    }

    /*
     If the visitableView is a child of the main view, and anchored to its top and bottom, then it's
     unlikely you will need to customize the layout. But more complicated view hierarchies and layout 
     may require explicit control over the contentInset. Below is an example of setting the contentInset 
     to the layout guides.

    public override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        visitableView.contentInset = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: bottomLayoutGuide.length, right: 0)
    }
    */
}

【问题讨论】:

  • 你需要告诉我们VisitableViewController构造函数。
  • 请⌘-单击VisitableViewController并在头文件中查找正确的方法。
  • @vadian 它来自 UIKits 框架,我已经在我的代码中更新了它。
  • 是的,看看我的评论。 @AbhishekJain
  • 无论声明来自何处,自己找出签名的最简单方法是⌘-单击符号。

标签: ios swift url swift3


【解决方案1】:

我建议:

func visit(url: URL) {
    let viewController = VisitableViewController(url: url)
    navigationController.pushViewController(viewController, animated: true)
    session.visit(viewController)
}

这是使用url: 而不是URL: 作为VisitableViewController 初始化程序。我还建议使用visit 遵循相同的约定,并将NSURL 替换为URL

【讨论】:

  • 谢谢它的工作!我还必须更新这一行 visit(url: URL(string: "localhost:9292/")!)
【解决方案2】:

试试这个代码-

func visit(URL: NSURL) {
    let viewController = VisitableViewController(url: URL)
    navigationController.pushViewController(viewController, animated: true)
    session.visit(viewController)
}

改变-

visit(URL: NSURL(string: "http://localhost:9292/")!)

visit(url: URL(string: "http://localhost:9292/")!)

【讨论】:

  • 谢谢!但现在我得到'无法将'URL.type'类型的值转换为预期的参数类型'URL'
  • 是的,我将其标记为正确。它正在工作。谢谢!
【解决方案3】:

我在 viewDidLoad 中使用了这个

self.navigationController?.navigationBar.topItem?.title = "YourTitle";
        
并且正在工作!

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 2016-12-16
    • 1970-01-01
    • 2018-04-02
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多