除了数组之外,我无法获取UIImage的任何内容。我尝试了几种不同的选择,包括创建var或在其中放置图片名称,但我似乎无法接受。

@IBAction func displayPressed(_ sender: Any) {
    externalPicture.image = UIImage (named: externalArray [1])
    seatingSetup.image = UIImage (named: seatingArray ["\(forPicture())" + "seating"])
}

func forPicture() -> String {
    var aircraftFullName = ""
    let manuf = aircraftType.selectedRow(inComponent: 0)
    let model = aircraftType.selectedRow(inComponent: 1)
    if manuf < aircraft.count {
        aircraftFullName = aircraft[manuf]
        if let aircraftModels =  models[aircraftFullName] {
            if model < aircraftModels.count {
                aircraftFullName = aircraftFullName  + " " + aircraftModels[model]
            }
        }
    }
    selectedAircraft.text = aircraftFullName

    if aircraftFullName != "0" {}
    return "\(aircraftFullName)"
}

let aircraft = ["Airbus", "Boeing", "Bombardier", "Embraer"]
var models = [ "Airbus": ["A300", "A320", "A330", "A340", "A350", "A380"], "Boeing": ["B737", "B747", "B757", "767", "777", "787"], "Bombardier": ["CRJ200", "CRJ700/900"], "Embraer": ["ERJ145", "ERJ170", "ERJ190"] ]

var externalArray = ["320outside", "747outside"]
var seatingArray = ["AirbusA320seating", "BoeingB747seating"]

最佳答案

您可以尝试将其转换

aircraftFullName = aircraftFullName  + " " + aircraftModels[model]




aircraftFullName = aircraftFullName + aircraftModels[model]


编辑:根据您的forPicture()方法,图像应类似于此Airbus A320seating,但对于AirbusA320seating,您需要删除上述提到的空间

seatingSetup.image = UIImage (named:"\(forPicture())" + "seating")


//

如果要保留空间以在文本字段中显示,则可以创建其他var并像这样返回它

func forPicture() -> String {
    var aircraftFullName = ""
    var aircraftFullNameNoSpace = ""
    let manuf = aircraftType.selectedRow(inComponent: 0)
    let model = aircraftType.selectedRow(inComponent: 1)
    if manuf < aircraft.count {
        aircraftFullName = aircraft[manuf]
        if let aircraftModels =  models[aircraftFullName] {
            if model < aircraftModels.count {
                aircraftFullName = aircraftFullName  + " " + aircraftModels[model]
                aircraftFullNameNoSpace = aircraftFullName + aircraftModels[model]
            }
        }
    }
    selectedAircraft.text = aircraftFullName

    if aircraftFullName != "0" {}
    return aircraftFullNameNoSpace
}

关于swift - UIImage除了数组外什么也不会,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51340367/

10-12 06:28