xcode - 无法将字符串类型的值转换为预期的参数类型 soundex

标签 xcode swift macos

我刚刚编写了这个名为 compute 的函数,它接受一个字符串并将其转换为一个 soundex 值。但是,当我调用该函数时,它给我一个错误,提示 Cannot convert value of type 'String' to expected argument type 'Soundex'。我不确定这里发生了什么,但我认为这与我没有在 Soundex 类中正确声明函数有关。

Soundex.swift

import Cocoa

class Soundex {

    func compute(word: String) -> String {
        return _compute(word, length: 4)
    }

    func _compute(word: String, length: Int) -> String {
        ...
    }
}

ViewController.swift

var lastName: String = lastNameText.stringValue
lastName = Soundex.compute(lastName) //get error on this line

任何帮助都会很棒,谢谢!

最佳答案

使用 Soundex.compute(lastName) 调用类方法。但是 compute(_:) 不是类方法。将方法更改为:

class func compute(word: String) -> String {
    ...
}

关于xcode - 无法将字符串类型的值转换为预期的参数类型 soundex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852217/

相关文章:

iphone - 在 iOS 6 中发送 udp 数据包

ios - Spritekit-如何设置两个物体之间的碰撞,使它们表现得像台球?

ios - 如何检查框架是否包含完整的位码?

macos - 当 'port' 不起作用时卸载 macports

ios - 无法在 Mac 上构建和运行 react-native 项目

ios - 带有界面生成器和 iPad 的多个 View Controller

ios - 在 Xcode 中重新调整坐标系

macos - 如果我是 JetBrains Toolbox 用户,如何关联 IntelliJ IDEA 在 "LightEdit"模式下打开的 Mac Finder 中的文件类型?

ios - View 不适合 iOS 模拟器

macos - 根据变量 swift 调整窗口大小