尝试创建一个应用程序,我可以根据感受对书籍进行分类。我可以选择幸福、恐惧、悲伤等类别,然后会显示特定的文本和章节,我可以水平滚动浏览每个类别。我现在需要做的就是找到一种更简单的方法来从类中获取章节和文本,并创建一个函数,在其中我可以在 UIScrollView
中单独调用章节和类.
我有一个 BookBank
模型和一个 HappyText
模型。
快乐文本模型是我将所有内容都存储在快乐中的地方。
我需要找到一种方法来单独调出文本和章节,以便我可以获取该信息并将其显示在 UIScrollView
上。
class BookBank {
let chapter : String
let verse : String
init(chapter: String, verse: String) {
self.chapter = chapter
self.verse = verse
}
}
class HappyText {
var list = [BookBank]()
init() {
list.append(BookBank(chapter: "Chapter 1", text: "And he rejoiced in happiness."))
list.append(BookBank(chapter: "Chapter 1", text: "The business was doing well and his health was improving."))
list.append(BookBank(chapter: "Chapter 2", text: "John walked down to the harbor to greet his wife and had the biggest smile on his face"))
}
}
class FeatureViewVC: UIViewController {
var featureArray = [String]()
let book = HappyText()
var bookChapter = [String]()
var bookText = [String]()
override func viewDidLoad() {
super.viewDidLoad()
if feelingLabel.text == "Happiness" {
featureArray = []
} else if feelingLabel.text == "Fear" {
featureArray = []
}
}
func loadText() {
var bookChapter = [book.list(chapter)]
}
}
最佳答案
根据我的评论,我将在这里为您提供一个代码示例。
首先,在您的 BookBank 模型中,您有 2 个参数:chapter
和 verse
。但是,当您在 HappyText 模型中创建列表时,在初始化 BookBank 时,您会给出 text
而不是 verse
。我也看不到 FeelingLabel 标签的任何导出集合,但我想您只是没有将其添加到问题示例中。
无论如何,loadText()
方法的工作示例:
func loadText() {
//instead of index you need to put an Int number, like here:
var bookChapter = [book.list[1].chapter]
var bookVerse = [book.list[1].verse]
print("Chapter: \(bookChapter), verse: \(bookVerse)")
}
现在调用loadText()
会打印你:
Chapter: ["Chapter 1"], verse: ["The business was doing well and his health was improving."]
如果您收到此处提到的错误:
error: cannot convert value of type '(Any) -> Int' to expected argument type '(UnboundedRange_) -> ()'
然后你用“索引”一词代替索引号。索引号是一个 int,就像数组一样。如果列表中有 3 个元素,则列表中的第一个元素的索引 = 0。下一个元素是 1、2...,依此类推。检查此链接:swift arrays
关于ios - 创建一个类并将信息存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577361/