import UIKit class ViewController: UIViewController { @IBOutlet weak var numberInput: UITextField! @IBOutlet weak var calculateButton: UIButton! var currentNumber = 0.0 var previousNumber = 0.0 var operation = "" override func viewDidLoad() { super.viewDidLoad() // Configura o campo de entrada de números numberInput.keyboardType = .decimalPad numberInput.returnKeyType = .done numberInput.enablesReturnKeyAutomatically = true // Configura o botão de calcular calculateButton.setTitle("Calcular", for: .normal) calculateButton.addTarget(self, action: #selector(performCalculation), for: .touchUpInside) } @objc func performCalculation() { guard let number = Number(string: numberInput.text!) else { return } switch operation { case "+": currentNumber += number.doubleValue case "-": currentNumber -= number.doubleValue case "*": currentNumber *= number.doubleValue case "/": currentNumber /= number.doubleValue default: break } previousNumber = currentNumber numberInput.text = "\(currentNumber)" operation = "" } }
Write, Run & Share Swift code online using OneCompiler’s Swift online compiler for free. It’s a fast and user-friendly platform to explore Swift programming right from your browser. Ideal for students, beginners, and professionals looking to prototype or test Swift code.
Swift is a powerful and intuitive programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development. It's designed to be safe, fast, and expressive, with modern features like optionals, closures, and type inference.
The following is a simple Swift program that prints a greeting:
import Foundation
print("Hello, OneCompiler!")
OneCompiler’s Swift editor supports standard input via the I/O tab. Here’s a sample Swift program that takes a user’s name and prints a greeting:
import Foundation
if let name = readLine() {
print("Hello, \(name)!")
}
var age = 25 // Mutable variable
let name = "Swift" // Constant
Type | Description |
---|---|
Int | Whole numbers |
Double | Decimal numbers |
String | Sequence of characters |
Bool | true or false |
Array | Ordered collection |
Dictionary | Key-value pairs |
let score = 75
if score >= 50 {
print("Pass")
} else {
print("Fail")
}
for i in 1...5 {
print(i)
}
var i = 1
while i <= 5 {
print(i)
i += 1
}
var j = 1
repeat {
print(j)
j += 1
} while j <= 5
func greet(name: String) {
print("Hello, \(name)!")
}
greet(name: "OneCompiler")
func add(a: Int, b: Int) -> Int {
return a + b
}
This guide provides a quick reference to Swift programming syntax and features. Start writing Swift code using OneCompiler’s Swift online compiler today!