我是新來的Swift,我試圖返回一組數組,但現在我陷入了一個錯誤的圈子。任何幫助,將不勝感激。我可能錯過了一些東西,但它確實不喜歡它:Swift:NSString不能轉換爲'字符串'
編輯:添加的代碼(未設置數組的約束)。
func createButtonWithTitle(title: String) -> UIButton {
let button = UIButton(type:.System) as UIButton
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(title, forState: .Normal)
button.sizeToFit()
button.titleLabel!.font = UIFont.systemFontOfSize(15)
button.backgroundColor = UIColor.greenColor()
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
return button
}
func createRowOfButtons(buttonTitles: [NSString]) -> UIView {
var buttons = [UIButton]()
let keyboardRowView = UIView()
for buttonTitle in buttonTitles{
let button = createButtonWithTitle(buttonTitle as String)
buttons.append(button)
keyboardRowView.addSubview(button)
}
addIndividualButtonConstraints(buttons, mainView: keyboardRowView)
return keyboardRowView
}
func createArraysOfButtons (rowTitles: [NSString]) -> UIView{
var rows = [UIView]()
let keyArrayView = UIView()
for rowTitle in rowTitles {
if let title = rowTitle as? String{
let row = createRowOfButtons(rowTitle as String)
rows.append(row)
keyArrayView.addSubview(row)
}
}
return keyArrayView
}
錯誤消息:
'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?
爲什麼不將參數類型更改爲[String]? –
@LeoDabus試圖這樣做,並得到了另一個錯誤:無法將類型'字符串'的值轉換爲預期的參數類型'[NSString]' – Geppelt
@LeoDabus我也在另一個函數中做了這個返回行,所以沒有理由不這樣做上班。 – Geppelt