swift 3.0 boom grammar

原创 2016年10月11日 18:05:05
var str = "Hello, playground"
print(str)
let a = 10
let b = 10
let c = a + b


// MARK: - swift chain
extension Int {
    func add(_ value: Int) -> Int {
        return self + value
    }
}
let sum = 3.add(2).add(6)

var myVariable = 42
myVariable = 50
/// Description 
let myConstant = 42

//basic

/// int type
let apple = 30
let orange = 23
let appleSummary = "I have \(apple) apples"
let fruitSummary = "I have \(apple + orange) pieces of fruit"
let fruitSummary_beta = "I have \(apple.add(orange)) pieces of fruit"

/// array or list type
var shoppingList = ["catfish","water","tulips","blue paint"]
shoppingList[1] = "bottle of water"
print(shoppingList)


/// dictionary
var occupations = [
    "Malcon":"Captian",
    "Kaylee":"Mechanic",
]
occupations["Jayne"] = "Public Relations"
print(occupations)

let emptyArray = [String]()
let emptyDictionary = [String:Float]()

shoppingList = []
occupations = [:]
print(shoppingList)

///////////////////////////////

/// if for switch Control Flow

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

//var optionalString: String? = "Hello"
var optionalString: String? = nil
print(optionalString == nil)

var optionalName: String? = "John Appleseed"
//var optionalName: String? = nil
var greeting = "Hello!"

if let name = optionalName {
    greeting = "Hello, \(name)"
}else{
    greeting = "Good Morning sir"
}

let nickName:String? = nil
let fullName:String? = "John Smith"
let informalGreeting = "Hi\(nickName ?? fullName)"

let compare = a > b ? a : b


////////////////////////////////////
// functions

func greeting(name:String,day:String)->String{
    return "Hello \(name), today is \(day)"
}

greeting(name: "Bob",day: "Sunday")

func greet(person:[String:String]){
    guard let name = person["name"] else {
        return
    }
    print("hello \(name)")
    guard let location = person["location"] else {
        print("I hope the weather is nice near you")
        return
    }
    print("H hope the weather is nice in \(location)")
}

greet(person: ["name":"Bob"])

print(greeting(name: "Bob", day: "Monday"))

greet(person: ["name":"Bob","location":"Beijing"])

if #available(iOS 10,macOS 10.12 ,*) {
    print("hello there")
}else{
    print("welcome back")
}

let array = 1..<10

print(array)

for arr in array {
    print(arr)
}

//func minMax(array:[Int]) ->(min:Int,max:Int){
//
//    var currentMin = array[0]
//    var currentMax = array[0]
//    for value in array[1..<array.count] {
//        currentMin = currentMin < value ? currentMin : value
//        currentMax = currentMax > value ? currentMax : value
//    }
//    return (currentMin,currentMax)
//}
//
//let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
//
//print(bounds)
//

func minMax(array:[Int]) ->(min:Int ,max:Int)?{
    if array.isEmpty {
        return nil
    }
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        currentMin = currentMin < value ? currentMin : value
        currentMax = currentMax > value ? currentMax : value
    }
    return (currentMin,currentMax)
}

let bounds = minMax(array: [])

print(bounds)

//func compareTwoNumber(number1:Int = -1,number2 : Int = -1){
//
//    let max = number1 > number2 ? number1 : number2
//    let min = number1 > number2 ? number2 : number1
//    
//    print("max is \(max) , min is \(min)")
//    
//}
//
//compareTwoNumber(number1: 1,number2: 3)

func compareTwoNumber(_ number1:Int = -1,_ number2 : Int = -1){

    let max = number1 > number2 ? number1 : number2
    let min = number1 > number2 ? number2 : number1

    print("max is \(max) , min is \(min)")

}

compareTwoNumber(1,3)

func swapNumber(_ number1:inout Int,_ number2: inout Int){
    let tmp = number1
    number1 = number2
    number2 = tmp
}

var t1 = 12
var t2 = 13

swapNumber(&t1, &t2)

print(t1)
print(t2)

////////////////////////

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    return a * b
}

var mathFunction: (Int, Int) -> Int = addTwoInts

print("Result = \(mathFunction(2,3))")

mathFunction = multiplyTwoInts
print("Result = \(mathFunction(2,3))")

func printMathResult(_ mathFunction:(Int,Int) ->Int, _ a:Int, _ b:Int){

    print("Result = \(mathFunction(a,b))")

}
printMathResult(addTwoInts,3,4)
printMathResult(multiplyTwoInts,3,4)

炸裂语法之closure

//排序
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}
var reversedNames = names.sorted(by: backward)

//reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 })

//reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )

//reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

//reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

//reversedNames = names.sorted(by: { $0 > $1 } )

reversedNames = names.sorted(by: >)

print(reversedNames)
版权声明:本文为博主原创文章欢迎分享转载。

相关文章推荐

swift3.0教程

  • 2017年11月03日 11:16
  • 4.17MB
  • 下载

ScrollViewDemo(Swift 3.0)

  • 2017年04月06日 13:36
  • 212KB
  • 下载

十三 Swift3.0之 数组基本使用

/* 数组定义: OC: 有值数组 NSArray *arr0 = @[@1, @2, @3]; NSArray *arr1 = [NSArray arrayWithObjects:@1, @2, @...

Swift3.0游戏开发实战教程

  • 2016年08月22日 14:00
  • 2.3MB
  • 下载

Swift3.0语言教程比较、判断字符串

Swift3.0语言教程比较、判断字符串Swift3.0语言教程比较、判断字符串

Swift 3.0 中文教程

  • 2017年02月22日 15:45
  • 4.19MB
  • 下载

objccn-advanced-swift 3.0

  • 2017年03月19日 07:44
  • 5.03MB
  • 下载

Swift3.0教程(一)-基本的语法

Swift3.0教程(一)-基本的语法

swift3.0 定位demo

  • 2017年11月01日 18:21
  • 54KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:swift 3.0 boom grammar
举报原因:
原因补充:

(最多只允许输入30个字)