所以这就是我现在所拥有的。
的数组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")
我的自定义BarAnnotation类的数组。阵列的大小明显固定。我一直试图从JSON文件中加载条形数据,然后从那里创建数组。
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)
}
然后我想用这个数组“test”替换吧。但是,test是一个NSMutableArray类型,bars是一个大小固定的BarAnnotations数组。当我尝试转换它时会导致错误。我觉得这是一个简单的解决方案,我只是在这里度过了难关。任何人吗?
这是我的BarAnnotation类
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
}
然后我尝试在这里访问它
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)
}
}
这是错误
答案 0 :(得分:0)
您的BarAnnotation
都是从MKPointAnnotation
继承并声明MKPointAnnotation
的属性 - 这没有任何意义。我假设您要将bar.annotation作为注释添加到地图视图中 - 由于BarAnnotation
继承自MKPointAnnotation
,您只需添加bar
本身。
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
}
}
我发现的一件事是,即使BarAnnotation
继承自MKPointAnnotation
,如果您尝试直接访问coordinate
属性,Swift会给出弃用错误。解决方案是将对象强制转换为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)
}
}