添加多个数组以形成一个最终数组。调试问题,Swift Xcode

时间:2017-08-01 14:04:34

标签: ios arrays swift xcode debugging

我正在尝试创建一个flashcard应用程序。我也成功地获得了应用程序    有11个不同的抽认卡阵列和所有的闪存卡    这些数组添加到一个最终数组,然后我可以刷卡   通过。正如你所看到的那样,每个小组的结尾都是“active:true”   这是因为我有一个设置页面可以打开和关闭每个单词组。

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

但是我需要在每张闪存卡中添加一个声音文件,这样无论何时敲击卡片播放音频文件,我都会尝试更改代码,以便每组中的每个单词都与音频文件相结合。但是,我无法让这个新代码顺利运行,它充满了bug。你们中的任何人都可以看到这个新代码有什么问题吗?当我运行新代码时,我应该能够像原始代码一样刷过所有图片,但目前这是不可能的。任何帮助将非常感激。谢谢! (下面的新代码)

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: "lamb")!, soundUrl: "Lamb"),
    Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"),
    Card(image: UIImage(named: "lark")!, soundUrl: "Lark"),
    Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"),
    Card(image: UIImage(named: "leash")!, soundUrl: "Leash"),
    Card(image: UIImage(named: "left")!, soundUrl: "Left"),
    Card(image: UIImage(named: "leg")!, soundUrl: "Leg"),
    Card(image: UIImage(named: "lime")!, soundUrl: "Lime"),
    Card(image: UIImage(named: "lion")!, soundUrl: "Lion"),
    Card(image: UIImage(named: "lips")!, soundUrl: "Lips"),
    Card(image: UIImage(named: "list")!, soundUrl: "List"),
    Card(image: UIImage(named: "lock")!, soundUrl: "Lock"),
    Card(image: UIImage(named: "log")!, soundUrl: "Log"),
    Card(image: UIImage(named: "look")!, soundUrl: "Look"),
    Card(image: UIImage(named: "love")!, soundUrl: "Love"),
    Card(image: UIImage(named: "lunch")!, soundUrl: "Lunch")
        ]

let secondList:[Card] = [
    Card(image: UIImage(named: "lake")!, soundUrl: "Lake"),
    Card(image: UIImage(named: "lamb")!, soundUrl: "Lamb"),
    Card(image: UIImage(named: "lamp")!, soundUrl: "Lamp"),
    Card(image: UIImage(named: "lark")!, soundUrl: "Lark"),
    Card(image: UIImage(named: "leaf")!, soundUrl: "Leaf"),

    ]

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

let list1 = List(words:firstList, active: true)
let list2 = List(words:secondList, active: true)

var imageList: [String]{
let wordLists = [list1, list2]

print((wordLists[0] as! List).words[0].soundurl)



    override func viewDidLoad() {
    super.viewDidLoad()



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

    imgPhoto.image = (imageList[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)

}

@IBAction func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    print("hhh")
    imageList[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 = words.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.
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

你还没有详细说明这些错误是什么。但是,从我在您粘贴的代码中看到的内容 - 主要更改是声音文件加上启用isUserInteractionEnabled

我没有看到playSound实现,但我在这里附上我在Swift3中经常使用的片段,每当我需要播放声音时 - 它工作正常:(不要忘记放{{ 1}}也)

import AVFoundation

如果它仍然没有顺利运行,当你在tap中调用它时,尝试将playSound放在后台线程中:DispatchQueue.global()。async {self.playSound()})

如果它根本不是声音 - 提供一些关于错误的数据..

祝你好运