1

I am using Google Ad and I am converting GADUnifiedNativeAd to AnyObject and converting back original GADUnifiedNativeAd is failing in Swift 5 . What can be the reason? Before this code working fine in Swift 4.2 and below:

GADUnifiedNativeAd

on delegate response of fetching Ad

func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
    let anyObjectTo: AnyObject = nativeAd
    if let nativeAd = anyObjectTo as? GADUnifiedNativeAd {
        print("success")
    } else {
        print("failing")
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
TheCodeTalker
  • 685
  • 6
  • 14
  • Why are you converting to/from `AnyObject`? Can you provide more context? – Jacob Relkin Feb 11 '20 at 10:10
  • `nativeAd` is `GADUnifiedNativeAd` by default, why do you have to convert it to `AnyObject` and then converting it back? – Ahmad F Feb 11 '20 at 10:11
  • is that `as?` supposed to be `is`? – Sulthan Feb 11 '20 at 10:15
  • in my project we holding in array of AnyObject for all type of ads and converting as per the type we are converting like GADUnifiedNativeAd or GADNativeCustomTemplateAd some things like this – TheCodeTalker Feb 11 '20 at 10:16
  • @Sulthan I want to convert is back to if let adObject = adInfo as? GADUnifiedNativeAd { customNativeAd = adObject } which is failing – TheCodeTalker Feb 11 '20 at 10:17
  • one things it solve like use GADUnifiedNativeAd but I want to know the reason failing – TheCodeTalker Feb 11 '20 at 10:21
  • 2
    The code makes no sense at all. The `nativeAd` parameter is a non-optional `GADUnifiedNativeAd` object. This is Swift. It can't be something else. – vadian Feb 11 '20 at 10:30
  • @vadian the issue I am suspecting is GADUnifiedNativeAd is objc class . isa pointer is changing to GADUnifiedNativeAd -> GADNUnifiedNativeAd of type on assign to AnyObject which is different type. which I can see debugger. Printing description of anyObjectTo: – TheCodeTalker Feb 11 '20 at 10:38
  • i think you're missing `let` in `if anyObjectTo as? GADUnifiedNativeAd` – S1LENT WARRIOR Feb 11 '20 at 10:46
  • I am experiencing the same – Neil Galiaskarov Apr 29 '20 at 19:19

0 Answers0