UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

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.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.