字符串比较不适用于Swift

时间:2016-01-01 23:54:27

标签: ios swift realm string-comparison didselectrowatindexpath

我似乎无法弄清楚这里发生了什么。我正在尝试根据两个字符串是否匹配来执行操作。有时它似乎有效,有时却没有。我的应用程序(用Swift编写)允许用户搜索高尔夫球场列表并选择一个。如果他们选择了一个,那么它将作为前一个课程添加到Realm。如果他们过去已经选择了这个高尔夫球场,那么我正在显示一条消息,说他们已经选择过去的那个球场,所以它没有被添加到列表中两次。我试图删除白色空间以帮助,但它似乎仍然无法正常工作。一些高尔夫球场有“??, - ,$”等字符,因为我从中下载了数据库。不确定这是否重要。同样,有时它有效,有时则不然。只是不确定为什么。感谢。

我在代码中比较的两个字符串是:

 if previousCourse.name.stringByReplacingOccurrencesOfString(" ", withString: "") == courseFromRealm.name.stringByReplacingOccurrencesOfString(" ", withString: "")

完整方法:

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if searchController.active {

    let realm = try! Realm()
        try! realm.write {
        let addPreviousCourse = PreviousCourse()

        addPreviousCourse.name = searchResults![indexPath.row].name
        addPreviousCourse.city = searchResults![indexPath.row].city
        addPreviousCourse.state = searchResults![indexPath.row].state

        self.previousCourse = addPreviousCourse

            for course in previousCoursesFromRealm {

                courseFromRealm = course

            }

        if previousCourse.name.stringByReplacingOccurrencesOfString(" ", withString: "") == courseFromRealm.name.stringByReplacingOccurrencesOfString(" ", withString: "") {

        displayAlert("Whoops!", message: "Check your list of previously selected courses or choose a different course.", actionTitle: "OK")

        } else {

            realm.add(previousCourse)
            print(previousCourse.name)
            searchController.active = false

        }

            if searchController.active == false {

            coursesTableView.reloadData()

           }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为你应该使用isEqualToString函数来比较两个字符串。

答案 1 :(得分:0)

好吧,我弄清楚发生了什么。我感谢大家的帮助,这些建议让我走上了尝试其他选择的道路。发生的事情是“courseFromRealm”在使用for-in循环时只占用了previousCoursesFromRealm中的最后一个值。这就是为什么有时候它有效,有时它没有,因为如果我选择的课程是for-in循环中的最后一个值,那么我的代码似乎工作,否则它没有。所以我摆脱了for-in循环并使用previousCoursesFromRealm上的“contains”方法来简单地检查我选择的这个课程是否已经在我以前的课程列表中。如果是,那么我显示警告消息,如果没有,那么我将它添加到列表中。再次感谢大家的帮助!编程和工作仍然有点新鲜。

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if searchController.active {

        let realm = try! Realm()
        try! realm.write {
        let addPreviousCourse = PreviousCourse()

         addPreviousCourse.name = searchResults![indexPath.row].name
         addPreviousCourse.city = searchResults![indexPath.row].city
         addPreviousCourse.state = searchResults![indexPath.row].state

         self.previousCourse = addPreviousCourse

            if previousCoursesFromRealm.contains( { $0.name == previousCourse.name }) {

            displayAlert("Whoops!", message: "This course already exists inside of your previous course list. Please choose a different course.", actionTitle: "OK")

            } else {

              realm.add(previousCourse)
              searchController.active = false

                }
            }

            if searchController.active == false {

                coursesTableView.reloadData()

            }
        }
    }
相关问题