<bdo id='nycgC'></bdo><ul id='nycgC'></ul>
<tfoot id='nycgC'></tfoot>

      <legend id='nycgC'><style id='nycgC'><dir id='nycgC'><q id='nycgC'></q></dir></style></legend>

      1. <small id='nycgC'></small><noframes id='nycgC'>

        <i id='nycgC'><tr id='nycgC'><dt id='nycgC'><q id='nycgC'><span id='nycgC'><b id='nycgC'><form id='nycgC'><ins id='nycgC'></ins><ul id='nycgC'></ul><sub id='nycgC'></sub></form><legend id='nycgC'></legend><bdo id='nycgC'><pre id='nycgC'><center id='nycgC'></center></pre></bdo></b><th id='nycgC'></th></span></q></dt></tr></i><div id='nycgC'><tfoot id='nycgC'></tfoot><dl id='nycgC'><fieldset id='nycgC'></fieldset></dl></div>

        如何将响应数据设置到 TodayExtension 小部件中

        时间:2023-09-09
          <bdo id='14Jvt'></bdo><ul id='14Jvt'></ul>

              <tbody id='14Jvt'></tbody>
          1. <tfoot id='14Jvt'></tfoot>

              <small id='14Jvt'></small><noframes id='14Jvt'>

              • <legend id='14Jvt'><style id='14Jvt'><dir id='14Jvt'><q id='14Jvt'></q></dir></style></legend>

                  <i id='14Jvt'><tr id='14Jvt'><dt id='14Jvt'><q id='14Jvt'><span id='14Jvt'><b id='14Jvt'><form id='14Jvt'><ins id='14Jvt'></ins><ul id='14Jvt'></ul><sub id='14Jvt'></sub></form><legend id='14Jvt'></legend><bdo id='14Jvt'><pre id='14Jvt'><center id='14Jvt'></center></pre></bdo></b><th id='14Jvt'></th></span></q></dt></tr></i><div id='14Jvt'><tfoot id='14Jvt'></tfoot><dl id='14Jvt'><fieldset id='14Jvt'></fieldset></dl></div>

                • 本文介绍了如何将响应数据设置到 TodayExtension 小部件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  尝试从服务访问响应数据以显示到 TodayExtension 小部件中

                  Trying to access response data from service to show into TodayExtenstion Widget

                  import Foundation
                  
                  struct MarketIndex:Codable {
                  
                      let indicesName: String
                      let indicesValue: String
                      let dateValue : String
                      let indicesChangeValue : String
                      let changePercentage : String
                      let indexVolume: String?
                  }
                  
                  struct MarketIndexCache {
                      static let key = "MARKET_INDEX_KEY"
                      static func save(_ value: [MarketIndex]!) {
                          UserDefaults.standard.set(try? PropertyListEncoder().encode(value), forKey: key)
                      }
                      static func get() -> [MarketIndex]! {
                          var marketIndex: [MarketIndex]!
                          if let data = UserDefaults.standard.value(forKey: key) as? Data {
                              marketIndex = try? PropertyListDecoder().decode([MarketIndex].self, from: data)
                              return marketIndex!
                          } else {
                              return marketIndex
                          }
                      }
                      static func remove() {
                          UserDefaults.standard.removeObject(forKey: key)
                      }
                  }
                  

                  MarketIndexClient 服务调用类代码片段

                  MarketIndexClient Service Call class code snippet

                  marketIndexClient.fetchMarketIndex(symbol: "AAPL,MSFT"){ marketIndex in
                      self.stockIndex = marketIndex
                      completion()
                      MarketIndexCache.save(self.stockIndex)
                      self.dispatchGroupCalls.leave()
                  
                  }
                  

                  当尝试访问另一个能够获取保存数据的视图控制器时.

                  When Trying to access another viewcontroller able to get saved data.

                  override func viewDidLoad() {
                      super.viewDidLoad()
                      marketIndex = MarketIndexCache.get()
                  }
                  

                  当尝试从 TodayWidgetViewController 访问它时,它返回 nil.

                  When trying to access its from TodayWidgetViewController its returns nil.

                  import UIKit
                  import NotificationCenter
                  class TodayWidgetViewController
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          marketIndex = MarketIndexCache.get()
                          print(marketIndex as Any)
                  
                      }
                  

                  两个目标的 UserDefaults 是否不会相互共享数据?

                  Is UserDefaults for two targets will not share the data with each other?

                  当我选择目标项目名称时,我是否需要配置 TodayExtenstion,因此当我的项目在设备上运行时,两者都需要相互同步.

                  When I select target projectName do I need to configure the TodayExtenstion so both need to sync with each other when my project run on device.

                  在运行应用程序项目时,我无法调试 TodayExtenstionWidget.

                  I unable to debug TodayExtenstionWidget when running application project.

                  这是我在使用 TodayExtenstion 时发现的观察结果.

                  This are my observation found while working with TodayExtenstion.

                  我无法弄清楚为什么它的回报为零!您的意见将真正指导我实现它.

                  I am unable to figure it out why its returns nil! Your inputs will really guide me to achieve it.

                  错误信息:

                  在展开可选值时意外发现 nil致命错误:在展开可选值时意外发现 nil

                  推荐答案

                  嘿,正如@LowKostKustomz 解释的那样,您需要在主应用程序和扩展程序中激活应用程序组.然后在从主应用程序和扩展程序保存时,尝试类似

                  Hey as @LowKostKustomz explained, you need to activate app groups in both main app and extension. Then while saving from main app as well as extension, try something like

                  struct MarketIndexCache {
                      static let key = "MARKET_INDEX_KEY"
                      static let groupBundleID = <#you group identifier#>
                      static func save(_ value: [MarketIndex]!) {
                          let defaults = UserDefaults.init(suiteName: groupBundleID)
                          defaults?.set(try? PropertyListEncoder().encode(value), forKey: key)
                      }
                      static func get() -> [MarketIndex]! {
                          var marketIndex: [MarketIndex]!
                          if let data = defaults?.value(forKey: key) as? Data {
                              let defaults = UserDefaults.init(suiteName: groupBundleID)
                              marketIndex = try? PropertyListDecoder().decode([MarketIndex].self, from: data)
                              return marketIndex!
                          } else {
                              return marketIndex
                          }
                      }
                      static func remove() {
                          let defaults = UserDefaults.init(suiteName: groupBundleID)
                          defaults?.removeObject(forKey: key)
                      }
                  }
                  

                  这篇关于如何将响应数据设置到 TodayExtension 小部件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android 更改小部件背景图片 下一篇:每个 notifyAppWidgetViewDataChanged() 仅调用一次 RemoteViewFactory

                  相关文章

                • <tfoot id='b8fs6'></tfoot>

                  <i id='b8fs6'><tr id='b8fs6'><dt id='b8fs6'><q id='b8fs6'><span id='b8fs6'><b id='b8fs6'><form id='b8fs6'><ins id='b8fs6'></ins><ul id='b8fs6'></ul><sub id='b8fs6'></sub></form><legend id='b8fs6'></legend><bdo id='b8fs6'><pre id='b8fs6'><center id='b8fs6'></center></pre></bdo></b><th id='b8fs6'></th></span></q></dt></tr></i><div id='b8fs6'><tfoot id='b8fs6'></tfoot><dl id='b8fs6'><fieldset id='b8fs6'></fieldset></dl></div>
                  <legend id='b8fs6'><style id='b8fs6'><dir id='b8fs6'><q id='b8fs6'></q></dir></style></legend>

                      <small id='b8fs6'></small><noframes id='b8fs6'>

                      • <bdo id='b8fs6'></bdo><ul id='b8fs6'></ul>