问题描述
尝试从服务访问响应数据以显示到TodayExtenstion小部件中
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()
}
当尝试访问另一个能够获取已保存数据的viewcontroller时.
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?
当我选择目标projectName时,我需要配置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.
我无法弄清楚为什么它返回nil!您的投入将真正指导我实现目标.
I am unable to figure it out why its returns nil! Your inputs will really guide me to achieve it.
错误消息:
在展开可选值时意外发现nil致命错误:展开一个可选值时意外发现nil
Unexpectedly found nil while unwrapping an Optional valueFatal error: Unexpectedly found nil while unwrapping an Optional value
推荐答案
嘿,就像@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)
}
}
这篇关于如何将响应数据设置到TodayExtenstion小部件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!