尝试创建一个应用程序,我可以根据感受对书籍进行分类。我可以选择喜欢,恐惧,悲伤等类别,然后特定的文本和章节显示我可以水平滚动每个文本和章节。我需要做的就是找到一种更简单的方法从类中获取章节和文本并创建一个函数,我可以在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)]
}
}
答案 0 :(得分:0)
根据我的评论,我会在这里给你一个代码示例。
首先,在您的BookBank模型中,您有2个参数:chapter
和verse
。但是当您在HappyText模型中创建列表时,在初始化BookBank时,您需要text
而不是verse
。我也看不到感觉标签的任何插座集合,但我想你在问题示例中没有添加它。
无论如何,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