NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Reflecting return of a function in UITextView

Forums > iOS

I have written a simple code as follows:

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 }

upon click of a button I want to print this function in a UITextView but that comes with an error {Cannot assign value of type '(Int, Int) -> Int' to type 'String'} How can I fix it


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

        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.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.