I am new to Swift and I am trying to return a group of rows into an array, but now I am trapped in a circle of errors. Any help would be appreciated. I am probably missing something, but it sure does not feel like it:
EDIT: Added code (Constraints for arrays not set).
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
}
Error Message:
'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?
Simplest solution: Do not use NSString
func createRowOfButtons(buttonTitles: [String]) -> UIView {
var buttons = [UIButton]()
let keyboardRowView = UIView()
for buttonTitle in buttonTitles{
let button = createButtonWithTitle(buttonTitle)
buttons.append(button)
keyboardRowView.addSubview(button)
}
addIndividualButtonConstraints(buttons, mainView: keyboardRowView)
return keyboardRowView
}
func createArraysOfButtons (rowTitles: [String]) -> UIView{
var rows = [UIView]()
let keyArrayView = UIView()
for rowTitle in rowTitles {
let row = createRowOfButtons([rowTitle])
rows.append(row)
keyArrayView.addSubview(row)
}
return keyArrayView
}
The main issue is that createRowOfButtons
expects an array of string but you're passing a single string to the method. createRowOfButtons([rowTitle])
solves that issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.