gpt4 book ai didi

ios - 在标签中显示 NSObject init

转载 作者:行者123 更新时间:2023-11-29 00:56:49 24 4
gpt4 key购买 nike

我有一个简单的 View Controller ,它显示两个标签,一个带有引号,一个带有作者。引用和作者放在 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

}

我想这是一个非常简单的解决方案,非常感谢帮助(我是编程新手)

最佳答案

您的数组和属性访问有点混淆。首先,从数组中访问 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

关于ios - 在标签中显示 NSObject init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37544191/

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