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

How could I make this variable assignment function into a loop?

我在重复的地方做了标记。一个变量进入函数,并根据其值的一部分,通过该函数进行路由。然而,同样的事情需要用不同的变量重复,否则相同......很多次。

有没有更简单的方法来做到这一点?我无法用 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
                    }
                }
            }
        }
    }

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

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

感谢@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)
            }
        }