将数据添加到AnyObject Var以便我可以使原生广告正常工作的问题 [英] Issue with adding Data to an AnyObject Var so that I could make native ads work

查看:75
本文介绍了将数据添加到AnyObject Var以便我可以使原生广告正常工作的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 for postdata in postdata {
        if index < tableViewItems.count {
            tableViewItems.insert(postdata, at: index)
            index += adInterval
        } else {
            break
        }
    }

我需要在同一个AnyObject Var上同时添加PostData广告和原生广告,以使其正常工作,但我找不到添加Post Data的方法,因为它说出现了一个错误,指出参数"类型"PostData",预期是类或类约束类型的实例."非常感谢您的协助.

I'll need to add both PostData ads and Native Ads on the same AnyObject Var for me to get it to work and I can't find a way to add the Post Data because it says an error appears saying "Argument type 'PostData' expected to be an instance of a class or class-constrained type." Assistance would be very much appreciated, thank you.

编辑1

class Ad {
    var postimage: String!
    var publisher: String!
    var timestring: String!
    var timestamp = Date().timeIntervalSince1970
}

class PostDataAd: Ad {

    // Declare here your custom properties
    struct PostData1
    {
        var postimage: String
        var publisher: String
        var timestring : String
        var timestamp = Date().timeIntervalSince1970
    }
}

class NativeAd: Ad {
    // Declare here your custom properties
    struct NativeAd
    {
        var nativeAds: [GADUnifiedNativeAd] = [GADUnifiedNativeAd]()
    }
}

我的模型类,用于将两个数据合并为一个AnyObject Var

My model class to merge both Data into one AnyObject Var

,然后尝试通过此操作从Firebase附加数据

and then trying to append the Data from Firebase by doing this

var ads: [Ad] = [PostDataAd(), NativeAd()]

let postList = PostDataAd.PostData1(postimage: postimage, publisher: 
postpublisher, timestring: postid, timestamp: timestamp)

self.ads.insert(postList, at:0)

发生错误,提示无法将类型"PostDataAd.PostData1"的值转换为预期的参数类型"Ad"

an error occurs saying Cannot convert value of type 'PostDataAd.PostData1' to expected argument type 'Ad'

推荐答案

我希望我正确地得到了你想要的东西.因此,基本上,您有两个对象要存储在 AnyObject 下的一个数组中.如果正确的话,我建议您朝不同的方向前进.这是一个很好的示例,您可以在其中使用子类化.您可以声明一个名为 Ad 的类,在其中定义通用属性,这些通用属性对于 PostDataAds NativeAds 都是正确的.

I hope I got what you want correctly. So basically you have two objects which you want to store in one array, under AnyObject. If that is correct, I recommend you to go in a bit of different direction. It is a nice example where you can use subclassing. You can declare a class called Ad, where you define the common properties which will be true for both PostDataAds and NativeAds.

class Ad {
    // Add here the common elements you want to retrieve from both classes
    var name: String = ""
}

定义从 Ad 继承的 PostDataAds NativeAds 后:

class PostDataAd: Ad {
    // Declare here your custom properties
}

class NativeAd: Ad {
    // Declare here your custom properties
}

如果要使用两种类型的对象定义数组,则可以:

And if you want to define an array with two types of objects you can go:

let ads: [Ad] = [PostDataAd(), NativeAd()]

检索时,您可以检查其类型:

When retrieving you can check their type:

if let item = ads.first as? PostDataAd {
     // The Ad is a PostDataAd
} else if let item = ad as? NativeAd {
    // The Ad is a NativeAd
}

或者在某些情况下,您甚至都不知道确切的类型,因为您无需检查即可访问 Ad 中定义的属性.

Or at some cases you don't even how to know the exact type, as you can access the properties defined in Ad without checking.

更新:

首先,您的 PostData1 Ad 对象是相同的,您无需复制它们.如果您确实要拥有两个类,则可以从 Ad 继承 PostData1 .

First of all your PostData1 and Ad objects are the same, you don't need to duplicate them. If you really want to have two classes you can inherit PostData1 from Ad.

class Ad {
    var postimage: String
    var publisher: String
    var timestring: String
    var timestamp = Date().timeIntervalSince1970

    // You can add timestamp here also if you wish
    init(postimage: String, publisher: String, timestring: String) {
        self.postimage = postimage
        self.publisher = publisher
        self.timestring = timestring
    }
}

class PostDataAd: Ad {
    // Define some custom properties
}

如果要将 PostData 附加到 [Ad] 数组,则可以执行以下操作:

And if you want to append PostData to the [Ad] array, you would do the following:

var ads: [Ad] = []
// Replace with your values
let postList = PostDataAd(postimage: "", publisher: "", timestring: "") 
ads.insert(postList, at: 0)

// Appending NativeAd works also
let nativeAdd = NativeAd(postimage: "", publisher: "", timestring: "")
ads.append(nativeAdd)

这篇关于将数据添加到AnyObject Var以便我可以使原生广告正常工作的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆