Variable does not appear as declared

Asked

Viewed 64 times

0

Two issues:

  1. I do not understand what is the reason at each end of text seems a \n.
  2. I declared a variable name, and when I call the variable in the middle of the text it does not seem that it has not been declared.

Xcode Version 7.3.1 (7D1014)

Code:

import UIKit

var age = 19

if age >= 18 {
    print("You can play")
} else {
    print("sorry are young")
}

var name = "Lucas"

if name == "Lucas" {
    print("Hi, + name + welcome")
}

Screen print:

Print de Tela

2 answers

2


Your name not as variable, but as String, should look like this:

print(" Hi, "+name+" welcome")

The \n in the text is why you are using the command print, this command prints a code for a new line at the end. so that it doesn’t happen you must do so:

print(" Hi, "+name+" welcome", terminator: "")
  • Thanks Jasar Orion, it worked but the n still appears

  • even using the Terminator? print("You can play", Terminator: "")

  • Now gave, had an extra space. Thanks

  • rsss otimo tag reply as solved there and already was

  • one more question where I close where I switch to solved just created the account

  • ta seeing in my comment yesterday has the number one with an arrow up and down ? just below has a V. It’s just a check

Show 1 more comment

1

I suggest you use a method called String Interpolation which enables you to use any type that conforms to the protocol CustomStringConvertible (String, Double, Int, etc.).

To insert your variable into the String just place the variable between parentheses and preceded by counter-bar \(suaVariavel).

In your case it would look like this:

print("Hi, \(name) welcome")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.