如何从 Swift 中的结构闭包中 return 取值?
How to return value from a closure in a Struct in Swift?
我正在从网站检索数据。
网络运行良好。从 JSON.
中正确解析数据
几个参考 - 在这个结构中:
- 回复是 JSON
的数据模型
- PrepareQuestions 是一个执行解析的函数(我在同一结构的扩展中有它)
我想在这个结构中有一个对象(downloadedData - 'Replies' 是带有数据模型的结构)包含所有下载的信息,但由于“self 是不可变的”而导致我出错捕获”。有什么建议么?谢谢!
struct QuestionsManager {
var downloadedData:Replies?
func useData() {
manageQuestions(url: K.urlForRetreival, numberOfQuestions: K.numberOfSquares) { [self] (replies, error) in
if let replies = replies {
DispatchQueue.main.async {
downloadedData = replies // Here I got the error
}
}
}
}
func manageQuestions(url: String, numberOfQuestions: String, myCompletion: @escaping (Replies?, Error?)->()) {
let generatedUrl = URL(string: url + numberOfQuestions)!
let urlSession = URLSession(configuration: .default)
let task = urlSession.dataTask(with: generatedUrl) { (data, response, error) in
if error == nil {
if let fetchedData = data {
let fetchedProcessedData = prepareQuestions(data: fetchedData)
myCompletion(fetchedProcessedData, nil)
return
}
} else {
myCompletion(nil, error)
return
}
}
task.resume()
}
}
struct is a value type. For value types, only methods explicitly
marked as mutating can modify the properties of self, so this is not
possible within a computed property.
If you change struct to be a class then your code compiles without
problems.
Structs are value types which means they are copied when they are
passed around.So if you change a copy you are changing only that copy,
not the original and not any other copies which might be around.If
your struct is immutable then all automatic copies resulting from
being passed by value will be the same.If you want to change it you
have to consciously do it by creating a new instance of the struct
with the modified data.
来自
您看到此错误是因为闭包捕获了不可变的 self
。
就像原始类型(例如Int
)一样,struct
是值类型,Swift是根据值类型的不变性概念构建的。
换句话说,如果您有 let questionManager = QuestionManager()
,您会期望 questionManager
不会更改。即使它是 var
,它也只能通过调用者的直接操作进行变异,例如questionManager.doMutatingFunc()
.
但是,如果允许闭包捕获 self,它可能会在稍后修改自己。这是不允许的。
解决这个问题的最简单(唯一?)方法是将 QuestionManager
变成 class
:
class QuestionManager {
// ...
}
我正在从网站检索数据。
网络运行良好。从 JSON.
中正确解析数据几个参考 - 在这个结构中:
- 回复是 JSON 的数据模型
- PrepareQuestions 是一个执行解析的函数(我在同一结构的扩展中有它)
我想在这个结构中有一个对象(downloadedData - 'Replies' 是带有数据模型的结构)包含所有下载的信息,但由于“self 是不可变的”而导致我出错捕获”。有什么建议么?谢谢!
struct QuestionsManager {
var downloadedData:Replies?
func useData() {
manageQuestions(url: K.urlForRetreival, numberOfQuestions: K.numberOfSquares) { [self] (replies, error) in
if let replies = replies {
DispatchQueue.main.async {
downloadedData = replies // Here I got the error
}
}
}
}
func manageQuestions(url: String, numberOfQuestions: String, myCompletion: @escaping (Replies?, Error?)->()) {
let generatedUrl = URL(string: url + numberOfQuestions)!
let urlSession = URLSession(configuration: .default)
let task = urlSession.dataTask(with: generatedUrl) { (data, response, error) in
if error == nil {
if let fetchedData = data {
let fetchedProcessedData = prepareQuestions(data: fetchedData)
myCompletion(fetchedProcessedData, nil)
return
}
} else {
myCompletion(nil, error)
return
}
}
task.resume()
}
}
struct is a value type. For value types, only methods explicitly marked as mutating can modify the properties of self, so this is not possible within a computed property.
If you change struct to be a class then your code compiles without problems.
Structs are value types which means they are copied when they are passed around.So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.If your struct is immutable then all automatic copies resulting from being passed by value will be the same.If you want to change it you have to consciously do it by creating a new instance of the struct with the modified data.
来自
您看到此错误是因为闭包捕获了不可变的 self
。
就像原始类型(例如Int
)一样,struct
是值类型,Swift是根据值类型的不变性概念构建的。
换句话说,如果您有 let questionManager = QuestionManager()
,您会期望 questionManager
不会更改。即使它是 var
,它也只能通过调用者的直接操作进行变异,例如questionManager.doMutatingFunc()
.
但是,如果允许闭包捕获 self,它可能会在稍后修改自己。这是不允许的。
解决这个问题的最简单(唯一?)方法是将 QuestionManager
变成 class
:
class QuestionManager {
// ...
}