简体   繁体   English

如何在 Xcode 8.2 的操场项目中使用 Swift 从用户那里获取输入?

[英]How to get input from user using Swift in playground project in Xcode 8.2?

I am trying to get the dynamic input from user in playground for iOS but it's not working.我试图在 iOS 的操场上从用户那里获取动态输入,但它不起作用。 I tried the following code but it didn't work.我尝试了以下代码,但没有用。

import Foundation
import UIKit
func input() -> String {
    var keyboard = FileHandle.standardInput
    var inputData = keyboard.availableData
    var strData = NSString(data: inputData, encoding: String.Encoding.utf8.rawValue)!

    return strData.trimmingCharacters(in: NSCharacterSet.newlines)
}

input()

You can get your playground input from a textField as follows:您可以从 textField 获取您的游乐场输入,如下所示:

    import PlaygroundSupport
    import UIKit

    class V: UIViewController {
        var textField = UITextField(frame: CGRect(x: 20, y: 20, width: 200, height: 24))
        override func viewDidLoad() {
            super.viewDidLoad()
            view.addSubview(textField)
            textField.backgroundColor = .white
            textField.delegate = self
        }
    }
    extension V: UITextFieldDelegate {
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            // Do stuff here
            return true
        }
    }
    let v = V()
    v.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    PlaygroundPage.current.liveView = v.view
    PlaygroundPage.current.needsIndefiniteExecution = true

Getting input from playground is not doable, You can do it in an XCode project using:从操场获取输入是不可行的,您可以在 XCode 项目中使用:

print("Please enter your name")
var name = readLine()
print("name: \(name!)")

Or:或者:

func input() -> String {
   let keyboard = FileHandle.standardInput
   let inputData = keyboard.availableData
   return String(data: inputData, encoding: .utf8)!
}

print("Please enter your name")
var name = input()
print("name: \(name!)")

我认为不可能在 Xcode 的 Playground 上获得用户输入。

print("Please Enter your favorite programming language", terminator: ".") let name = readLine() print("Your favorite programming language is \\(name!).")

In the above program, the print function outputs Please Enter your favorite programming language.在上面的程序中,打印功能输出请输入您喜欢的编程语言。 in the debug area.在调试区。 The statement let name = readLine() waits for user input in the debug area.语句 let name = readLine() 在调试区等待用户输入。

If you type "Swift" and press enter, the readLine function assigns that string to constant name and displays the output as Your favorite programming language is Swift.如果您键入“Swift”并按回车键,则 readLine 函数会将该字符串分配给常量名称并将输出显示为您最喜欢的编程语言是 Swift。

Since the readLine function returns an optional string, we have forcefully unwrapped the constant as name!由于 readLine 函数返回一个可选字符串,因此我们强制将常量解包为 name! in the statement print("Your favorite programming language is (name!)").在语句中打印(“你最喜欢的编程语言是(名称!)”)。

Is it possible to “Answer” project in the same section as an empty playground.是否可以在与空的操场相同的部分“回答”项目。 You can use “ask()” and “show()” methods.您可以使用“ask()”和“show()”方法。

For example:例如:

func square(num: Double) -> Double {
    return num * num
}

show("Please type a number to square it:")
var sqrt = askForNumber()
show("Result: \(square(num: Double(sqrt)))")

Sorry, I'm writing on iPad and it's difficult to input code.抱歉,我在 iPad 上写字,输入代码很困难。

How it looks during execution执行过程中的样子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Xcode 9.2的Playground项目中使用swift从用户获取输入 - how to get input from user using swift in playground project in xcode 9.2 如何在 Swift Xcode playgroud 中获取用户的输入 - How to get input from the user in swift Xcode playgroud 我们可以将xcode项目(xcodeproj)转换为Swift Playground(.playground)格式吗? - Can we convert xcode project (xcodeproj) to swift playground (.playground) format? 如何在使用cocoapods时将项目代码暴露给Xcode游乐场? - How to expose your project code to a Xcode playground when using cocoapods? Xcode 8.2 Playground中“ PAGE”描述的格式是什么? - Which is the format described 'PAGE' in Xcode 8.2 Playground? Xcode Project和Playground关于Swift错误处理的区别 - Difference between Xcode Project and Playground about Swift error handling 为什么我的Swift代码在操场上可以工作,而在Xcode项目中却不能工作? - Why does my Swift code work in the playground but not in the Xcode project? 如何使用Xcode 8.2中的Swift 3.0创建和存储包含数据的托管对象 - How to create and store managed objects containing the data using Swift 3.0 in Xcode 8.2 如何在SWIFT3 XCODE 8.2的IOS应用程序中通过Snackbar的操作显示特定视图 - How to display a specific view from the action of the Snackbar in IOS app in SWIFT3 XCODE 8.2 如何获得清晰的 PlayGround 文件 Xcode 12.3 - How to Get a Clear PlayGround file Xcode 12.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM