我有一个简单的viewcontroller,它显示两个标签,一个带引号,另一个带作者。引号和作者放在一个NSObject中(参见代码),显示的引用/作者是随机的。我的问题是如何在特定标签中显示引用和作者。
class Quotes: NSObject {
var person : String
var quote : String
init(person : String, quote : String) {
self.person = person
self.quote = quote
}
}
let quotes = [
Quotes(person: "Person1", quote: "Quote1"),
Quotes(person: "Person2", quote: "Quote2"),
Quotes(person: "Person3", quote: "Quote3" ), ]
override func viewDidLoad() {
super.viewDidLoad()
let range: UInt32 = UInt32(quotes.count)
let randomNumber = Int(arc4random_uniform(range))
let QuoteString = quotes.person.objectAtIndex(randomNumber) //ERROR here, how do I specify the choosen author from the randomization?
let AuthorString = quotes.quote.objectAtIndex(randomNumber)//ERROR here, how do I specify the choosen quote from the randomization?
self.quoteLabel.text = QuoteString as? String
self.personLabel.text = NameString as? String
}
我认为这是一个非常简单的解决方案,非常感谢帮助(我是编程新手)
答案 0 :(得分:1)
你有你的阵列&财产准入略有混淆。首先,从数组中访问Quote
对象,然后访问对象的属性
let randomNumber = Int(arc4random_uniform(self.quotes.count))
let quote = self.quotes[randomNumber]
let quoteString = quote.quote
let authorString = quote.author
self.quoteLabel.text = quoteString
self.personLabel.text = authorString