按行表示每行是数组的不同索引(按顺序)。
这是我的课程,其中包含一系列歌曲(目前数组为空,因为用户需要输入歌曲,这是在另一个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.
}
我也试过转储数组,但我仍然得到值类型错误。
答案 0 :(得分:1)
加入了一个" ed"在它上面,并且在swift中表示是副本,而不是变异方法。您必须分配它返回的副本,如:
songList_lbl.text = multiLineString.joined(分隔符:" \ n")