如果没有更多的上下文,表达的类型是不明确的。 xcode swift

时间:2017-08-03 19:09:55

标签: ios arrays swift xcode debugging

我正在构建一个flashcard应用程序。我在下面附上了我的代码。当下面的代码运行时,用户应该能够向左和向右滑动一系列卡片(每张卡片包含一个与声音相结合的图像。有两个名为firstList和secondList的卡片阵列)。轻触图像时,应该播放与之耦合的声音。我没有在播放声音或刷一个列表时遇到问题。但是,当我尝试组合并滑动" firstList"时,我收到错误。和" secondList"。我似乎无法弄清楚的一个错误是"表达式的类型是不明确的,没有更多的上下文"。请参阅下面的代码和错误

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(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(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"),

    ]




    override func viewDidLoad() {
    super.viewDidLoad()

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

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

            print((wordLists[0] ).words[0].soundurl)


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

                } else {
                    return result
                }
            }

            return active

        }


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

enter image description here

import Foundation; import UIKit; import AVFoundation

var player: AVAudioPlayer?

class Card: NSObject
{
var image: UIImage
var soundUrl: String


init(image: UIImage, soundUrl: String) {
    self.image = image
    self.soundUrl = soundUrl
}
 func playSound()

{ print("play")
    guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
    do
    {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)

        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.prepareToPlay()
        player.play()
    print("hhh")
    } catch let error {
        print(error.localizedDescription)
    }
}
}

1 个答案:

答案 0 :(得分:0)

result的类型为[String],但list.words的类型为[Card]。 Swift不知道你想如何在声明return result + list.words中将这两者结合起来。

Swift要求您对类型非常具体。因此,当有任何问题时,您需要准确指定所需内容。

您没有显示Card类型,因此我将为您提供一般示例:

var list1:[String] = ["a", "b", "c"]
var list2:[Any] = ["d", "e", "f"]

let newList = list1 + list2
// error: type of expression is ambiguous without more context

这种组合应该是可能的,因为两个列表都包含String个值。但是,由于list2声明它的类型为[Any],因此Swift不确定连接的意图。它会给你“模棱两可”的错误。

要解决此问题,您必须具体说明相关类型 - 这意味着您必须根据您希望Swift使用的类型进行转换:

let newList = list1 + (list2 as! [String])

由于list1已经是[String]类型,因此Swift现在知道您想要生成[String]类型。 (在这种情况下,Swift将隐式指定[String]作为newList的类型。)

另请注意,您的类型必须兼容才能执行此操作!如果[Card]不能转换为[String],则会收到其他错误。