With an expressive, easy-to-read syntax, Swift empowers new developers to quickly understand core programming concepts. And with resources like the Develop in Swift Tutorials, Swift Coding Clubs, and Swift Playground, it’s never been easier to get started with Swift as your first programming language.
Learn more about Swift education resources from Apple
Experienced developers can also quickly dive in and take advantage of the power and safety of Swift, with the comfort of familiar modern features used in other programming languages.
struct Player {
var name: String
var highScore: Int = 0
var history: [Int] = []
init(_ name: String) {
self.name = name
}
}
var player = Player("Tomas")
Declare new types with modern, straightforward syntax, provide default values for instance properties, and define custom initializers.
extension Player {
mutating func updateScore(_ newScore: Int) {
history.append(newScore)
if highScore < newScore {
print("\(newScore)! A new high score for \(name)! 🎉")
highScore = newScore
}
}
}
player.updateScore(50)
// Prints "50! A new high score for Tomas! 🎉"
// player.highScore == 50
Add functionality to existing types with extensions, and cut down on boilerplate code with custom string interpolations.
extension Player: Codable, Equatable {}
import Foundation
let encoder = JSONEncoder()
try encoder.encode(player)
print(player)
// Prints "Player(name: "Tomas", highScore: 50, history: [50])"
Perform powerful custom transformations using streamlined closures.
let players = getPlayers()
// Sort players, with best high scores first
let ranked = players.sorted(by: { player1, player2 in
player1.highScore > player2.highScore
})
// Create an array with only the players’ names
let rankedNames = ranked.map { $0.name }
// ["Erin", "Rosana", "Tomas"]
Quickly extend your custom types to take advantage of powerful language features, such as automatic JSON encoding and decoding.