蘋果於WWDC 2014釋出的程式語言Swift
阿新 • • 發佈:2018-12-23
Swift是什麼?
簡單的說:
Swift用來寫iOS和OS X程式。(估計也不會支援其它屌絲系統)
Swift吸取了C和Objective-C的優點,且更加強大易用。
Swift可以使用現有的Cocoa和Cocoa Touch框架。
Swift兼具編譯語言的高效能(Performance)和指令碼語言的互動性(Interactive)。
Swift語言概覽
基本概念
Hello, world
類似於指令碼語言,下面的程式碼即是一個完整的Swift程式。
Swift使用var宣告變數,let宣告常量。
Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to imagine how software development works. Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language. |
- println("Hello, world")
- var myVariable = 42
- myVariable = 50
- let myConstant = 42
- let explicitDouble : Double = 70
-
let label = "The width is "
- let width = 94
- let widthLabel = label + String(width)
- let apples = 3
- let oranges = 5
- let appleSummary = "I have \(apples) apples."
- let appleSummary = "I have \(apples + oranges) pieces of fruit."
- var shoppingList = ["catfish", "water", "tulips", "blue paint"]
- shoppingList[1] = "bottle of water"
- var occupations = [
- "Malcolm": "Captain",
- "Kaylee": "Mechanic",
- ]
- occupations["Jayne"] = "Public Relations"
- let emptyArray = String[]()
- let emptyDictionary = Dictionary<String, Float>()
- let individualScores = [75, 43, 103, 87, 12]
- var teamScore = 0
- for score in individualScores {
- if score > 50 {
- teamScore += 3
- } else {
- teamScore += 1
- }
- }
- var optionalString: String? = "Hello"
- optionalString == nil
- var optionalName: String? = "John Appleseed"
- var gretting = "Hello!"
- if let name = optionalName {
- gretting = "Hello, \(name)"
- }
- let vegetable = "red pepper"
- switch vegetable {
- case"celery":
- let vegetableComment = "Add some raisins and make ants on a log."
- case"cucumber", "watercress":
- let vegetableComment = "That would make a good tea sandwich."
- case let x where x.hasSuffix("pepper"):
- let vegetableComment = "Is it a spicy \(x)?"
- default:
- let vegetableComment = "Everything tastes good in soup."
- }
- let interestingNumbers = [
- "Prime": [2, 3, 5, 7, 11, 13],
- "Fibonacci": [1, 1, 2, 3, 5, 8],
- "Square": [1, 4, 9, 16, 25],
- ]
- var largest = 0
- for (kind, numbers) in interestingNumbers {
- for number in numbers {
- if number > largest {
- largest = number
- }
- }
- }
- largest
- var n = 2
- while n < 100 {
- n = n * 2
- }
- n
- var m = 2
- do {
- m = m * 2
- } while m < 100
- m
- var firstForLoop = 0
- for i in 0..3 {
- firstForLoop += i
- }
- firstForLoop
- var secondForLoop = 0
- forvar i = 0; i < 3; ++i {
- secondForLoop += 1
- }
- secondForLoop
- func greet(name: String, day: String) -> String {
- return"Hello \(name), today is \(day)."
- }
- greet("Bob", "Tuesday")
- func getGasPrices() -> (Double, Double, Double) {
- return (3.59, 3.69, 3.79)
- }
- getGasPrices()
- func sumOf(numbers: Int...) -> Int {
- var sum = 0
- for number in numbers {
- sum += number
- }
- return sum
- }
- sumOf()
- sumOf(42, 597, 12)
- func returnFifteen() -> Int {
- var y = 10
- func add() {
- y += 5
- }
- add()