'[[String]]'到预期的参数类型'[String]'

时间:2016-01-21 17:45:58

标签: ios arrays string swift

我在我的项目中收到错误:

  

无法将'[[String]]'类型的值转换为预期的参数类型'[String]'

我完全理解这个错误;我在数组中的数组中有一个字符串,不能在数组中作为字符串传递。但我遇到的问题是解决问题。我要做的是将一串标题作为按钮返回,这些按钮被转换为7行,这些行被转换为4行的组。因此按钮从字符串转换为UIButton,Rows是作为UIViews转换的数组,组是作为UIViews转换的数组。但是当我去组织这个时,我就得到了错误。以下是相关代码,我们非常感谢任何帮助(对不起英语不好):

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
}

1 个答案:

答案 0 :(得分:0)

你的问题在于这个功能:

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
}

它应该采用数组数组,而是采用一个简单的数组,将函数更改为:

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
}

考虑一下你的工作,你有一个[String]:

let buttonTitles4 = ["1","2","3","4","5","6","7"]

然后将它存储到另一个数组中,使其成为[[String]]

let matrixTitle1 = [buttonTitles1, buttonTitles1, buttonTitles1, buttonTitles1]

然后你正在调用你的函数

let matrix1 = createArraysOfButtons(matrixTitle1)