skip to Main Content

I started learning Swift 3 days ago and I think you will understand it. But every time I want to put a Text or Image to the Button, It gives me this error:

Result of ‘Image’ initializer is unused / Result of ‘Text’ initializer is unused

This is driving me crazy and I am trying to search for help but everybody describes it in a professional way. Can anyone help me?

Thanks

2

Answers


  1. You put the Image in the action block of the button, which is the stuff do do when the button is tapped. It’s not the place to put the content of the button.

        Button(action: {
          print("Pressed")
        }) {
            Image("someImageName")
        }
    

    The error message means something like you computed some thing, but then did nothing with that result. For example 3+4 would compute 7 and then just do nothing with the 7. But print(3+4) would do something with the 7. It’s suspicious to compute something for no reason, so XCode warns you.

    Login or Signup to reply.
  2. //For set text to button

    btnTest.setTitle("Your text", for: .normal)
        
    

    //For set icon to button

    btnTest.setImage(UIImage(named: "Your Imgae name"), for: .normal)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search