I want to define a static variable to hold an NSDate object in Swift 2.2. I've tried the following:
static var interval:NSDate
var static interval:NSDate
var interval:static NSDate
None are working. I've written the same thing in Objective-C with the normal:
static NSString* str
But just isn't working in Swift.
Let me be more clear, I want to use a static interval in the didUpdateLocation method for locationManager so that a function will only happen after five minutes, but the user's location will still be seen in real time. This is how I did it in Objective C
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
static NSDate *previous; //<--- This in Swift
static int run = 0;
CLLocation *location = locations.lastObject;
if (run == 0 || [location.timestamp timeIntervalSinceDate:previous] > 10)
{
[_dbObj insert: location.coordinate.latitude : location.coordinate.longitude : [NSDate date] : location.timestamp];
previous = location.timestamp;
run++;
}
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2*1609.34, 2*1609.34);
[[self mapView] setRegion:viewRegion animated:YES];
}
From what I gather, I would need to create a class tat contains this static NSDate variable and instantiate it in the didUpdateLocations method. Yes? Thanks!