简体   繁体   English

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

[英]'[[String]]' to expected argument type '[String]'

I am getting the error in a project of mine: ​ 我在我的一个项目中遇到了错误:

Cannot convert value of type '[[String]]' to expected argument type '[String]'​ 无法将类型[[[String]]'的值转换为预期的参数类型'[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. 我想做的是返回一个标题字符串作为按钮,将其强制转换为7行,强制7行转换为4行。 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. 因此,按钮从字符串转换为UIButton,行是转换为UIViews的数组,而组是转换为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]: 考虑一下您的工作,您有一个[String]:

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

and you're storing it into another array making it [[String]] 然后将其存储到另一个数组中,使其成为[[String]]

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

Then you're calling your function 然后,您正在调用函数

let matrix1 = createArraysOfButtons(matrixTitle1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' Nil与预期的参数类型&#39;String&#39;不兼容 - Nil is not compatible with expected argument type 'String' 无法将类型(“字符串:字符串”)的值转换为预期的参数类型“ URL” - Cannot convert value of type ('string: String)' to expected argument type 'URL' 无法将类型“ [String:Any]”的值转换为预期的参数类型“ String” - Cannot convert value of type '[String : Any]' to expected argument type 'String' 无法转换类型&#39;(用户)-&gt;字典的值 <String, String> &#39;到预期参数类型&#39;Dictionary <String, String> &#39; - Cannot convert value of type '(User) -> Dictionary<String, String>' to expected argument type 'Dictionary<String, String>' 无法转换&#39;NSString&#39;类型的值? 预期参数类型&#39;String&#39; - Cannot convert value of type 'NSString'? to expected argument type 'String' 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift 无法将类型“()”的值转换为预期的参数类型“字符串” - Cannot convert value of type '()' to expected argument type 'String' 无法转换类型[[String:Any]? 到预期的参数类型“ _?” - Cannot convert value of type '[String:Any]?' to expected argument type '_?' 无法转换&#39;String?&#39;类型的值 预期的参数类型&#39;URL&#39; - Cannot convert value of type 'String?' to expected argument type 'URL'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM