if语句正在重复,好像它是一个循环?

时间:2017-02-19 18:20:51

标签: swift loops if-statement

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语句。请帮帮我:))

1 个答案:

答案 0 :(得分:0)

虽然我不清楚这是否会导致问题,因为我认为它位于代码的其他位置,但您发布的这部分代码不正确:

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

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