[英]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 上写字,输入代码很困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.