【问题标题】:Getting an error: "Extensions may not contain stored properties"收到错误:“扩展可能不包含存储的属性”
【发布时间】:2017-10-09 05:55:30
【问题描述】:

我是 Swift 和 xcode 的新手。我试图在 UISplitViewControllerDelegate 中声明一个 GoogleMobileAds 变量,但出现错误:扩展可能不包含存储的属性。

这是我的代码:

import GoogleMobileAds

extension MainBiblePagerVC: UISplitViewControllerDelegate{
    // Setup Navigation Items in Bible Page
    var interstitial: GADInterstitial!

谢谢!

【问题讨论】:

  • 您查看过these search results 的错误信息吗?
  • 你应该阅读 Swift 书籍的Extensions 章节。
  • 尝试理解错误信息。这个很清楚:interstitial 是一个存储属性,扩展可能不包含存储属性

标签: swift


【解决方案1】:

您不能在扩展中声明存储的属性,只能声明计算的属性

所以你有两种方法:

  1. 在你的类 MainBiblePagerVC 中声明存储的属性

  2. 使用计算属性:

    extension MainBiblePagerVC: UISplitViewControllerDelegate{
        var interstitial: GADInterstitial! {
            // add object initialization here
            let object = GADInterstitial()
            // set its parameters 
            return object
        }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多