Can you do something similar to the following:
nombre = raw_input("cual es tu nombre?")
to take the user's response from textfield
or from the console?
Can you do something similar to the following:
nombre = raw_input("cual es tu nombre?")
to take the user's response from textfield
or from the console?
This way I can get it to work from the same Xcode and I can enter the value without error by console, it is practically the same code as the previous one but changing some little thing.
func input() -> NSString{
let teclado = NSFileHandle.fileHandleWithStandardInput()
let inputData = teclado.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)!
}
var operacion:String = input() as String
To assign the content of a UITextField
you just have to use the property text
:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
// otros métodos y variables
@IBAction func buttonClicked(sender: AnyObject) {
let valorIngresado = textField.text;
}
}
If you want to execute a script in the console, there is no method to directly read a value from the keyboard, but it is possible to implement it in the following way:
// Archivo script.swift
#!/usr/bin/env xcrun swift
import Foundation
// Esta función se encarga de leer desde la entrada estándar (teclado)
// Y regresa el dato como una cadena
func readLine() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding) as! String
}
let cadena = readLine()
print(cadena)
And run it with
$ ./script.swift
It is necessary to execute this example as a script, it does not work in playgrounds.
The code to read from the keyboard is originally here
I see that I can do something like this for use with Xcode:
let dato = UITextField(frame: CGRect(x:0, y: 0, width: 50, height: 30))
dato.placeholder = "Introduzca un valor"
In this way I formulate what I want to ask in the same textfield
. Although for console is not clear.