結(jié)構(gòu)體

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

結(jié)構(gòu)體的構(gòu)造函數(shù)

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){//省略構(gòu)造函數(shù)的名字
        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可以失敗的構(gòu)造函數(shù)

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
        }
*/
        //繼續(xù)簡化
        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")

在結(jié)構(gòu)體和枚舉中定義方法

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
    }
    //在結(jié)構(gòu)體和枚舉中叫做方法
    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)

結(jié)構(gòu)體和枚舉是值類型Value Type

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

#######Array Dictionary,Set都是結(jié)構(gòu)體
#######Int, Float, Double, Bool, String都是結(jié)構(gòu)體

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末繁莹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子寂祥,更是在濱河造成了極大的恐慌裤园,老刑警劉巖语泽,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件力喷,死亡現(xiàn)場離奇詭異足删,居然都是意外死亡眨层,警方通過查閱死者的電腦和手機匣距,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門面哥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人毅待,你說我怎么就攤上這事尚卫。” “怎么了尸红?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵吱涉,是天一觀的道長。 經(jīng)常有香客問我外里,道長怎爵,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任盅蝗,我火速辦了婚禮鳖链,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘墩莫。我一直安慰自己芙委,他們只是感情好,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布狂秦。 她就那樣靜靜地躺著灌侣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪故痊。 梳的紋絲不亂的頭發(fā)上顶瞳,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機與錄音愕秫,去河邊找鬼慨菱。 笑死,一個胖子當著我的面吹牛戴甩,可吹牛的內(nèi)容都是我干的符喝。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼甜孤,長吁一口氣:“原來是場噩夢啊……” “哼协饲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起缴川,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤茉稠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后把夸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體而线,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了膀篮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嘹狞。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赎离,我是刑警寧澤粮彤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嫂丙,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望规哲。 院中可真熱鬧跟啤,春花似錦、人聲如沸唉锌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽袄简。三九已至腥放,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绿语,已是汗流浹背秃症。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留吕粹,地道東北人种柑。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像匹耕,于是被迫代替她去往敵國和親聚请。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容