调用无参数函数时需要参数 swift
While calling a no parameterized function wants a parameter swift
我使用 Duncan C 的 post 编写了该函数。我输入的不是参数,但在调用函数 Xcode 时需要一个 ViewController 参数。我该如何解决这个问题?
编辑:我添加了其余代码。是什么破坏了 assign() 函数?
呼叫:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textBox: UITextView!
@IBOutlet weak var firstInput: UITextField!
@IBOutlet weak var resultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
firstInput.returnKeyType = .Search
firstInput.delegate = self
textBox.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if firstInput.text == "" {
}
else {
getFromPath()
}
self.view.endEditing(true)
return false
}
func search(#set: [String], letters: String) -> [String] {
let result = filter(set) { item in
for char in letters {
if !contains(item, char) {
return false
}
}
return true
}
return result
}
func assign(){
let path = "/Users/ardakaraca/Documents/Xcode/ATC Radio/Stands/Stands/words.txt"
//let bundle = NSBundle.mainBundle()
//let path = bundle.pathForResource("words", ofType: "txt")
let content = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
let newArray = content!.componentsSeparatedByString("\n")
}
let newConten
func getFromPath() {
//getFromPath() func used to be in assign func.
var letters = firstInput.text
var res = search(set: newArray, letters: letters)
textBox.text! = ""
for element in res {
textBox.text = (textBox.text ?? "") + "\n" + "\(element)"
}
}
}
您已将 assign()
定义为 ViewController()
class 的 实例方法 ,这意味着它必须在
class 的实例。
如果您尝试用
初始化 class 的 属性
let dictArray = assign()
然后assign
被当作"curried function"类型
ViewController -> () -> [String]
这解释了 Xcode 中意外的自动补全(参见
http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/).
最简单的
解决方案是将 assign()
函数移出
ViewController
class 并将其定义为 "free function"
(也许为该函数选择一个更好的名称):
import UIKit
func getWordList() -> [String] {
let path = NSBundle.mainBundle().pathForResource("words", ofType: "txt")!
let content = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
let wordList = content!.componentsSeparatedByString("\n")
return wordList
}
class ViewController: UIViewController {
let wordList = getWordList()
// ...
}
我使用 Duncan C 的 post 编写了该函数。我输入的不是参数,但在调用函数 Xcode 时需要一个 ViewController 参数。我该如何解决这个问题?
编辑:我添加了其余代码。是什么破坏了 assign() 函数?
呼叫:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textBox: UITextView!
@IBOutlet weak var firstInput: UITextField!
@IBOutlet weak var resultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
firstInput.returnKeyType = .Search
firstInput.delegate = self
textBox.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if firstInput.text == "" {
}
else {
getFromPath()
}
self.view.endEditing(true)
return false
}
func search(#set: [String], letters: String) -> [String] {
let result = filter(set) { item in
for char in letters {
if !contains(item, char) {
return false
}
}
return true
}
return result
}
func assign(){
let path = "/Users/ardakaraca/Documents/Xcode/ATC Radio/Stands/Stands/words.txt"
//let bundle = NSBundle.mainBundle()
//let path = bundle.pathForResource("words", ofType: "txt")
let content = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
let newArray = content!.componentsSeparatedByString("\n")
}
let newConten
func getFromPath() {
//getFromPath() func used to be in assign func.
var letters = firstInput.text
var res = search(set: newArray, letters: letters)
textBox.text! = ""
for element in res {
textBox.text = (textBox.text ?? "") + "\n" + "\(element)"
}
}
}
您已将 assign()
定义为 ViewController()
class 的 实例方法 ,这意味着它必须在
class 的实例。
如果您尝试用
初始化 class 的 属性let dictArray = assign()
然后assign
被当作"curried function"类型
ViewController -> () -> [String]
这解释了 Xcode 中意外的自动补全(参见 http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/).
最简单的
解决方案是将 assign()
函数移出
ViewController
class 并将其定义为 "free function"
(也许为该函数选择一个更好的名称):
import UIKit
func getWordList() -> [String] {
let path = NSBundle.mainBundle().pathForResource("words", ofType: "txt")!
let content = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
let wordList = content!.componentsSeparatedByString("\n")
return wordList
}
class ViewController: UIViewController {
let wordList = getWordList()
// ...
}