'featureLayer' is deprecated: Use 'layer' instead

385
1
05-22-2022 08:34 PM
VinceCarloSantos
New Contributor

Hi, I received this warning on my old app, how do I resolve it if my usage is like this?

func drawCallout(_ feature: AGSFeature) {
          let e: AGSEnvelope = (feature.geometry?.extent)!
          let p: AGSPoint = e.center
          let pointSymbol: AGSMultilayerPointSymbol = (feature.featureTable?.featureLayer?.renderer?.symbol(for: feature)) as! AGSMultilayerPointSymbol
          var screenPoint: CGPoint = (self.mapView as! AGSMapView).location(toScreen: p)
          screenPoint.y -= pointSymbol.size/2
          let newp: AGSPoint = (self.mapView as! AGSMapView).screen(toLocation: screenPoint)
          (self.mapView as! AGSMapView).callout.show(for: feature, tapLocation: newp, animated: true)
     }



I cant see the renderer completion for the new 'layer' instead of 'featureLayer'. 

Thanks!

Tags (1)
0 Kudos
1 Reply
Nicholas-Furness
Esri Regular Contributor

Hi.

The deprecated featureLayer property is of type AGSFeatureLayer, whereas the layer property that the deprecation warning tells you to use is of type AGSLayer. We had to make this change when we introduced Dimension and Annotation layers. You need to cast to AGSFeatureLayer.

Something like this will work:

let pointSymbol: AGSMultilayerPointSymbol = (feature.featureTable?.layer as? AGSFeatureLayer)?.renderer?.symbol(for: feature) as! AGSMultilayerPointSymbol

However, the use of a forced unwrap at the end is a bit unnerving to me - it will crash your app if the layer's renderer symbol changes from a AGSMultilayerPointSymbol to some other symbol type (perhaps if the map is authored differently). You might consider something like this:

func drawCallout(_ feature: AGSFeature) {
    let e: AGSEnvelope = (feature.geometry?.extent)!
    let p: AGSPoint = e.center
    guard let mapView = self.mapView,
          let pointSymbol: AGSMultilayerPointSymbol = (feature.featureTable?.layer as? AGSFeatureLayer)?.renderer?.symbol(for: feature) as? AGSMultilayerPointSymbol else { return }
    var screenPoint: CGPoint = mapView.location(toScreen: p)
    screenPoint.y -= pointSymbol.size/2
    let newp: AGSPoint = mapView.screen(toLocation: screenPoint)
    mapView.callout.show(for: feature, tapLocation: newp, animated: true)
}

However, just the first change will work if you're confident your original code is safe.

Hope that helps.

0 Kudos