DEV Community

BC
BC

Posted on

Learn SwiftUI (Day 1/100)

Swift

  • Variable
  • Constants
  • String and multiple-line string
  • Integer
  • Double
import Cocoa // "var" means make a new variable var greeting = "Hello, playground" print(greeting) var name = "hello" print(name) name = "world" print(name) // let: Declares a constant that cannot be changed once it has been set, i.e., an immutable variable let lang = "python" print(lang) // lang = "java" // Error: cannot assign to value // print(lang) /* Best practice suggests that you should default to using let to define a variable and only use var when you explicitly need the variable’s value to change. This helps lead to cleaner, more predictable code. */ // ## Create strings let quote = "Then he tapped a sign \"Believe\" and walked away" // multiple line string let movie = """ hello 世界 """ print(movie) print(movie.count) // 8 -> count returnes unicode count let trimmed = movie.trimmingCharacters(in: .whitespacesAndNewlines) print(trimmed.count) // define integers let score = 10 print(score / 3) // 3 var points = 10 points += 1 print(points) // define floating number let number = 0.1 + 0.2 print(number) let a = 1 let b = 2.0 // int and double cannot sum, must cast first let c = Double(a) + b let d = a + Int(b) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)