I am getting the error in a project of mine:
Cannot convert value of type '[[String]]' to expected argument type '[String]'
I totally understand this error; I have a String within an Array within an Array that cannot be casted as a String within an Array. But the issue I am running into is a work around. What I am trying to do is return a string of titles as buttons, which is casted into rows of 7, which is cast into a group of 4 rows. So the buttons are casted from a string to UIButton, the Rows are arrays casted as UIViews, and the groups are arrays casted as UIViews. But when I go to cast the group this is when I get the error. Below is the relative code, and any help would be much appreciated (sorry for the bad english):
let buttonTitles1 = ["1","2","3","4","5","6","7"]
let buttonTitles2 = ["1","2","3","4","5","6","7"]
let buttonTitles3 = ["1","2","3","4","5","6","7"]
let buttonTitles4 = ["1","2","3","4","5","6","7"]
let row1 = buttonTitles1
let row2 = buttonTitles2
let row3 = buttonTitles3
let row4 = buttonTitles4
let matrixTitle1 = [buttonTitles1, buttonTitles1, buttonTitles1, buttonTitles1]
let matrixTitle2 = [buttonTitles1, buttonTitles1, buttonTitles1, buttonTitles1]
let matrix1 = createArraysOfButtons(matrixTitle1)
let matrix2 = createArraysOfButtons(matrixTitle2)
keyBoardKeys.addSubview(matrix1) //ERROR
keyBoardKeys.addSubview(matrix2) //ERROR
//BUTTON FUNCTION
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)
print("BUTTON")
return button
}
//ROW FUNCTION
func createRowOfButtons(buttonTitles: [String]) -> UIView {
var buttons = [UIButton]()
let keyboardRowView = UIView()
keyboardRowView.translatesAutoresizingMaskIntoConstraints = false
for buttonTitle in buttonTitles{
let button = createButtonWithTitle(buttonTitle as String)
buttons.append(button)
keyboardRowView.addSubview(button)
}
addIndividualButtonConstraints(buttons, mainView: keyboardRowView)
print("ROW")
return keyboardRowView
}
//MATRIX FUNCTION
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)
}
addConstraintsToInputView(rows, mainView: keyArrayView)
print("MATRIX")
return keyArrayView
}
Your problem is with this function:
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)
}
addConstraintsToInputView(rows, mainView: keyArrayView)
print("MATRIX")
return keyArrayView
}
It should take an array of arrays and it instead takes a simple array, change your function to this:
func createArraysOfButtons (rowTitles: [[String]]) -> UIView {
var rows = [UIView]()
let keyArrayView = UIView()
for rowTitle in rowTitles {
let row = createRowOfButtons(rowTitle) // remove square brakets
rows.append(row)
keyArrayView.addSubview(row)
}
addConstraintsToInputView(rows, mainView: keyArrayView)
print("MATRIX")
return keyArrayView
}
Think about what your working with, you've got an [String]:
let buttonTitles4 = ["1","2","3","4","5","6","7"]
and you're storing it into another array making it [[String]]
let matrixTitle1 = [buttonTitles1, buttonTitles1, buttonTitles1, buttonTitles1]
Then you're calling your function
let matrix1 = createArraysOfButtons(matrixTitle1)
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.