数组元素中迅速检查 [英] Array element checking in swift

查看:120
本文介绍了数组元素中迅速检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前实施最爱功能到我的应用程序,它是基于报价。我想为用户是否已经保存一个元素的数组执行检查。如果他们有,我会改变一个忙按钮unfavour按钮。结果
进口的UIKit

 类DreamViewController:UIViewController的{    @IBOutlet VAR liketext:UIButton的!
    @IBOutlet VAR QuoteImage:UIImageView的!
    @IBOutlet弱VAR DreamLabel:的UILabel!
    VAR计数器= 1    VAR factIndex = 0    让dreamFact =梦()    覆盖FUNC viewDidLoad中(){
        super.viewDidLoad()        //在载入视图后的任何额外的设置。
        DreamLabel.text = dreamFact.dreamArray [factIndex]
    }    覆盖FUNC didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        可以重新创建任何资源的处置//。
    }
    @IBAction FUNC LeftSwipe(){
        factIndex--
        变种数= dreamFact.dreamArray.count - 1
        如果(factIndex℃,){
            self.factIndex =号        }
        DreamLabel.text = dreamFact.dreamArray [factIndex]
        如果计数器== 1 {            计数器= 36        }其他{            self.counter--        }        QuoteImage.image =的UIImage(命名为:框架\\(计数器).JPG)    }
@IBAction FUNC RightSwipe(){
        factIndex ++
        如果factIndex> = {dreamFact.dreamArray.count
            self.factIndex = 0
        }
        DreamLabel.text = dreamFact.dreamArray [factIndex]
    如果计数器== {36    计数器= 1    }其他{    self.counter ++    }
    QuoteImage.image =的UIImage(命名为:框架\\(计数器).JPG)
}
    结构常数{
        静态设默认= NSUserDefaults.standardUserDefaults()
        静态让fqKey =FavoriteQuotes
        静态让喜欢=赞
        不像=静态令不同于
    }
    类DreamViewController {
        VAR favouriteQuotesArray = Constants.defaults.objectForKey(Constants.fqKey)作为? [字符串]? [字符串](){
            didSet {
                Constants.defaults.setObject(favouriteQuotesArray,forKey:Constants.fqKey)
            }    }         @IBOutlet弱VAR likeButton:UIButton的! {            didSet {
                让喜爱= dreamArray [factIndex]
                如果让foundIndex =找到(favouriteQuotesArray,最喜欢的){
                    likeButton.setTitle(Constants.unlike,forState:。正常)
                }
                其他{
                    likeButton.setTitle(Constants.like,forState:。正常)
                }
            }        }        @IBAction FUNC likeButtonClicked(发件人:UIButton的){
        }        }
}

一经文本的依赖是不可靠的,因为如果用户重新打开应用程序,该元素仍然被保存了与不像默认起步的。如果元件阵列中找到。 如何将我的favouriteQuotesArray内进行搜索,如果存在类似元素

 如果elementfound声明{button.settitle(不同于)
removearrayatindex声明。}其他{
button.settitle(喜欢)
makeQuoteFavourite()
}

如何将数组检查语句是什么样子?
P.S如果可以请给建设删除类的提示?
更新:

  VAR favouriteQuotesArray:[字符串] = NSUserDefaults.standardUserDefaults()objectForKey(thekey)作为! [串]    FUNC makeQuoteFavourite(){
        如果favouriteQuotesArray == {为零
            favouriteQuotesArray = []
        }
        让CURRENTQUOTE = dreamFact.dreamArray [factIndex]
        favouriteQuotesArray.append(CURRENTQUOTE)
        NSUserDefaults.standardUserDefaults()的setObject(favouriteQuotesArray,forKey:thekey)。
    }
    @IBAction FUNC likeButton(){        如果含有(favouriteQuotesArray,dreamFact.dreamArray [factIndex]){
        liketext.setTitle(不像,forState:UIControlState.Normal)
        }


解决方案

做这种方式;一些清洁编码技术,通过削减包括废墟。

favouriteQuotesArray的声明是一个金色的星星单行。得到了这一切?

 进口的UIKit结构常数{
    静态设默认= NSUserDefaults.standardUserDefaults()
    静态让fqKey =FavoriteQuotes
    静态让喜欢=赞
    不像=静态令不同于
}类Page2ViewController:UIViewController的{
    VAR dreamArray = [字符串]()//占位如此类的编译
    VAR factIndex = 0 //占位如此类的编译    VAR favouriteQuotesArray = Constants.defaults.objectForKey(Constants.fqKey)作为? [字符串]? [字符串](){
        didSet {
            Constants.defaults.setObject(favouriteQuotesArray,forKey:Constants.fqKey)
        }
    }    @IBOutlet弱VAR likeButton:UIButton的! {
        didSet {
            让喜爱= dreamArray [factIndex]
            如果让foundIndex =找到(favouriteQuotesArray,最喜欢的){
                likeButton.setTitle(Constants.unlike,forState:。正常)
            }
            其他{
                likeButton.setTitle(Constants.like,forState:。正常)
            }
        }
    }    @IBAction FUNC likeButtonClicked(发件人:UIButton的){
        让喜爱= dreamArray [factIndex]
        如果让foundIndex =找到(favouriteQuotesArray,最喜欢的){
            sender.setTitle(Constants.like,forState:。正常)
            favouriteQuotesArray.removeAtIndex(foundIndex)
        }
        其他{
            sender.setTitle(Constants.unlike,forState:。正常)
            favouriteQuotesArray.append(喜爱)
        }
    }
}

Currently implementing a favourite function to my application which is based on quotes. I would like to perform a check for if the user has already saved an element to an array. If they have, I would change a favour button to unfavour button.
import 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) {
        }

        }
}

The dependency on upon the text which is unreliable because if the user reopens the app, the element would still be saved instead of starting out with the default unlike. If the element is found in the array. How would I perform a search inside my favouriteQuotesArray if the element exists like:

if elementfound statement {

button.settitle(unlike)
removearrayatindex statement.

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

How would the array checking statement look like? P.S If you can please give tips on building the delete class? Update:

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

解决方案

Do it this way; some cleaner coding techniques to cut through the rubble included.

The declaration of favouriteQuotesArray is a gold star one liner. Got it all?

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

这篇关于数组元素中迅速检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆