Swift字符串比较失败

时间:2017-09-21 08:21:09

标签: ios swift sqlite

所以我刚开始为我正在做的项目“学习”swift,我遇到了一个非常有趣的场景,我无法弄清楚它为什么会发生。

所以我的项目的基本要点如下:我有一个州法律的SQLite数据库,它基本上是一个包含4列的excel表;部分,法律,注意,标记。我的所有应用程序都会将此数据库预加载到用户手机或模拟器上,并允许用户根据标签搜索法律。多个法则可以使用相同的标签,在这种情况下,我会显示带有此类标签的所有法律的预览,并让用户选择一个。

以下是处理此标记搜索的ViewController:

import UIKit
import MessageUI
import SQLite


class UISearchController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, MFMessageComposeViewControllerDelegate {

    var database2: Connection!
    var table2: Table?
    var lawTemp: String?
    let section = Expression<String>("section")
    let law = Expression<String>("law")
    let note = Expression<String>("note")
    let tag = Expression<String>("tag")

    @IBAction func queryTable(_ sender: Any) {
        do {
            print("The chosen tag was: " + String(tagChosen.text!))
            let laws = try self.database2.prepare(self.table2!)
            print("CHECKPOINT 2")
            for row in laws {
                print(String(row[self.tag]) == String(tagChosen.text!))
                print("because the current law tag is: " + String(row[self.tag]))
                    //print(row[Expression<String>("law")])
                    lawTemp = row[Expression<String>("law")]
            }
        }catch {
            print(error)
        }
        print("QUERY DONE")

    }
    @IBOutlet weak var tagChosen: UITextField!


    @IBAction func sendText(sender: AnyObject) {
        if (MFMessageComposeViewController.canSendText())
        {
            let controller = MFMessageComposeViewController()
            controller.body = String(describing: lawTemp)
            //controller.recipients = [phoneNumber.text!]
            controller.messageComposeDelegate =   self
            self.present(controller, animated: true, completion: nil)
        }
    }

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        //... handle sms screen actions
        self.dismiss(animated: true, completion: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }

    let tags = [String](arrayLiteral: "Public lands", "Definitions","Natural Resources","Powers","Risk Assesment","Land Disposition","Board Duties")

    //MARK: - Picker View Data Sources and Delegates

    func numberOfComponents(in tags: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return tags.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
             tagChosen.text = tags[row]
        return tags[row]
    }
    override func viewDidLoad()
    {
        super.viewDidLoad()

        do {
            let laws = try self.database2.prepare(self.table2!)
            for lawl in laws {
                print("SECTION: " + lawl[self.section])
                print("TAG: " + lawl[self.tag])
                print("NOTE: " + lawl[self.note] + "\n -----------------------------------------------------------------------------------------")
                print("LAW: " + lawl[self.law])
            }
        } catch {
            print(error)
        }

        let pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        tagChosen.inputView = pickerView
    }
    //override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //let dest = segue.destination
        //let tableViewController  = dest as! UILawTableViewController!
        //tableViewController.table3 = table2
        //tableViewController.database3 = database2
    //}
}

这是我选择“自然资源”作为我的tagChosen时得到的,这是控制台输出:

The chosen tag was: Natural Resources
CHECKPOINT 2
false
because the current law tag is:
Definitions

false
because the current law tag is:
Public lands

false
because the current law tag is:
Natural Resources

false
because the current law tag is:
Natural Resources

false
because the current law tag is:
Natural Resources

false
because the current law tag is:
Powers

false
because the current law tag is:
Board Duties

QUERY DONE

对于其中3张支票,它真的打印出true。理想情况下,我会将那些匹配的3行放入数组中,并使用它来向用户显示可能的规律。有人可以帮忙吗?如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

删除Martin认为不可见的字符:

String(tagChosen.text!).trimmingCharacters(in: .whitespacesAndNewlines) == String(row[self.tag])).trimmingCharacters(in: .whitespacesAndNewlines)