Swift - Write program to create a variadic function

Requirements

create a variadic function called cat () using default parameters that can be called in the following manner with the outputs shown:

print(cat(joiner:":", nums: 1,2,3,4,5,6,7,8,9))
// 1:2:3:4:5:6:7:8:9

print(cat(nums: 1,2,3,4,5))
// 1 2 3 4 5

Demo

func cat(joiner:String = " ", nums: Int...) -> String {
        var str = ""
        for (index, num) in enumerate(nums) {
            str = str + String(num)
            if index != nums.count - 1 {
                str += joiner//from   w  ww  .  jav a 2  s  . co  m
            }
        }
        return str
}

Related Exercise