我刚刚编写了这个名为 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/