我想访问该字符串,该字符串在一个类的另一个swift文件中。
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let clubnamehome: String
var string: String = {
let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.co....") ? .appendingPathComponent("hello")
let data =
try !Data(contentsOf: url!)
let string = String(data: data, encoding: .utf8) !
return string
}()
}
我想放在这里:
class NetworkManager: ObservableObject {
func fetchData(completion: @escaping(Post) - > Void) {
let teamId = >>insert string<<<
我使用了MyTeamWidget.SimpleEntry.string,但是它不起作用。 我还尝试了@Published或静态var。
我也尝试过类似的方法。但这不起作用:
let teamId = {
let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.co....") ? .appendingPathComponent("hello")
let data =
try !Data(contentsOf: url!)
let string = String(data: data, encoding: .utf8)!
return string
}()
有什么建议吗?
编辑: 解决方案正在使用此:
class NetworkManager: ObservableObject {
func fetchData(completion: @escaping (Post) -> Void) {
let teamId: String = {
let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group....")?.appendingPathComponent("hello")
let data = try! Data(contentsOf: url!)
let string = String(data: data, encoding: .utf8)!
return string
}()