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?