gpt4 book ai didi

ios - Swift:NSString 无法转换为 'String'

转载 作者:行者123 更新时间:2023-11-29 01:19:28 25 4
gpt4 key购买 nike

我是 Swift 的新手,我试图将一组行返回到一个数组中,但现在我陷入了错误循环。任何帮助,将不胜感激。我可能遗漏了一些东西,但感觉确实不像:

编辑:添加代码(未设置数组的约束)。

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?

最佳答案

最简单的解决方案:不要使用 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
}

主要问题是 createRowOfButtons 需要一个字符串数组,但您将单个字符串传递给该方法。 createRowOfButtons([rowTitle]) 解决了这个问题。

关于ios - Swift:NSString 无法转换为 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com