gpt4 book ai didi

swift - 在 Promisekit 完成运行并在 Swift 中返回正确的值之前循环递增并重新运行

转载 作者:行者123 更新时间:2023-11-28 12:40:52 26 4
gpt4 key购买 nike

我正在尝试使用带有 Alamofire 的 Promisekit 的 while 循环来链接四个 GET 请求,返回一个值,然后使用新参数重新运行这四个请求。这是我当前使用的代码:

        var index = 0
var count = classDictionary["class"]!.count-1

while index <= count {

firstly {
parseBooksXML(index)
}.then { abrevCode in
self.parseBooksXML2(index, key: abrevCode)
}.then { courseNumber in
self.parseBooksXML3(index, key: courseNumber)
}.then { instructorCode in
self.parseBooksXML4(instructorCode)
}
index += 1
}

前三个函数中的每一个都返回一个 promise 的字符串值,然后将其传递给下一个函数,直到第四个也是最后一个函数调用另一个函数来使用 Kanna 解析下载的 HTML。

理想情况下,我希望所有四个函数都被调用并完成,之后索引将递增并使用新索引号再次运行循环。请注意,传递给函数的索引是一种识别值应与数组中的哪个索引进行比较的方法。

为清楚起见,我在下面包含了 parseBooksXML 函数的代码:

func parseBooksXML(index: Int) -> Promise<String> {
return Promise {fulfill, reject in

let headers = [
"Referer": "URL"
]


Alamofire.request(.GET, "URL", headers: headers)
.responseData { response in

switch response.result {
case .Success:
let xml = SWXMLHash.parse(response.data!)

do {

let range = self.classDictionary["class"]![index].rangeOfString("[a-zA-Z]{2,4}", options: .RegularExpressionSearch)
let result = self.classDictionary["class"]![index].substringWithRange(range!)

try self.abrevCode = (xml["departments"]["department"].withAttr("abrev", result).element!.attribute(by: "id")!.text)

}
catch {
print("Error: \(error)")

}
fulfill(self.abrevCode)

case .Failure(let error):
print(error)
}
}
}
}

func parseBooksXML2(index: Int, key: String) -> Promise<String> {
return Promise {fulfill, reject in

let headers = [
"Referer": "URL"
]


Alamofire.request(.GET, "URL", headers: headers)
.responseData { response in

switch response.result {
case .Success:
let xml = SWXMLHash.parse(response.data!)
do {

let range = self.classDictionary["class"]![index].rangeOfString("\\d\\d\\d", options: .RegularExpressionSearch)
let result = self.classDictionary["class"]![index].substringWithRange(range!)

try self.courseNumber = (xml["courses"]["course"].withAttr("name", result).element?.attribute(by: "id")?.text)!

}
catch {
print("Error: \(error)")

}
fulfill(self.courseNumber)

case .Failure(let error):
print(error)
}

}
}
}

func parseBooksXML3(index: Int, key: String) -> Promise<String> {
return Promise {fulfill, reject in

let headers = [
"Referer": "URL"
]

Alamofire.request(.GET, "URL", headers: headers)
.responseData { response in

switch response.result {
case .Success:
let xml = SWXMLHash.parse(response.data!)
do {

let range = self.classDictionary["class"]![index].rangeOfString("[a-zA-Z]{1,3}?\\d?\\d?\\d?$", options: .RegularExpressionSearch)
let result = self.classDictionary["class"]![index].substringWithRange(range!)

try self.instructorCode = (xml["sections"]["section"].withAttr("instructor", self.classTeacher[index]).element?.attribute(by: "id")?.text)!
}
catch {
print("Error: \(error)")


}
fulfill(self.instructorCode)

case .Failure(let error):
print(error)
}

}
}
}

func parseBooksXML4(key: String) -> Void {

let headers = [
"Referer": "URL"
]

Alamofire.request(.GET, "URL", headers: headers)
.responseData { response in

switch response.result {
case .Success:
self.parseISBN(String(data: response.data!, encoding: NSUTF8StringEncoding)!)

case .Failure(let error):
print(error)
}

}
}

如有任何帮助,我们将不胜感激!

最佳答案

需要使用:

let count = classDictionary["class"]!.count-1

let promises = (0..<count).map { index -> Promise<ReplaceMe> in
return firstly {
parseBooksXML(index)
}.then { abrevCode in
self.parseBooksXML2(index, key: abrevCode)
}.then { courseNumber in
self.parseBooksXML3(index, key: courseNumber)
}.then { instructorCode in
self.parseBooksXML4(instructorCode)
}
}

when(fulfilled: promises).then {
//…
}

关于swift - 在 Promisekit 完成运行并在 Swift 中返回正确的值之前循环递增并重新运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com