2017-02-19 46 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