-
Notifications
You must be signed in to change notification settings - Fork 0
/
Location.swift
58 lines (49 loc) · 1.34 KB
/
Location.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
import Foundation
import CoreData
import MapKit
class Location: NSManagedObject, MKAnnotation {
class func nextPhotoID() -> Int {
let userDefaults = NSUserDefaults.standardUserDefaults()
let currentID = userDefaults.integerForKey("PhotoID")
userDefaults.setInteger(currentID + 1, forKey: "PhotoID")
userDefaults.synchronize()
return currentID
}
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(latitude, longitude)
}
var title: String? {
if locationDescription.isEmpty {
return "(No Description)"
} else {
return locationDescription
}
}
var subtitle: String? {
return category
}
var photoImage: UIImage? {
return UIImage(contentsOfFile: photoPath)
}
var hasPhoto: Bool {
return photoID != nil
}
var photoPath: String {
assert(photoID != nil, "No photo ID set")
let filename = "Photo-\(photoID!.integerValue).jpg"
return (applicationDocumentsDirectory as NSString).stringByAppendingPathComponent(filename)
}
func removePhotoFile() {
if hasPhoto {
let path = photoPath
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(path) {
do {
try fileManager.removeItemAtPath(path)
} catch {
print("Error removing file: \(error)")
}
}
}
}
}