Swift 第一個程序
要運(yùn)行您的首個Swift程序,您必須在計算機(jī)上安裝Xcode和Swift。愛掏網(wǎng) - it200.com
打開Xcode Playground,并寫下以下代碼:
// This is the first Swift program also called Hello, World! Program
import Swift
print("Hello, World!")
輸出:
"Hello World"
程序解釋
1. // This is the first Swift program also called Hello, World! Program
上述一行以兩個斜杠(//)開頭,是一個注釋。愛掏網(wǎng) - it200.com編譯器完全忽略它。愛掏網(wǎng) - it200.com注釋用于使其他人能夠理解你的代碼。愛掏網(wǎng) - it200.com
2. import Swift
導(dǎo)入Swift關(guān)鍵字用于訪問在Swift語言中定義的所有符號。愛掏網(wǎng) - it200.com如果不使用這個關(guān)鍵字,你無法使用print語句在Xcode上打印任何東西。愛掏網(wǎng) - it200.com
3. print("Hello, World!")
上述行是Swift中的print函數(shù)。愛掏網(wǎng) - it200.com它用于在屏幕上顯示輸出。愛掏網(wǎng) - it200.com
在終端上的”Hello World”程序
按照下面給出的步驟進(jìn)行:
- 打開終端
- 鍵入swift并按回車。愛掏網(wǎng) - it200.com會得到一個歡迎消息,如Welcome to Apple Swift version x.x.x。愛掏網(wǎng) - it200.com
- 鍵入print(“Hello, World!”)
- 按回車鍵
輸出:
Hello, World!
注意:與其他編程語言C,C ++,Java,Python等不同,Swift不需要在語句末尾添加分號(;),這是可選的。愛掏網(wǎng) - it200.com 您可以通過在第一條語句的末尾使用分號(;)來在一行中添加多行。愛掏網(wǎng) - it200.com
示例
print("Hello World",");print(" Welcome to Javatpoint")
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。