These days, I accepted some easy problems on leetcode, in order to get familiar to a new programming language——
Swift. In this series, I will write down basic syntax and come useful skills. Mostly, it’s my personal online note.
// hello_world.swift print("hello, world!")
The code above will print
"hello, world!\n", and it’s a complete program. I can run it in command line by tapping
swift hello_world.swift. It’s obviously that this language has some feature of script language.
We can add a parameter to change the last character of the string, and by doing this, the
// hello_world_unwrap.swift print("hello, world!", terminator: "")
And another parameter
separator: is also important. You can use it like
Swift’s comments are C++ like. For example:
// luv 🍟 /* luv 📦*/
It’s no need to explain more.
values, variables, and constants
Swift uses different keywords.
import Darwin // use acos() let Pi = acos(0.0) * 2, r: Double = 1 var s = Pi * r * r
btw. You can interctive with swift like ruby, just tap
swift and enter.
You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store.
If you provide an initial value for a constant or variable at the point that it’s defined, Swift can almost always infer the type to be used for that constant or variable.
print(Int.max) // 9223372036854775807 print(Int32.max) // 2147483647 print(UInt.max) // 18446744073709551615
Digitals can be separated by
print(2_333_444_555) // 2333444555