-
Notifications
You must be signed in to change notification settings - Fork 93
/
Contents.swift
59 lines (41 loc) · 1.54 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//: Playground - noun: a place where people can play
import UIKit
struct Location{
let latitude: Double
let longitude: Double
var placeName: String?
init(latitude: Double, longitude: Double, placeName: String){
self.latitude = latitude
self.longitude = longitude
self.placeName = placeName
}
init(latitude: Double, longitude: Double){
self.latitude = latitude
self.longitude = longitude
}
init(){
latitude = 0.0
longitude = 0.0
}
// 使用Failable-Initializer
init?(coordinateString: String){
// 第二种写法,使用guard提高可读性
guard let commaIndex = coordinateString.rangeOfString(",")?.startIndex else{
return nil
}
guard let firstElement = Double(coordinateString.substringToIndex(commaIndex)) else{
return nil
}
guard let secondElement = Double(coordinateString.substringFromIndex(commaIndex.successor())) else{
return nil
}
latitude = firstElement
longitude = secondElement
}
}
let location = Location(coordinateString: "37.3230,-122.0322")
let location2 = Location(coordinateString: "37.3230,-122.0322")!
let location3 = Location(coordinateString: "37.3230&-122.0322")
let location4 = Location(coordinateString: "apple,-122.0322")
let location5 = Location(coordinateString: "37.3230,apple")
let location6 = Location(coordinateString: "Hello, World!")