3

I am trying to get the dynamic input from user in playground for iOS but it's not working. 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()
1
  • I don't think it's possible to get command line input in playground.
    – ebby94
    Jan 19, 2017 at 4:28

5 Answers 5

4

Getting input from playground is not doable, You can do it in an XCode project using:

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!)")
4

You can get your playground input from a textField as follows:

    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
2

I don't think it's possible to get the user input on Playground of Xcode.

-1

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.

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.

Since the readLine function returns an optional string, we have forcefully unwrapped the constant as name! in the statement print("Your favorite programming language is (name!)").

0
-1

Is it possible to “Answer” project in the same section as an empty playground. You can use “ask()” and “show()” methods.

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.

How it looks during execution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.