gpt4 book ai didi

ios - 快速检查数组元素

转载 作者:搜寻专家 更新时间:2023-11-01 06:08:12 25 4
gpt4 key购买 nike

目前正在为我的应用程序实现一个基于引号的最喜欢的功能。我想检查用户是否已经将元素保存到数组中。如果他们有,我会将赞成按钮更改为不喜欢按钮。
导入 UIKit

class DreamViewController: UIViewController {

@IBOutlet var liketext: UIButton!
@IBOutlet var QuoteImage: UIImageView!
@IBOutlet weak var DreamLabel: UILabel!
var counter = 1

var factIndex = 0

let dreamFact = Dream()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
DreamLabel.text = dreamFact.dreamArray[factIndex]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func LeftSwipe() {
factIndex--
var number = dreamFact.dreamArray.count - 1
if (factIndex < 0){
self.factIndex = number

}
DreamLabel.text = dreamFact.dreamArray[factIndex]
if counter == 1 {

counter = 36

} else {

self.counter--

}

QuoteImage.image = UIImage(named: "frame\(counter).jpg")

}
@IBAction func RightSwipe() {
factIndex++
if factIndex >= dreamFact.dreamArray.count {
self.factIndex = 0
}
DreamLabel.text = dreamFact.dreamArray[factIndex]
if counter == 36 {

counter = 1

} else {

self.counter++

}
QuoteImage.image = UIImage(named: "frame\(counter).jpg")
}
struct Constants {
static let defaults = NSUserDefaults.standardUserDefaults()
static let fqKey = "FavoriteQuotes"
static let like = "Like"
static let unlike = "Unlike"
}
class DreamViewController {




var favouriteQuotesArray = Constants.defaults.objectForKey(Constants.fqKey) as? [String] ?? [String]() {
didSet {
Constants.defaults.setObject(favouriteQuotesArray, forKey: Constants.fqKey)
}



}

@IBOutlet weak var likeButton: UIButton! {

didSet {
let favourite = dreamArray[factIndex]
if let foundIndex = find(favouriteQuotesArray, favourite) {
likeButton.setTitle(Constants.unlike, forState: .Normal)
}
else {
likeButton.setTitle(Constants.like, forState: .Normal)
}
}



}

@IBAction func likeButtonClicked(sender: UIButton) {
}

}
}

对文本的依赖是不可靠的,因为如果用户重新打开应用程序,元素仍会被保存,而不是从默认值开始,这与不同。如果在数组中找到该元素。 如果该元素存在,我将如何在我的 favouriteQuotesArray 中执行搜索:

if elementfound statement {

button.settitle(unlike)
removearrayatindex statement.

} else {
button.settitle(like)
makeQuoteFavourite()
}

数组检查语句会是什么样子?P.S 如果可以,请提供有关构建删除类的提示?更新:

 var favouriteQuotesArray: [String] = NSUserDefaults.standardUserDefaults().objectForKey("thekey") as! [String]

func makeQuoteFavourite() {
if favouriteQuotesArray == nil {
favouriteQuotesArray = []
}
let currentQuote = dreamFact.dreamArray[factIndex]
favouriteQuotesArray.append(currentQuote)
NSUserDefaults.standardUserDefaults().setObject(favouriteQuotesArray, forKey: "thekey")
}
@IBAction func likeButton() {

if contains(favouriteQuotesArray, dreamFact.dreamArray[factIndex]) {
liketext.setTitle("Unlike", forState: UIControlState.Normal)
}

最佳答案

这样做;一些更清晰的编码技术,以消除包含的废墟。

favouriteQuotesArray 的声明是一个金星单行。明白了吗?

import UIKit

struct Constants {
static let defaults = NSUserDefaults.standardUserDefaults()
static let fqKey = "FavoriteQuotes"
static let like = "Like"
static let unlike = "Unlike"
}

class Page2ViewController: UIViewController {
var dreamArray = [String]() // place holder so class compiles
var factIndex = 0 // place holder so class compiles

var favouriteQuotesArray = Constants.defaults.objectForKey(Constants.fqKey) as? [String] ?? [String]() {
didSet {
Constants.defaults.setObject(favouriteQuotesArray, forKey: Constants.fqKey)
}
}

@IBOutlet weak var likeButton: UIButton! {
didSet {
let favourite = dreamArray[factIndex]
if let foundIndex = find(favouriteQuotesArray, favourite) {
likeButton.setTitle(Constants.unlike, forState: .Normal)
}
else {
likeButton.setTitle(Constants.like, forState: .Normal)
}
}
}

@IBAction func likeButtonClicked(sender: UIButton) {
let favourite = dreamArray[factIndex]
if let foundIndex = find(favouriteQuotesArray, favourite) {
sender.setTitle(Constants.like, forState: .Normal)
favouriteQuotesArray.removeAtIndex(foundIndex)
}
else {
sender.setTitle(Constants.unlike, forState: .Normal)
favouriteQuotesArray.append(favourite)
}
}
}

关于ios - 快速检查数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757991/

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