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