Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

ios - Adding multiple arrays to form one final array. Debug swift xcode

I am trying to create a flashcard app. I successfully got the app too the point where there were 11 different arrays of flashcards and all of these arrays added up to one final array which I could then swipe through. As you can see each group has "active: true" at the end of it. This is because I have a settings page to turn each word group on and off.

import UIKit

class SecondViewController: UIViewController , UIGestureRecognizerDelegate  {

@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}

@IBOutlet weak var imgPhoto: UIImageView!

struct List {
let words: [String]
var active: Bool
}

let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)

let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)

let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)

let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)

let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)

let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)

let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)

let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)

let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)

let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)

let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)

var imageIndex: Int = 0

var imageList: [String] {

let wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]



let active = wordLists.reduce([]) { (result:[String], list:List) in
    if list.active {
        return result + list.words

    } else {
        return result
    }
}

return active

}




override func viewDidLoad() {
super.viewDidLoad()

imgPhoto.image = UIImage(named: imageList[imageIndex])

// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false

let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false

leftSwipe.direction = .left
rightSwipe.direction = .right

view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)

}

func Swiped(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

    switch swipeGesture.direction {

    case UISwipeGestureRecognizerDirection.right :
        print("User swiped right")

        // decrease index first

        imageIndex -= 1

        // check if index is in range

        if imageIndex < 0 {

            imageIndex = imageList.count - 1

        }

        imgPhoto.image = UIImage(named: imageList[imageIndex])

    case UISwipeGestureRecognizerDirection.left:
        print("User swiped Left")

        // increase index first

        imageIndex += 1

        // check if index is in range

        if imageIndex > imageList.count - 1 {

            imageIndex = 0

        }

        imgPhoto.image = UIImage(named: imageList[imageIndex])

    default:
        break //stops the code/codes nothing.
    }
}
}
}

but I needed to add a sound file to each flashcard so that whenever the card is tapped an audio file plays, so I changed the code so that each card in each group is coupled with an audio file. However, I have not been able to get this new code to run smooth, it is riddled with bugs. I have posted the bugs below. When I run the new code I should be able to swipe through all of the pictures just like my original code, however this is currently not possible (new code is below)

import UIKit

class SecondViewController: UIViewController , UIGestureRecognizerDelegate  {

var imageIndex: Int = 0
@IBAction func home(_ sender: Any) {
    performSegue(withIdentifier: "home", sender: self)
}

@IBOutlet weak var imgPhoto: UIImageView!

struct List {
    let words: [Card] /*Create array of cards*/
    var active: Bool
}



let firstList:[Card] = [
    Card(image: UIImage(named: "lake")!, soundUrl: "lake"),
    Card(image: UIImage(named: "river")!, soundUrl: "river"),
    Card(image: UIImage(named: "ocean")!, soundUrl: "ocean")
]

let secondList:[Card] = [
    Card(image: UIImage(named: "alligator")!, soundUrl: "alligator"),
    Card(image: UIImage(named: "apple")!, soundUrl: "apple"),
    Card(image: UIImage(named: "grape")!, soundUrl: "grape")
]

override func viewDidLoad() {


    var imageList: [String] {
        let list1 = List(words:firstList, active: true)
        let list2 = List(words:secondList, active: true)

        let wordLists = [list1, list2]

        let active = wordLists.reduce([]) { (result:[String], list:List) in
            if list.active {
                return result + list.words

            } else {
                return result
            }
        }

        return active

    }

    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    imgPhoto.isUserInteractionEnabled = true
    imgPhoto.addGestureRecognizer(tapGestureRecognizer)


    imgPhoto.image = (wordLists)[0]; ).image

    // Do any additional setup after loading the view.
    imgPhoto.isUserInteractionEnabled = true

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
    leftSwipe.cancelsTouchesInView = false

    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
    rightSwipe.cancelsTouchesInView = false

    leftSwipe.direction = .left
    rightSwipe.direction = .right

    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)

}


func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{

    itemList[imageIndex].playSound()
    // Your action
}
func Swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.right :
            print("User swiped right")

            // decrease index first

            imageIndex -= 1

            // check if index is in range

            if imageIndex < 0 {

                imageIndex = itemList.count - 1

            }

            imgPhoto.image = itemList[imageIndex].image

        case UISwipeGestureRecognizerDirection.left:
            print("User swiped Left")

            // increase index first

            imageIndex += 1

            // check if index is in range

            if imageIndex > itemList.count - 1 {

                imageIndex = 0

            }

            imgPhoto.image = itemList[imageIndex].image
        default:


            break //stops the code/codes nothing.
        }
    }
}
}

error

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I could find following mistakes with the code.

  1. imageList is declared in viewDidLoad and is accessed in imageTapped & Swiped method. This method can't see imageList. You can make imageList class variable or pass it to imageTapped method as an argument.
  2. Also super.viewDidLoad must be called first before any code in your case, var imageList: [Any] is initialized before calling super.viewDidLoad
  3. return active as! [String] always fail as you can't convert [Any] to String. Image list has to be a list of Cards i.e. [Card]
  4. Keeping 3 in mind, I rewrote imageList code as follows. Hope this helps
let list1 = List(words:firstList, active: true)
let list2 = List(words:secondList, active: true)
let wordLists = [list1, list2]

var imageList: [Card] = []

for list in wordLists {
  if list.active{
    imageList.append(contentsOf: list.words)
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...