To split by the split method and then output each word separately.

var text = ["food - еда", "read - читать", "tea - чай"] let randomIndex = Int(arc4random_uniform(UInt32(text.count))) let separeteArr = text[randomIndex].characters.split{$0 == " - "}.map(String.init) separeteArr[0] // food (для примера) separeteArr[1] // еда (для примера) // выводится error: missing argument for parameter #1 in call 

    1 answer 1

    Not sure about the correctness. There is no pc on hand to check, but you do not correctly set the limit.

    I will explain. arc4random_uniform(5) - generates a random number in the range from 0 to 5 . Those. you have an array of three elements and you get arc4random_uniform(3) , a number in the range from 0 to 3 . But the indexes of the elements of your array 0,1,2 - you simply do not have an element with index 3 .

    Try this - let randomIndex = Int(arc4random_uniform(UInt32(text.count - 1)))

    • Not so, let randomIndex = Int (arc4random_uniform (UInt32 (text.count))) It is needed to derive a value from the array to get a random value, for example (food - food) And this line: let separeteArr = text [ randomIndex] .characters.split {$ 0 == "-"} .map (String.init) It must divide by 1 "-" the element obtained from randomIndex. - Pavel Anpleenko
    • Did you read my comment? I explained why you get a wrong index. - Jon Fir
    • And it all came thanks! - Pavel Anpleenko
    • The best thanks is to answer the question as a solution) - Jon Fir