swift 3.0 boom grammar

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)
阅读更多
版权声明:本文为博主原创文章欢迎分享转载。 https://blog.csdn.net/hi_chen_xingwang/article/details/52790365
文章标签: swift swift-3-0
个人分类: iOS swift3-0
上一篇python 输入字符串比较
下一篇swift 3.0 字符串截取
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭