-1

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 enter image description here

leerob
  • 2,876
  • 1
  • 18
  • 38
  • "When I try to convert it's causing errors" What did you try and exact what "errors" did it cause? – matt Dec 10 '14 at 04:05
  • When I'm looping through the bars array, I try to access a property of BarAnnotation which is location. However, it won't let me access that property because the bars array used to be an array of BarAnnotations and now it's an NSMutableArray. – leerob Dec 10 '14 at 04:12
  • You are not answering the question. Do you want help or not? I see no location property anywhere in your code. Nor do I see you accessing any property of anything in bars array. Show the actual code that is causing a problem - not the preparatory stuff, the actual code where the problem actually happens! - and state _exactly_ what the problem is at _exactly_ what line. Does the code refuse to compile? Do you crash? If you crash, what's the crash log? – matt Dec 10 '14 at 04:19
  • So is that the only compile error or is it one of many? – matt Dec 10 '14 at 05:04
  • Yes, that is the only error. But if I comment that out (it's the code for searching the map) it runs but none of the annotations load on the map. – leerob Dec 10 '14 at 05:13
  • I can post the full code if you'd like! – leerob Dec 10 '14 at 05:15

1 Answers1

0

Your BarAnnotation is both inheriting from MKPointAnnotation and declaring a property of an MKPointAnnotation - this doesn't make sense. I presume you are adding bar.annotation as the annotation to your map view - Since BarAnnotation inherits from MKPointAnnotation you can just add the bar itself.

class BarAnnotation: MKPointAnnotation {

        var distance: Double
        var name, imageName, deal: String


        init(latitude: Double, longitude: Double, name: String, deal: String) {
            self.distance=0
            self.name = name
            self.imageName = "image1.jpg"
            self.deal = deal
            super.init()
            self.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            self.title = name
            self.subtitle = deal
    }

}

One thing I found, is that even though BarAnnotation inherits from MKPointAnnotation, Swift gives a deprecation error if you try and access the coordinate property directly. The solution is to cast the object to an MKPointAnnotation

for bar in bars  {
    let annotation=bar as MKPointAnnotation
    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: annotation.coordinate, span: span)
        mapView.setRegion(region, animated: true)
        mapView.selectAnnotation(bar, animated: true)
    }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • So what error are you getting? The error message in your screenshot is nothing to do with arrays – Paulw11 Dec 10 '14 at 04:52
  • The error in the message is because the arrays aren't being copied correctly. The new array "test" is a NSMutableArray. When I try to set the bars array (which was originally an array of BarAnnotation objects) to the test array "bars.arrayByAddingObjectsFromArray(test)" I believe is where the issue is but I can't get it to work. – leerob Dec 10 '14 at 05:00
  • That's not what the error message says. NSArray and NSMutableArray are not "typed" in terms of their contents – Paulw11 Dec 10 '14 at 05:01
  • I know, but what the error message says is that the .location value is wrong. Thats because it won't allow me to do .location on that current bar. – leerob Dec 10 '14 at 05:03
  • Did you try MKCoordinateRegionMake? – Paulw11 Dec 10 '14 at 05:09
  • And if it was a type problem, how come it is happy with bar.name? – Paulw11 Dec 10 '14 at 05:12
  • Also, why not just use the coordinate property that comes for free with MkPointAnnotation? – Paulw11 Dec 10 '14 at 05:14
  • Yes, I tried that Paul. When it's using bar.name it's not using BarAnnotations name. It's just using the generic .name. – leerob Dec 10 '14 at 05:14
  • I'm not sure exactly what you mean by that, could you explain? – leerob Dec 10 '14 at 05:16
  • Looking at your code, it doesn't make sense. You class inherits from MKPointAnnotation but also has a property that is an MKPointAnnotation. As your class inherits from MKPointAnnotation it already has a coordinate property available that is a CLLocation2D as well as title and subtitle properties – Paulw11 Dec 10 '14 at 05:18
  • Ahh, I see what you mean. I can definitely change that, but I don't think that's the issue here. Do you have any other suggestions? One thing I noticed was that in the loop "for bar in bars", if I said var newBar = bar as BarAnnotation then I was able to inherit it's property. That fixed the error but it didn't make the annotations show. So for some reason when I'm copying the array it's not keeping it the same type and letting me access it's properties. – leerob Dec 10 '14 at 05:24
  • That's not how it works. The object in an array is still the object. You can say "as X" to get the compiler to cast it, but that doesn't change its actual type. Comment the line so it compiles and set a breakpoint - you can then inspect bar and see what it is – Paulw11 Dec 10 '14 at 05:26
  • It looks like when I'm creating the new BarAnnotations in the RetrieveData method, it's putting all zeros or empty strings for the values. I don't see why though... that's how I created the BarAnnotations before. – leerob Dec 10 '14 at 05:34
  • I think you are missing () in your declaration of test – Paulw11 Dec 10 '14 at 05:36
  • Nevermind, I left a line commented accidentally. When looping through the test array, all of the BarAnnotations have the correct values. But after I transfer it to the bars array, they are now AnyObject type instead of BarAnnotation. – leerob Dec 10 '14 at 05:45
  • Where do you declare `bars` ? – Paulw11 Dec 10 '14 at 05:46
  • So bars was being declared globally. I was creating it empty and then trying to copy the test array over to it. I don't know why I didn't do this earlier but instead I just got rid of the test array. I declared the bars array as a NSMutableArray globally and then populated it in the RetrieveData array. It's now somewhat working with the lines commented that were creating the initial error. I think the issue now is what you were saying earlier, I'm not using the MKPointAnnotaions location properties. Could you submit an answer revising my BarAnnotation so I can see what you meant? – leerob Dec 10 '14 at 05:52
  • The lines that were creating the initial error were used to search the map. Right now the application loads and the annotations show correctly (showing they were created correctly and the data is correct) but when I search it doesn't zoom in like it should. I think this is because the location properties of the BarAnnotations are messed up. – leerob Dec 10 '14 at 05:54
  • Weird. When I'm looping through the bars in the search method (shown above where I try to access it) if I cast the bar as a BarAnnotation it works correctly now. I would still appreciate your revision of my BarAnnotation class to make it more correct. Thanks though for helping me sort through this error haha. – leerob Dec 10 '14 at 06:01