【问题标题】:Print repetition in Swift while statement [closed]在 Swift while 语句中打印重复 [关闭]
【发布时间】:2023-07-21 23:30:01
【问题描述】:

在我的 while 语句中,我无法理解为什么我的输出被打印了两次? 我只想打印一次 i,我的错误在哪里?

func fetch2(){
    var i: Int = 0
    while i <= (self.returned-1) {
        let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
        print(i)            
        i = i+1 
    }
}

这是我得到的输出:

0
1
2
3
0
1
2
3

提前谢谢你。

【问题讨论】:

  • fetch2 被调用了两次吗?
  • fetch2() 被调用一次

标签: swift api while-loop alamofire swift5


【解决方案1】:

看起来fetch2() 被调用了两次。

var i 之前添加print(#function) 并检查fetch2() 是否没有被多次调用。

【讨论】:

  • 是的,这就是问题所在,我在某处忘记了 fetch2() 调用,谢谢!
  • 太好了,不要犹豫接受答案!