我目前正在我的应用程序中实现类似的功能,我似乎无法获得此追加和检索工作。这是我在我的ViewController文件中找到的Button Action
var Liked = Favourite()
let factBook = FactBook()
@IBAction func favour() {
var currentQuote = factBook.factsArray[factIndex]
Liked.favouriteArray.append(currentQuote)
}
从
调用Favorite结构 import Foundation
struct Favourite {
var favouriteArray: [String] = []
}
(factBook结构是相同的,除了数组实际上有元素。)
现在我的目标是让所有这些显示在名为favouriteViewController的单独视图控制器上:
import UIKit
class FavouriteViewController: UIViewController {
@IBOutlet var LikeQuote: UILabel!
var liked = Favourite()
override func viewDidLoad() {
super.viewDidLoad()
if liked.favouriteArray.count > 0 {
LikeQuote.text = liked.favouriteArray[0]
} else if liked.favouriteArray.count == 0 {
LikeQuote.text = "No Liked Quotes Found, Go Favour Some!"
}
}
现在当我按下按钮时,理论上我应该可以将它附加到favouriteArray 然后能够在我的favouriteViewController文件中显示它,但是当我保存它然后打开viewcontroller文件默认为likes.favouriteArray.count = 0 方案,无论我保存多少引号,都会打印出文本。我只是想知道这个过程中出了什么问题?
更新:如果我将Hello world附加到文本中,它仍然不会附加到元素并将数组值计算为0.
答案 0 :(得分:0)
问题在于,因为您没有将数据保存到数据库或文件中,所以您将无法检索它,因此您添加到 viewDidLoad中的代码将无法正常工作唯一可行的方法是点击UIButton时查看下面的代码。希望有助于
import Foundation
import UIKit
struct Favourite {
var favouriteArray = [String]()
}
class FavouriteViewController: UIViewController {
// let factBook = FactBook()
@IBOutlet var LikeQuote: UILabel!
var liked = Favourite()
override func viewDidLoad() {
super.viewDidLoad()
}
func updateContent(){
if liked.favouriteArray.count > 0 {
LikeQuote.text = liked.favouriteArray[0]
}
else if liked.favouriteArray.count == 0 {
LikeQuote.text = "No Liked Quotes Found, Go Favour Some!"
}
}
@IBAction func favour() {
// var currentQuote = liked.factsArray[factIndex]
// Liked.favouriteArray.append(currentQuote)
liked.favouriteArray.append("today is a new day ")
updateContent()
}
}