结构体

作者: 心底碎片 | 来源:发表于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都是结构体

    相关文章

      网友评论

          本文标题:结构体

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