结构体

作者: 心底碎片 | 来源:发表于2016-09-08 17:31 被阅读14次
struct Location{
    let latitude:Double
    let longitute:Double
}
let appleHeadQuarterLocation = Location(latitude: 37.3230, longitute: -122.0322)
let googleHeadQuarterLocation: Location = Location(latitude: 37.4220, longitute: -122.0841)
appleHeadQuarterLocation.latitude
appleHeadQuarterLocation.longitute

struct Place {
    let location:Location
    var name: String
}
var googleHeadQuarter = Place(location: googleHeadQuarterLocation, name: "Google")
googleHeadQuarter.location.latitude

结构体的构造函数

struct Location{
    let latitude:Double
    let longitute:Double
    var placename:String?
    //使用Failable-Initializer
    init(coordinateString: String){
        let commsIndex = coordinateString.rangeOfString(",")!.startIndex
        let firstElement = coordinateString.substringToIndex(commsIndex)
        let secondElement = coordinateString.substringFromIndex(commsIndex.successor())
        self.latitude = Double(firstElement)!
        self.longitute = Double(secondElement)!
    }
    init( _ coordinateString2: String){//省略构造函数的名字
        let commsIndex = coordinateString2.rangeOfString(",")!.startIndex
        let firstElement = coordinateString2.substringToIndex(commsIndex)
        let secondElement = coordinateString2.substringFromIndex(commsIndex.successor())
        self.latitude = Double(firstElement)!
        self.longitute = Double(secondElement)!
    }
    init(latitude:Double, longitute:Double){
        self.latitude = latitude
        self.longitute = longitute
    }
    init(latitude:Double, longitute:Double, placename:String?){
        self.latitude = latitude
        self.longitute = longitute
        self.placename = placename
    }
}
let location = Location(coordinateString: "37.3230,-122.0322")
let location2 = Location("37.3230,-122.0322")
let location3 = Location(latitude: 37.3230, longitute: -122.0322)
let location4 = Location(latitude: 37.3230, longitute: -122.0322, placename: "apple")

使用Failable-Initializer可以失败的构造函数

struct Location{
    let latitude:Double
    let longitute:Double
    init?(coordinateString: String){
        if let commsIndex = coordinateString.rangeOfString(",")?.startIndex{
            if let firstElement = Double(coordinateString.substringToIndex(commsIndex)){
                if let secondElement = Double(coordinateString.substringFromIndex(commsIndex.successor())){
                    self.latitude = Double(firstElement)
                    self.longitute = Double(secondElement)
                }else{
                    return nil
                }
            }else{
                return nil
            }
        }else{
            return nil
        }
    }
}

用guard简化为---层层筛选

struct Location{
    let latitude:Double
    let longitute:Double
    init?(coordinateString: String){
        /*
        guard let commsIndex = coordinateString.rangeOfString(",")?.startIndex
            else{
            return nil
        }
        guard let firstElement = Double(coordinateString.substringToIndex(commsIndex))
            else{
            return nil
        }
        guard let secondElement = Double(coordinateString.substringFromIndex(commsIndex.successor()))
            else{
            return nil
        }
*/
        //继续简化
        guard
        let commsIndex = coordinateString.rangeOfString(",")?.startIndex,
        let firstElement = Double(coordinateString.substringToIndex(commsIndex)),
        let secondElement = Double(coordinateString.substringFromIndex(commsIndex.successor()))
            else{
                return nil
        }
        self.latitude = Double(firstElement)
        self.longitute = Double(secondElement)
    }
}
//let location = Location(coordinateString: "37.3230&-122.0322")

在结构体和枚举中定义方法

struct Location{
    let latitude:Double
    let longitute:Double
    init?(coordinateString: String){
        guard
            let commsIndex = coordinateString.rangeOfString(",")?.startIndex,
            let firstElement = Double(coordinateString.substringToIndex(commsIndex)),
            let secondElement = Double(coordinateString.substringFromIndex(commsIndex.successor()))
            else{
                return nil
        }
        self.latitude = Double(firstElement)
        self.longitute = Double(secondElement)
    }
    init(latitude:Double, longitute:Double){
        self.latitude = latitude
        self.longitute = longitute
    }
    //在结构体和枚举中叫做方法
    func printLocation(){
        print("The location is \(self.latitude),\(self.longitute)")
    }
    func isNorth() ->Bool{
        return self.longitute > 0
    }
    func isSouth() ->Bool{
        return !self.isNorth()
    }
    func distanceTo(location: Location) ->Double{
        return sqrt(pow(self.latitude - location.latitude, 2) + pow(self.longitute - location.latitude, 2))
    }
}
let appleHeadQuarterLocation = Location(latitude: 37.3230, longitute: -122.0322)
appleHeadQuarterLocation.printLocation()
appleHeadQuarterLocation.isSouth()
appleHeadQuarterLocation.isNorth()
let googleHeadQuarterLocation: Location = Location(latitude: 37.4220, longitute: -122.0841)
appleHeadQuarterLocation.distanceTo(googleHeadQuarterLocation)

结构体和枚举是值类型Value Type

struct Point{
    var x = 0
    var y = 0
}
var p1 = Point()
var p2 = p1
p2.x += 1
p2

#######Array Dictionary,Set都是结构体
#######Int, Float, Double, Bool, String都是结构体

相关文章

  • 结构体

    [toc] 结构体的定义方式 先定义结构体类型,再定义结构体变量 定义结构体类型的同时定义结构体变量 定义结构体类...

  • 【C语言笔记】<十九>结构体

    结构体的基本概念 结构体初始化 结构体的内存存储细节 结构体定义的方式 结构体类型的作用域 指向结构体的指针 结构...

  • C结构体和链表

    一,结构体变量定义及初始化 二,无名结构体 备注:无名结构体很少使用 三,宏定义结构体 四,结构体嵌套 五,结构体...

  • 结构体

    结构体定义* 结构体中的格式:* struch 结构体名* {* 结构体成员变量* }* 结构体中的特点* 1.结...

  • 结构体数组的定义

    结构体数组的定义 1、先定义结构体类型,再定义结构体数组 2、定义结构体类型的同时定义结构体数组 3、省略结构体类...

  • C#结构体,析构方法,跨程序访问

    结构体 结构体定义 结构体的语法格式: struct + 结构体名 { 结构体成员变量(相当于类中的字段) } 结...

  • 结构体

    结构体有名定义 无名定义 结构体嵌套定义 结构体内存对齐 结构体成员初始化 结构体变量引用 结构体的有名定义:直白...

  • 菜鸡学Swift3.0 13.结构体

    结构体 struct 是值类型 1.定义结构体 struct 结构体类型 { var 结构体属性:类型 ...} ...

  • 结构体

    结构体初识 结构体指针 结构体的匿名字段 结构体嵌套 Go语言中的OOP

  • C语言 第九章 结构体

    [TOC] 第九章结构体 结构体的定义 结构体定义2 指针表示结构体

网友评论

      本文标题:结构体

      本文链接:https://www.haomeiwen.com/subject/cansettx.html