我怎么能把这个变量赋值函数变成一个循环?

时间:2021-07-20 23:39:36

标签: swift for-loop switch-statement

我已经注明了重复的地方。一个变量进入函数,并根据它的部分值,通过这个函数路由。然而,同样的事情需要用不同的变量重复,否则相同......很多次。

有没有更巧妙的方法来做到这一点?我无法通过 for 循环弄清楚。

    case _ where message.addressParts[0] == "GigPerformer" :
        if message.addressParts[1] == "Main"{
            if message.addressParts[2] == "Voice"{
                if message.addressParts[3] == "CurrentSongName"{
                    gpMainVoice.gpSongNumber = "\(message.arguments.first ?? error)"
                    gpMainVoice.gpSongName = "\(message.arguments.last ?? error)"
                }
                if message.addressParts[3] == "CurrentSongPartName"{
                    gpMainVoice.gpCurrentSongPartNumber = "\(message.arguments.first ?? error)"
                    gpMainVoice.gpCurrentSongPartName = "\(message.arguments.last ?? error)"
                }
                if message.addressParts[3] == "SongPartName"{
                        for i in gpMainVoice.gpSongPartNumber.indices {
                            if "\(message.arguments.first ?? error)" == String(i) {
                                gpMainVoice.gpSongPartNumber[i] = "\(message.arguments.first ?? error)"
                                gpMainVoice.gpSongPartName[i] = "\(message.arguments.last ?? error)"
                            break
                        }
                    }
                }
            }

//这里是重复的地方

        if message.addressParts[2] == "Amp"{
            if message.addressParts[3] == "CurrentSongName"{
                gpMainAmp.gpSongNumber = "\(message.arguments.first ?? error)"
                gpMainAmp.gpSongName = "\(message.arguments.last ?? error)"
            }
            if message.addressParts[3] == "CurrentSongPartName"{
                gpMainAmp.gpCurrentSongPartNumber = "\(message.arguments.first ?? error)"
                gpMainAmp.gpCurrentSongPartName = "\(message.arguments.last ?? error)"
            }
            if message.addressParts[3] == "SongPartName"{
                    for i in gpMainAmp.gpSongPartNumber.indices {
                        if "\(message.arguments.first ?? error)" == String(i) {
                            gpMainAmp.gpSongPartNumber[i] = "\(message.arguments.first ?? error)"
                            gpMainAmp.gpSongPartName[i] = "\(message.arguments.last ?? error)"
                        break
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您能否将重复的代码放入一个函数中并调用该函数?大致是这样的:

func song_information(message, gpMainVoice) {
                if message.addressParts[3] == "CurrentSongName"{
                    gpMainVoice.gpSongNumber = "\(message.arguments.first ?? error)"
                    gpMainVoice.gpSongName = "\(message.arguments.last ?? error)"
                }
                if message.addressParts[3] == "CurrentSongPartName"{
                    gpMainVoice.gpCurrentSongPartNumber = "\(message.arguments.first ?? error)"
                    gpMainVoice.gpCurrentSongPartName = "\(message.arguments.last ?? error)"
                }
                if message.addressParts[3] == "SongPartName"{
                        for i in gpMainVoice.gpSongPartNumber.indices {
                            if "\(message.arguments.first ?? error)" == String(i) {
                                gpMainVoice.gpSongPartNumber[i] = "\(message.arguments.first ?? error)"
                                gpMainVoice.gpSongPartName[i] = "\(message.arguments.last ?? error)"
                            break
                        }
                    }
                }
}

然后

if message.addressParts[2] == "Voice"{
     song_information(message, gpMainVoice)
}
...
if message.addressParts[2] == "Amp"{
     song_information(message, gpMainVoice)
}

答案 1 :(得分:0)

感谢@purple 的快速回答。

        func gpInput (message: OSCMessage, instance:inout GpModel) {
        if message.addressParts[3] == "CurrentSongName"{
            instance.gpSongNumber = "\(message.arguments.first ?? error)"
            instance.gpSongName = "\(message.arguments.first ?? error)"
        }
        if message.addressParts[3] == "CurrentSongPartName"{
            instance.gpCurrentSongPartNumber = "\(message.arguments.first ?? error)"
            instance.gpCurrentSongPartName = "\(message.arguments.last ?? error)"
        }
        if message.addressParts[3] == "SongPartName"{
                for i in gpMainVoice.gpSongPartNumber.indices {
                    if "\(message.arguments.first ?? error)" == String(i) {
                        instance.gpSongPartNumber[i] = "\(message.arguments.first ?? error)"
                        instance.gpSongPartName[i] = "\(message.arguments.last ?? error)"
                    break
                }
            }
        }
            
        }
        if message.addressParts[1] == "Main"{
            if message.addressParts[2] == "Voice"{
                gpInput(message: message, instance: &gpMainVoice)
            }
        }