在swift中检查数组元素

时间:2015-08-01 03:46:21

标签: ios arrays swift

目前在我的应用程序中实现了一个基于引号的收藏夹功能。我想检查用户是否已将元素保存到数组中。如果他们有,我会改变一个支持按钮以不喜欢按钮 导入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)
        }

3 个答案:

答案 0 :(得分:3)

这样做;一些清洁的编码技术,以切断瓦砾。

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)
        }
    }
}

答案 1 :(得分:1)

试试这个:

let currentQuote = dreamFact.dreamArray[factIndex]
if find(favouriteQuotesArray!, currentQuote) != nil {
    //Do something
}

答案 2 :(得分:1)

找出数组中元素的更好,更快捷的方法是:

if contains(yourFavArray, stringStatement){
  //Contains you can write your logic
}

更改您的favouriteArray如下:

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

编辑:

 if let foundIndex = find(favouriteQuotesArray, dreamFact.dreamArray[factIndex]) {
     sender.setTitle(Constants.like, forState: .Normal)
     favouriteQuotesArray.removeAtIndex(foundIndex)
 }