【问题标题】:Issue with adding Data to an AnyObject Var so that I could make native ads work将数据添加到 AnyObject Var 的问题,以便我可以制作原生广告
【发布时间】:2021-05-22 07:53:29
【问题描述】:
 for postdata in postdata {
        if index < tableViewItems.count {
            tableViewItems.insert(postdata, at: index)
            index += adInterval
        } else {
            break
        }
    }

我需要在同一个 AnyObject Var 上添加 PostData 广告和 Native Ads 才能使其正常工作,但我找不到添加 Post Data 的方法,因为它显示出现错误提示“参数类型'PostData' 应为类或类约束类型的实例。”非常感谢您的帮助,谢谢。

编辑 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

然后尝试通过执行此操作从 Firebase 追加数据

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”的值转换为预期的参数类型“广告”

【问题讨论】:

  • 什么是PostData,什么是tableViewItems?它们是如何声明的?
  • PostData 是来自 Firebase 的数据,而 tableViewItems 是我试图将我的 PostData 存储在其中的 AnyObject 数组

标签: ios swift xcode admob native-ads


【解决方案1】:

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

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

在你定义你的PostDataAdsNativeAds 继承自Ad 之后:

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

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

如果你想定义一个包含两种类型对象的数组,你可以去:

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

检索时可以检查它们的类型:

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 中定义的属性。

更新:

首先你的PostData1Ad 对象是相同的,你不需要复制它们。如果你真的想要两个类,你可以从Ad 继承PostData1

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] 数组,您可以执行以下操作:

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)

【讨论】:

  • 我试试这个,谢谢你的帮助
  • let postList = PostDataAd.PostData(postimage: postimage, publisher: postpublisher, timestring: postid, timestamp: timestamp) 是我用来存储来自 firebase 的数据并尝试将其附加到 self. ads.insert(postList, at:0) 出现错误提示无法将“PostDataAd.PostData”类型的值转换为预期的参数类型“广告”有什么帮助吗?
  • @AbuNabé 你能更新你的代码吗?
  • 非常感谢,非常感谢您的帮助
猜你喜欢
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多