2017-02-19 59 views
-4
if let place = placement?[0] { 

    if place.postalCode != nil { 
     self.currentLocation.text = "Current Location: \(place.postalCode!)" 
     self.currentPostalCode = Int(place.postalCode!) 

     if (district1ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 1st" 
      representatives.insert(String("Steve Chabot(R)"), at: 0) 
     } 
     if (district2ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 2nd" 
      representatives.insert(String("Brad Wenstrup(R)"), at: 0) 
     } 
     if (district3ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 3rd" 
      representatives.insert(String("Joyce Beatty(D)"), at: 0) 
     } 
     if (district3ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 4th" 
      representatives.insert(String("Jim Jordan(R)"), at: 0) 
     } 
     if (district5ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 5th" 
      representatives.insert(String("Robert Latta(R)"), at: 0) 
     } 
     if (district14ZipCodes.contains(self.currentPostalCode) != false) { 
      self.congressionalDistrict.text = "Congressional District: 14th" 
      representatives.insert(String("David Joyce(R)"), at: 0) 
     } 
    } 
} 

所以在我的代码中,我有一个正在运行的应用程序,它是为表分配标签的代码,但由于某种原因,它一遍又一遍地重复着,好像它是一个循环,但它是一个if声明。请帮我:)if语句正在重复,就好像它是一个循环?

+0

您需要显示更多的代码。这个问题似乎是在你的代码中的其他地方。 – naglerrr

+0

忽略你的代码是一个痛苦的阅读 - 什么函数包含此代码?你继续插入第一个元素:'representatives.insert(“...”,在:0)'和最外层的if'处理第一个元素,所以这些'insert'可能再次触发了这个函数,再次 –

回答

0

虽然目前尚不清楚,如果因为我觉得在你的代码,坐在别处这有助于问题,您发布的这部分代码是不正确:

if (district3ZipCodes.contains(self.currentPostalCode) != false) { 
    self.congressionalDistrict.text = "Congressional District: 4th" 
    representatives.insert(String("Jim Jordan(R)"), at: 0) 
} 

你正在检查district3ZipCodes两次而不是district4ZipCodes