So essentialy this is what I have current. An array of
var bars = [BarAnnotation(latitude: 42.022352, longitude: -93.650413, name: "Micky's Irish Pub", deal: "$2 Drinks 9PM-1AM"),
BarAnnotation(latitude: 42.021948, longitude: -93.650348, name: "Cy's Roost", deal: "$2 Drinks 9PM-1AM")
An array of my custom BarAnnotation class. The array is fixed in size obviously. What I've been trying to do it load the bar data from a JSON file and then create the array from there.
func retrieveData(){
var filePath = NSBundle.mainBundle().pathForResource("test", ofType: "json")
var data = NSData.dataWithContentsOfMappedFile(filePath!) as NSData
var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var test = [] as NSMutableArray
let barArray = json["bars"] as NSArray
for bar in barArray{
var name = bar["name"] as NSString
var deal = bar["deal"] as NSString
var lat = bar["lat"] as Double
var long = bar["long"] as Double
var negLong = -long
var newBar = BarAnnotation(latitude: lat, longitude: negLong, name: name, deal: deal)
test.addObject(newBar)
}
bars.arrayByAddingObjectsFromArray(test)
}
Then I wanted to replace bars with this array "test". However, test is a type NSMutableArray and bars is an array of BarAnnotations fixed in size. When I try to convert it's causing errors. I feel like this is a simple fix I'm just having a hard time here. Anyone?
Here is my BarAnnotation class
class BarAnnotation: MKPointAnnotation {
var latitude, longitude, distance: Double
var name, imageName, deal: String
var location: CLLocationCoordinate2D
var loc: CLLocation
var annotation: MKPointAnnotation
init(latitude: Double, longitude: Double, name: String, deal: String) {
self.latitude = latitude
self.longitude = longitude
self.name = name
self.imageName = "image1.jpg"
self.deal = deal
self.distance = 0
self.location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.loc = CLLocation(latitude: latitude, longitude: longitude)
self.annotation = MKPointAnnotation()
annotation.setCoordinate(self.location)
annotation.title = name
annotation.subtitle = deal
}
Then I try to access it here
for bar in bars {
var barName = bar.name.stringByReplacingOccurrencesOfString("\'", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var searchString = searchBar.text.stringByReplacingOccurrencesOfString("\'", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
if barName.rangeOfString(searchString) != nil || barName.lowercaseString.rangeOfString(searchString.lowercaseString) != nil {
var span = MKCoordinateSpanMake(0.005, 0.005)
var region = MKCoordinateRegion(center: bar.location, span: span)
mapView.setRegion(region, animated: true)
mapView.selectAnnotation(bar.annotation, animated: true)
}
}
and this is the error