gpt4 book ai didi

ios - 在 swift 中存储一个 for in 循环

转载 作者:行者123 更新时间:2023-11-29 01:45:22 25 4
gpt4 key购买 nike

我有一个 for in 循环,它扫描数组并返回三个不同的值。对于每个值,执行不同的 preformSegueWithIdentifier。在新 View 中回答问题,回答问题后, View 会将您带回原始 View 。但是,for in 循环从头开始再次运行,每次都将我重定向到相同的 View 并回答相同的问题而不是前进。

所以我想存储数组并控制我离开它的位置,以防止循环重复。

我是 Swift 的新手,所以非常感谢任何帮助。干杯!

这是循环:

   if let data = response.responseObject as? NSData {
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
println("response: \(str)") //prints the HTML of the page
let json = JSON(data: data)
let name = json["company"]["name"].stringValue
let user = json["fields"]["user"]
let extra = json["fields"]["custom"]
for (i: String,custom: JSON)in extra {
let type = custom["type"]
let Qs = custom["displayName"]

if type == "text"{
performSegueWithIdentifier(Text, sender: self)
}

if type == "multi"{
performSegueWithIdentifier(Multiple, sender: self)
}
if type == "ord"{
performSegueWithIdentifier(Ordinal, sender: self)
}




}

数组取自一个json:

 global.token = defaults.stringForKey("MyKey")!
var request = HTTPTask()
request.requestSerializer = HTTPRequestSerializer()
request.requestSerializer.headers["Authorization"] = "Bearer " + global.token
global.QRId = global.url.substringFromIndex(advance(global.url.startIndex,31)) //retrieves the ID from the URL
request.GET("url" , parameters: nil, completionHandler: {(response: HTTPResponse) -> Void in
if let err = response.error {
println("error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
if let data = response.responseObject as? NSData {
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
println("response: \(str)") //prints the HTML of the page
let json = JSON(data: data)
let name = json["company"]["name"].stringValue
let user = json["fields"]["user"]
let extra = json["fields"]["custom"]

json格式如下:

{
"id": 3,
"name": "Test Relay #1",
"anonymous": false,
"fields": {
"user": [
"name",
"email",
"dateofbirth"
],
"custom": [
{
"type": "text",
"displayName": "What did you like about our service today?"
},
{
"type": "multi",
"choices": [
"Red",
" Green",
" Blue",
" Orange"
],
"displayName": "Which of these do you like the most?"
},
{
"type": "ordinal",
"choices": {
"max": "10",
"min": "1"
},
"displayName": "How would you rate us on a scale of 1-10?"
}
]
},
"created_at": "2015-08-10 13:23:45",
"updated_at": "2015-08-10 13:23:45",
"company": {
"id": 1,
"name": "Test Company",
"address": "5 Test Place",
"postcode": "TE57 1AB",
"phonenumber": "0800001066",
"countrycode": null,
"created_at": "2015-08-10 12:17:15",
"updated_at": "2015-08-10 12:17:15"
}
}

最佳答案

这就是你如何实现它的方法,我没有在 Xcode 中检查它,所以要小心错误。

let extra = json["fields"]["custom"]
var i = 0

let defaults = NSUserDefaults.standardUserDefaults()
if defaults.objectForKey("savedPosition") != nil {
i = defaults.objectForKey("savedPosition") as! Int
}

for i; i < extra.count; i++ {
let type = extra[i]["type"]
let Qs = extra[i]["displayName"]

if type == "text"{
defaults.setObject(i++, forKey: "savedPosition")
performSegueWithIdentifier(Text, sender: self)
break
}

if type == "multi"{
defaults.setObject(i++, forKey: "savedPosition")
performSegueWithIdentifier(Multiple, sender: self)
break
}

if type == "ord"{
defaults.setObject(i++, forKey: "savedPosition")
performSegueWithIdentifier(Ordinal, sender: self)
break
}

defaults.setObject(nil, forKey: "savedPosition")
}

关于ios - 在 swift 中存储一个 for in 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988296/

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