swift

Swift 是由 Apple 創造,強大且直覺易用的全新程式語言,用於打造 iOSMacapp蘋果強調Swift具備「簡單、互動、現代、快速、安全」等特性,可輕鬆開發出安全而快速的軟體。對學生而言,學習Swift是其接觸現代化開發概念及最佳典範的入門,其技能將能應用於行動裝置、桌面、雲端等多種環境上,開源釋出後,更有利其廣泛使用。 藉由與Objective-C的相容,Swift旨在取代Objective-CPython等程式語言。

遞增 (++) 遞減 (--)

Installing Swift

shell> sudo apt-get install clang libicu-dev
shell> wget https://swift.org/builds/swift-3.1-release/ubuntu1604/swift-3.1-RELEASE/swift-3.1-RELEASE-ubuntu16.04.tar.gz
shell> tar xvzf swift-3.1-RELEASE-ubuntu16.04.tar.gz
shell> mv swift-3.1-RELEASE-ubuntu16.04 /usr/local/
shell> export PATH=/usr/local/swift-3.1-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
shell> swift -version
Swift version 3.1 (swift-3.1-RELEASE)
Target: x86_64-unknown-linux-gnu

shell> swiftc -version
Swift version 3.1 (swift-3.1-RELEASE)
Target: x86_64-unknown-linux-gnu
shell> swift
Welcome to Swift version 3.1 (swift-3.1-RELEASE). Type :help for assistance.
  1>

helloworld.swift

print("Hello, world!")
shell> swift helloworld.swift

shell> swiftc helloworld.swift
./helloworld: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory

shell> swiftc -static-stdlib helloworld.swift

:books: 參考網站:


print("Hello, world!")
1 + 2

let greeting = "Hello!"
print(greeting)

let numbers = [1,2,3]
for n in numbers.reverse() {
  print(n)
}

import Glibc
random() % 10

libcurl4-openssl-dev

shell> swift build --help
shell> mkdir Hello
shell> cd Hello
shell> swift package init
/usr/local/swift-3.1-RELEASE-ubuntu16.04/usr/bin/swift-package: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

shell> sudo apt-get install libcurl4-openssl-dev
shell> swift package init
Creating library package: Hello
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/Hello.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/HelloTests/
Creating Tests/HelloTests/HelloTests.swift
.
├── Package.swift
├── Sources
│   └── Hello.swift
└── Tests
    ├── HelloTests
    │   └── HelloTests.swift
    └── LinuxMain.swift

3 directories, 4 files
shell> touch Package.swift
shell> mkdir Sources
shell> main.swift

shell> swift build

shell> .build/debug/Hello
Hello, world!

shell> swift build -Xswiftc -static-stdlib

Greeter.swift

func sayHello(personName: String) -> String {
    let greeting = "Hello, " + personName + "!"
    return greeting
}

main.swift

print(sayHello(personName: "Anna"))
// Prints "Hello, Anna!"

print(sayHello(personName: "Brian"))
// Prints "Hello, Brian!"

Greeter.swift

func sayHello(personName: String) {
    print("Hello, \(name)!")
}

main.swift

if Process.arguments.count != 2 {
    print("Usage: hello NAME")
} else {
    let name = Process.arguments[1]
    sayHello(personName: name)
}
com.example.app1
com.example.app2
com.example.app.app1
com.example.app.app2

:books: 參考網站:


//  Created by Timmy on 2015/2/24.
//  Copyright (c) 2015年 Timmy. All rights reserved.

import Foundation

println("Hello, World!")

var myVariable = 42
myVariable = 50
let myConstant = 42

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 fruitSummary = "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 = [String: Float]()

let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}
println(teamScore)

:books: 參考網站:

powered by Gitbook最後更新: 2017-08-25 10:06:20

results matching ""

    No results matching ""