Swift笔记2

//

//  SwiftDemo.swift

//  SwifyDemo

// 

import Foundation

class TonySwiftModel{

    var numberOfDides:Int=0

    var name:String

    

    // 构造函数

    init(name:String){

        self.name=name

    }

    func PrintInfo(str:String){

        println(str+"tony")

        

        var num=0

        

        var numbers=[20,19,7,12]

        // 闭包函数

        var temp=numbers.map({

            (number:Int) -> Int in

            let result=3*number

            return result

            }

        )

        //PrintInfo(String(num))

        

        //调用其他类 属性、方法

        var shape=Shape(name: "")

        shape.numberOfSides=10

        shape.simpleDescription()

        

    }

    

    // 析构函数

//    deinit(){

//        self.name=""

//        self.numberOfDides=0

//    }

}

// 类

class Shape{

    var numberOfSides=0

    var nameId:String?

    // 初始化

    init(name:String)

    {

        self.nameId=name

    

    }

    

    func simpleDescription()->String{

        var doubleNum:Double=120.3

        doubleNum.getName()

        

        

        return "A shape with (numberOfSides) sides"

    }

    //析构函数

    deinit{

        // 释放掉一些内存

    }

}

// 枚举

enum Rank:Int{

    case Ace=1

    case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten

    case Jack,Queen,King

    func simpleDescription()->String{

        switch self{

        case .Ace:

            return "ace"

        case .Jack:

            return "jack"

        default:

            return "Default"

        }

    }

}

// 接口

protocol ExampleProtocol{

    var simpleDescription:String{get}

    // nutating 可变的

    mutating func adjust()

}

// 实现接口

class simpleClass: ExampleProtocol {

    var simpleDescription:String="A very simple class"

    var anotherProperty:Int=10110

    func adjust() {

        simpleDescription+="Now 100% adjust"

    }

}

// extension 给现有类型添加功能

extension Int:ExampleProtocol{

    var simpleDescription:String{

        return "The Number (self)"

    }

    mutating func adjust() {

        self += 42

    }

}

// double 扩展方法

extension Double{

    func getName()->String{

        return "admin"

    }

}

// 泛型

//class Generic{

//

//    func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {

//    var result=ItemType[]()

//    for i in 0..times{

//    result+=item

//    }

//    return result

//

//

//}

原文地址:https://www.cnblogs.com/pengzhaov/p/4370243.html