行是指每一行都是数组的不同索引(按顺序)。
这是我的类,它包含一个歌曲列表(数组目前为空,因为用户需要输入歌曲,这是在另一个 ViewController 中完成的):
class Songs {
private var _songs : [String]
var songs : [String]
{
get
{
return _songs
}
set (newSongs)
{
_songs = newSongs
}
}
init(songs: [String])
{
self._songs = songs
}
func songList() -> [String] {
let songs = _songs
return songs
}
}
var songList = Songs (songs: [String]())
我希望标签显示数组的第三个 ViewController:
class ThirdViewController: UIViewController {
// Properties
@IBOutlet weak var songList_lbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var multiLineString = songList.songList()
multiLineString.joined(separator: "\n") // warning telling me separator is unused
songList_lbl.text = multiLineString // error telling me cannot assign value of type '[String]' to type 'String?'
songList_lbl.numberOfLines = 0 // code I found but haven't tested out yet
songList_lbl.lineBreakMode = NSLineBreakMode.byWordWrapping
songList_lbl.sizeToFit()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我也试过转储数组,但我仍然得到值类型错误。
最佳答案
Joined 上有一个“ed”,在 swift 中表示它是一个副本,而不是一个变异方法。您必须分配给它返回的副本,例如:
songList_lbl.text = multiLineString.joined(separator: "\n")
关于ios - 如何将数组打印为标签中的行 (Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41842597/