-
Notifications
You must be signed in to change notification settings - Fork 0
/
Location.swift
72 lines (58 loc) · 1.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Location.swift
// MyLocations
//
// Created by Antonio Alves on 1/31/16.
// Copyright © 2016 Antonio Alves. All rights reserved.
//
import Foundation
import CoreData
import MapKit
import UIKit
class Location: NSManagedObject, MKAnnotation {
// Insert code here to add functionality to your managed object subclass
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 hasPhoto:Bool {
return photoID != nil
}
var photoPath: String {
assert(photoID != nil, "No photo ID set")
let filename = "Photo-\(photoID!.intValue).jpg"
return (applicationDocumentsDirectory as NSString).appendingPathComponent(filename)
}
var photoImage: UIImage? {
return UIImage(contentsOfFile: photoPath)
}
class func nextPhotoID() -> Int {
let userDefaults = UserDefaults.standard()
let currentID = userDefaults.integer(forKey: "PhotoID")
userDefaults.set(currentID + 1, forKey: "PhotoID")
userDefaults.synchronize()
return currentID
}
func removePhotoFile() {
if hasPhoto {
let path = photoPath
let fileManager = FileManager.default()
if fileManager.fileExists(atPath: path) {
do {
try fileManager.removeItem(atPath: path)
} catch {
print("Error removing file: \(error)")
}
}
}
}
}