<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Swift 基本輸入和輸出 > 原文: [https://www.programiz.com/swift-programming/basic-input-output](https://www.programiz.com/swift-programming/basic-input-output) #### 在本文中,您將學習在 Swift 中顯示輸出和獲取輸入的不同方法。 ## Swift 基本輸出 您可以簡單地使用`print(_:separator:terminator:)`函數將輸出發送到標準輸出(屏幕)。 請參閱 [Swift 函數](/swift-programming/functions "Swift functions")文章,以了解 Swift 中的函數。 函數`print(_:separator:terminator:)`接受三個參數。 * `item`要在控制臺中打印的項目。 它可以接受多個項目。 * `separator`在每個項目之間打印的字符串。 默認值為單個空格`(" ")`。 * `terminator`在打印所有項目之后要打印的字符串。 默認值為換行符`("\n")`。 由于最后兩個參數(`separator`,`terminator`)已指定了默認值,因此在調用打印函數時不必強制使用它們。 * * * ### 示例 1:使用簡單的`print()`函數打印到屏幕 ```swift print("Hello, World!") print("I love Swift.") ``` 運行該程序時,輸出為: ```swift Hello, World! I love Swift. ``` 在上述程序中,`print("Hello, World!")`輸出字符串字面值`"Hello, World!"`。 您可以看到,在打印`"I love Swift."`時,它還會更改行(添加換行符)。因為打印方法的參數`terminator`具有默認值`\n`(新行)。 因此,語句`print("I love Swift.")`在新行中輸出消息。 * * * ### 示例 2:打印常量,變量和字面值 ```swift var helloMsg = "Hello, World!" print(helloMsg) print(123.45) ``` 運行該程序時,輸出為: ```swift Hello, World! 123.45 ``` 您可以通過直接在打印函數中添加變量或常量名稱來打印變量或常量的值。 在上面的程序中`print(helloMsg)`輸出變量`helloMsg`的值`Hello, World!`。 您也可以在`print`語句中插入字面值。 在語句`print(123.45)`中,它采用不帶雙引號的浮點字面值`123.45`并將其打印出來。 * * * ### 示例 3:使用終止符參數進行打印而沒有行中斷 如果要打印而沒有換行符,則需要在`print`函數的終止符參數中傳遞一個空字符串,如下所示: ```swift print("Hello, World!", terminator: "") print("I love Swift.") print("I also love Taylor Swift.") ``` 運行該程序時,輸出為: ```swift Hello, World!I love Swift. I also love Taylor Swift. ``` 在上述程序中,`terminator`是在打印完所有項目之后打印的字符串。 我們傳遞了一個空字符串作為終止符(默認為換行符`\n`)。 因此,打印第一條語句時無需添加新行,并且語句`print("I love Swift.")`在同一行中顯示消息。 由于`print("I love Swift.")`函數添加了換行符,因此語句`print("I also love Taylor Swift")`在新行中輸出。 * * * ### 示例 4:使用單次打印函數打印多個項目 您還可以在單??個打印語句中打印多個項目,并在這些項目之間添加`separator`,如下所示: ```swift print("Hello, World!", 2020, "See you soon", separator: ". ") ``` 運行該程序時,輸出為: ```swift Hello, World!. 2020\. See you soon ``` 在上面的程序中,我們在打印語句中添加了不同的數據類型,并用逗號分隔。 要打印的項目是字符串`"Hello, World!"`,整數 **2020** 和字符串`"See you soon"`。 我們還在`separator`參數中傳遞了`"."`值。 這將在每個項目之間插入點字符(`.`)。 這樣您就可以看到輸出以分隔符分隔,后跟一個空格。 * * * ### 示例 5:打印多行 如果要使用單個打印語句多行打印,則可以在打印語句中使用轉義序列`\r`,即回車符: ```swift print("Hello, \rWorld!") ``` 運行該程序時,輸出為: ```swift Hello, World! ``` * * * ### 示例 6:使用三引號打印多行 在 Swift 中,有一種更好的方法可以在單個`print`語句中輸出多行消息。 您必須使用多行字符串字面值。 這是通過在多行字符串字面值中使用三引號將字符添加為 ```swift print(""" Hello, World! """) ``` 運行該程序時,輸出為: ```swift Hello, World! ``` * * * ### 示例 7:使用字符串插值打印變量 您還可以通過使用字符串插值將變量或常量的值添加到字符串字面值中,即,將變量包裝在以反斜杠`(\)`為前綴的一對括號中。 ```swift var helloMsg = "Hello, World!" print("I have a message \(helloMsg)") ``` 運行該程序時,輸出為: ```swift I have a message Hello, World! ``` 語句`print("I have a message \(helloMsg)")`將變量`helloMsg`的值包裝為字符串字面值中的`\(helloMsg)`,從而插入該值。 因此,該語句輸出`I have a message Hello, World!`。 * * * ## Swift 基本輸入 如果要在 Swift 中從用戶那里獲取輸入,則必須在不使用 UIKit 框架的情況下才能在 Xcode 游樂場上進行輸入。 但是,使用 Swift 框架,您可以在 Xcode 中創建一個命令行應用,以從用戶那里獲取輸入。 您可以參閱 *Swift 命令行應用*文章,以使用 Xcode 創建命令行應用。 這是您可以用來獲取用戶輸入的代碼。 ### 示例 8:使用`readLine()`從用戶那里獲取輸入 ```swift print("Please Enter your favorite programming language", terminator: ".") let name = readLine() print("Your favorite programming language is \(name!).") ``` 運行該程序時,輸出為: ```swift Please Enter your favorite programming language. Swift Your favorite programming language is Swift. ``` 在上述程序中,`print`函數輸出`Please Enter your favorite programming language.`。 語句`let name = readLine()`在調試區域中等待用戶輸入。 如果鍵入`"Swift"`并按`Enter`,則`readLine`函數將該字符串分配給常量`name`,并將輸出顯示為`Your favorite programming language is Swift. `。 由于`readLine`函數返回一個可選字符串,因此我們已強制將常量解壓縮為`print("Your favorite programming language is \(name!)")`語句中的`name`。 您將在文章中了解有關可選的更多信息: [Swift 可選的](/swift-programming/optionals "Swift Optionals")。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看