Swift functions

var xo = [String]()

var xo1 = ["x" , "10" ,"10" ,"10","10"  ]

var xo2 = ["10 " , "x" ,"10" ,"10" ,"10" ]

var xo3 = ["10 " , "10" ,"x" ,"10" ,"10" ]

var xo4 = ["10 " , "10" ,"10" ,"x" ,"10"]

var xo5 = ["10 " , "10" ,"10" ,"10" ,"x" ]

xo.append(contentsOf: xo1)

xo.append(contentsOf: xo2)

xo.append(contentsOf: xo3)

xo.append(contentsOf: xo4)

xo.append(contentsOf: xo5)

//for _ in xo {

//    print(xo)

//}

func xos (item: Int , count:Int )  -> [String]  {

    let xo8 = Array(repeating: item, count: count)

    let xo9 = Array(repeating: item, count: count)

  

}



xos(item: xo, count: 1)



whats wrong ? how i can print like this :
[X, 0, 0, 0, 0]
[0, X, 0, 0, 0]
[0, 0, X, 0, 0]
[0, 0, 0, X, 0]
[0, 0, 0, 0, X]
Answered by Claude31 in 672748022
Your answers are absolutely confusing.

If you want a matrix in x0, declare

Code Block
var matrix = [[String]]() // Array of Arrays ; changed the name to be more meaningful

and populate as follows:
Code Block
var xo1 = ["x", "10","10","10","10"]
var xo2 = ["10 ","x", "10","10","10"]
var xo3 = ["10 ","10","x" ,"10","10"]
var xo4 = ["10 ","10","10","x" ,"10"]
var xo5 = ["10 ","10","10","10","x"]
matrix.append(xo1)
matrix.append(xo2)
matrix.append(xo3)
matrix.append(xo4)
matrix.append(xo5)

Then with
Code Block
print(matrix)
you get

[["x", "10", "10", "10", "10"], ["10 ", "x", "10", "10", "10"], ["10 ", "10", "x", "10", "10"], ["10 ", "10", "10", "x", "10"], ["10 ", "10", "10", "10", "x"]]

Or with:
Code Block
for row in matrix {
    print(row)
}
you get

["x", "10", "10", "10", "10"]
["10 ", "x", "10", "10", "10"]
["10 ", "10", "x", "10", "10"]
["10 ", "10", "10", "x", "10"]
["10 ", "10", "10", "10", "x"]

Is it what you are looking for ?
If so, don't forget to close the thread on this answer.
Otherwise, please explain what different output you want.
Your function xos(item:count:) takes Int for item:.

Your function call xos(item: xo, count: 1) is passing an Array (not Int) to item:.

What sort of function do you want to write? A function taking Int and Int? Or a function taking Array and Int?
[X, 0, 0, 0, 0]
[0, X, 0, 0, 0]
[0, 0, X, 0, 0]
[0, 0, 0, X, 0] i want to write this, so string and int

i want to write this,

Do you just want to write? No need to create an Array?

so string and int

What will be the values of the String and Int for the example output:
Code Block
[X, 0, 0, 0, 0]
[0, X, 0, 0, 0]
[0, 0, X, 0, 0]
[0, 0, 0, X, 0] 

The String value would be "X"? The Int value might be 4 (number of rows)? Or 5 (number of items in a row)? Or 0 (the value for non-X elements)? Or 20 (total number of items)?

Or something else?

Your question is not very clear.

Do you want to print (where ?) or do you want to display the matrix on screen in a Label, TextField, TextView ?

To print in log, simply do:
Code Block
for x in xo {
print(x)
}

i want to create a function with array

i want to create a function with array

You once said, string and int and this time with array. Please clarify what you want to do.
i want to make a function with array

i want to make a function with array 

So, you want to define a function taking Array and Int. OK?

And you have not answered to my other questions.

Do you just want to write? No need to create an Array?
What will be the values of the Array and Int for the example output:

Please clarify all the things needed to write a function.
I want to write with a matrix function using if for, and get this result
[X, 0, 0, 0, 0]
[0, X, 0, 0, 0]
[0, 0, X, 0, 0]
[0, 0, 0, X, 0]
[0, 0, 0, 0, X]

 get this result 

Please clarify what are the input to get the result.
10 and x

10 and x

Int and String (or String and String)?

You once said:

i want to make a function with array 

Please clarify:
  • All the parameters, with the types of them and how they affect the result

  • Result type, Array of String? Or Array of Array of String (often used as a 2-D array)? Or something else?

Accepted Answer
Your answers are absolutely confusing.

If you want a matrix in x0, declare

Code Block
var matrix = [[String]]() // Array of Arrays ; changed the name to be more meaningful

and populate as follows:
Code Block
var xo1 = ["x", "10","10","10","10"]
var xo2 = ["10 ","x", "10","10","10"]
var xo3 = ["10 ","10","x" ,"10","10"]
var xo4 = ["10 ","10","10","x" ,"10"]
var xo5 = ["10 ","10","10","10","x"]
matrix.append(xo1)
matrix.append(xo2)
matrix.append(xo3)
matrix.append(xo4)
matrix.append(xo5)

Then with
Code Block
print(matrix)
you get

[["x", "10", "10", "10", "10"], ["10 ", "x", "10", "10", "10"], ["10 ", "10", "x", "10", "10"], ["10 ", "10", "10", "x", "10"], ["10 ", "10", "10", "10", "x"]]

Or with:
Code Block
for row in matrix {
    print(row)
}
you get

["x", "10", "10", "10", "10"]
["10 ", "x", "10", "10", "10"]
["10 ", "10", "x", "10", "10"]
["10 ", "10", "10", "x", "10"]
["10 ", "10", "10", "10", "x"]

Is it what you are looking for ?
If so, don't forget to close the thread on this answer.
Otherwise, please explain what different output you want.
Swift functions
 
 
Q