Variable does not seem to be declared

0

Two issues:

  • I do not understand why each end of text looks like \n .
  • I declare a variable name , and when I call the variable in the middle of the text it does not seem to be 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")
    }
    

    Print screen:

        
    asked by anonymous 06.09.2016 / 02:36

    2 answers

    2

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

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

    The \n in the text is why you are using the print command, this command prints a code for a new line at the end. so this does not happen you should do so:

    print(" Hi, "+name+" welcome", terminator: "")
    
        
    06.09.2016 / 02:39
    1

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

    To insert your variable inside the String just put the variable in parentheses and preceded by% slash%.

    In your case it would look like this:

    print("Hi, \(name) welcome")
    
        
    06.09.2016 / 08:21