字符串比较不适用于Swift

时间:2023-01-25 22:59:40

I can't seem to figure out what's going on here. I'm trying to perform an action based on if two strings are a match. Sometimes it seems to work and sometimes it doesn't. My app (written in Swift) allows a user to search through a list of golf courses and select one. If they select one then it is added to Realm as a previous course. If they have already selected this golf course in the past then I'm displaying a message to say they have already selected that course in the past so it isn't added to the list twice. I've tried to remove the white spaces to help but it still doesn't seem to work. Some of the golf courses have characters such as "??, -, $" because of the data base I downloaded it from. Not sure if this matters or not. Again, sometimes it works and sometimes it doesn't. Just not sure why. Thanks.

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

The two strings I'm comparing in the code are:

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

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

Full method:

    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 个解决方案

#1


0  

I think you should use isEqualToString function for comparing two strings.

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

#2


0  

Alright, I figured out what was happening. I appreciate everyone's help and the suggestions lead me down a path of trying some other options. What was happening is "courseFromRealm" was only taking on the last value in my previousCoursesFromRealm when using the for-in loop. This is why sometimes it worked and sometimes it didn't because if the course I selected was the last value in the for-in loop then my code appeared to work otherwise it didn't. So I got rid of the for-in loop and used the "contains" method on previousCoursesFromRealm to simply check if this course I was selecting was already in my list of previous courses. If it is then I display the alert message and if not then I add it to the list. Again, thanks for everyone's help! still a little new to programming and working though it.

好吧,我弄清楚发生了什么。我感谢大家的帮助,这些建议让我走上了尝试其他选择的道路。发生的事情是“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()

            }
        }
    }

#1


0  

I think you should use isEqualToString function for comparing two strings.

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

#2


0  

Alright, I figured out what was happening. I appreciate everyone's help and the suggestions lead me down a path of trying some other options. What was happening is "courseFromRealm" was only taking on the last value in my previousCoursesFromRealm when using the for-in loop. This is why sometimes it worked and sometimes it didn't because if the course I selected was the last value in the for-in loop then my code appeared to work otherwise it didn't. So I got rid of the for-in loop and used the "contains" method on previousCoursesFromRealm to simply check if this course I was selecting was already in my list of previous courses. If it is then I display the alert message and if not then I add it to the list. Again, thanks for everyone's help! still a little new to programming and working though it.

好吧,我弄清楚发生了什么。我感谢大家的帮助,这些建议让我走上了尝试其他选择的道路。发生的事情是“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()

            }
        }
    }