简体   繁体   中英

Google Mobile Native Express ads iOS - Issues with [AnyObject]

I'm trying to implement Native Express Ads in my iOS Project, with swift.

First of all I have a class

class Events {
  var _eventName: String!
  var _realDate: Date!

    var realDate: Date {
        if _realDate == nil {
            _realDate = nil
        }
        return _realDate
    }

    var eventName: String {
        if _eventName == nil {
            _eventName = ""
        }
        return _eventName
    }

//...
}

Which class is getting a JSON from internet and it fills out a tableview. Also i have some functions with this like Searching through the names of the events and also i have a button that filters events by date. All these are working fine with the code below

var eventData = [Events]()
var filteredEvents = [Events]()

eventData.sort() { $0.realDate < $1.realDate }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filteredEvents = eventData.filter { events in
            return events.eventName.lowercased().contains(searchText.lowercased())
        }

So While i followed the example of the native express ads from here and here

First of all i changed

var eventData = [Events]()
var filteredEvents = [Events]()

to

var eventData = [AnyObject]()
var filteredEvents = [AnyObject]()

So the ads can be appended to the array as i show below in the function addNativeExpressAds() ! I have issues with the functions sort() and the filter .

for the sort() it says Value of type 'AnyObject' has no member 'realDate'

and for filter it says Value of type 'AnyObject' has no member 'eventName'

Update 1. After some research on google i found out this could be a solution for sort() function

eventData.sort() { (($0 as! Events).realDate ) < ($1 as! Events).realDate }

Although the eventData Array also includes GADNativeExpressAdView since in the example that i have linked above there is this block of code that downloads the express native ads and appends them to the AnyObject Array eventData see below

func addNativeExpressAds() {
    var index = adInterval
    // Ensure subview layout has been performed before accessing subview sizes.
    nearMeTable.layoutIfNeeded()
    while index < eventData.count {
        let adSize = GADAdSizeFromCGSize(
            CGSize(width: nearMeTable.contentSize.width, height: adViewHeight))
        guard let adView = GADNativeExpressAdView(adSize: adSize) else {
            print("GADNativeExpressAdView failed to initialize at index \(index)")
            return
        }
        adView.adUnitID = adUnitID
        adView.rootViewController = self
        adView.delegate = self


        eventData.insert(adView, at: index) //here it inserts the ads
        adsToLoad.append(adView)
        loadStateForAds[adView] = false

        index += adInterval
    }
}

and by this I get the error

Could not cast value of type 'GADNativeExpressAdView' (0x10046ebd8) to 'Events_Near_Me.Events' (0x10046fb90).

So i can't sort the array of any object cause the GADNativeExpressAdView ads inside my array.

Any idea about this one?

First of all in your sort function you force a cast ! to an Event type but your array does not only contain Event object only. So you force the cast and your code fails.

eventData.sort { (first, second) in  
    if let first = first as? Event,
        let second = second as? Event {
        return first.realDate < second.realDate
    }
    if let fist = first as? GADNativeExpressAdView,
        let second = second as? GADNativeExpressAdView {
        return \\ compare?
    }

    return false
}

This is a sample code, I haven't tested it to see if it works as expected but it should give you an idea on how to proceed.

PS I do not think it is a good practice to mix your data with views. You should keep your array clean of views and have it only contain data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM