When you post questions on this forum, it is helpful to wrap your code in triple backticks, so that it will be formatted properly.
That is the button just below the escape key on your keyboard. You can type 3 backticks on a line, press the return key to move to a new line, and type 3 more backticks there. Then, paste your code between the two lines of backticks.
When I do that with your code after formatting it in XCode, it looks like this...
var i: Int = 1
func multipleTable (tableD: Int, tableT: Int) -> Int {
let result: Int = tableD
i
while (i<=tableT) {
print("(tableD) (i) =", tableD * i)
if i >= tableT { break }
i = i + 1
}
return result
}
Honestly, I can't really tell what you are trying to do in this function. Right now, it looks like it prints multiple lines of text to the standard output, and then returns result
which will always be equal to tableD
because you set it equal to tableD
and never change its value.
The reason you would be getting the error "Cannot assign value of type (Int, Int) -> Int
to type String
" is probably because you are trying to write something like this...
text = multipleTable
A UITextView
's text property must be a String
and since you are not actually calling your function, you are trying to set the text String equal to your function itself. (Your function takes two Int
as parameters and returns an Int
so it is of the type (Int, Int) -> Int
)
If you try to use this instead...
text = multipleTable(tableD: 5, tableT: 5))
(I have inserted 5 for both parameters, but you could use any Int you want there)
It will still fail, because even though you are calling your function now, your function returns an Int
and not a String
But if you use this instead...
text = "\(multipleTable(tableD: 5, tableT: 5))"
Then it will use string interpolation to convert the Int
value into a String
which can be used in the UITextView
. However, since your function is returning result
which is always equal to the tableD
parameter that you pass to your function, I don't think that is the result that you are looking for still.