如何将变量String()与swift中的字符串进行比较?

时间:2016-04-27 02:32:20

标签: arrays string swift swift2 string-comparison

我有一个类,我需要在数组中检查json数据的URL,然后查看最新的内容是文章/项目/调查。我的代码编译但它比较字符串,看它是否是一篇文章/项目/调查。不确定我做错了什么?

我的代码是

class LocalNotificationsManager {
    var articleSurveyOrProject = String()

    func checkForNewContent() {
        let url = "https://cdn.contentful.com/spaces/maz0qqmvcx21/entries?access_token=ae8163cb8390af28cd3d7e28aba405bac8284f9fe4375a605782170aef2b0b48";
        var jsonData:NSData?

        let url = "https://cdn.contentful.com/spaces/maz0qqmvcx21/entries?access_token=ae8163cb8390af28cd3d7e28aba405bac8284f9fe4375a605782170aef2b0b48";
        var jsonData:NSData?
        var latestContentDates = [String]()


        do{
            jsonData = try NSData(contentsOfURL: NSURL(string: url)!, options: NSDataReadingOptions.DataReadingUncached)
            let jsonObject:AnyObject? = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.AllowFragments)
            if let itemArray = jsonObject?.objectForKey("items") as? NSArray{
                for item in itemArray{
                    if let sysItem = item.objectForKey("sys"){
                        //this is createdAt
                        if let createdAt = sysItem.objectForKey("createdAt") as? String{
                            print("createdAt:\(createdAt)")
                             latestContentDates.append(createdAt)
                        }

                        if let contentTypeItem = sysItem.objectForKey("contentType")!.objectForKey("sys"){
                            //this is id
                            if let id = contentTypeItem.objectForKey("id") as? String{
                                content.append(id)
                            }
                        }
                    }
                }
            }
        }catch let err as NSError{
            print("err:\(err)")
        }

        let articleSurveyOrProject = content[0]
        print("articleSurveyOrProject:\(articleSurveyOrProject)")
        sendLocalNotification()

    }


    func sendLocalNotification() {

        if (articleSurveyOrProject == "article") {
            print(Article)
        } else if (articleSurveyOrProject == "survey") {
            print("Survey")
        } else if (articleSurveyOrProject == "project")
        {
            print("Project")
        } else {
            print("Oops! something went wrong it didnt get any values")
        }

    }
}

注意:我在swift2工作

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

$firstName = mysqli_real_escape_string($firstName);
$lastName = mysqli_real_escape_string($lastName);
$emailAddress = mysqli_real_escape_string($emailAddress);
$foodBudget = mysqli_real_escape_string($foodBudget);
$utilityBudget = mysqli_real_escape_string($utilityBudget);
$entertainmentBudget = mysqli_real_escape_string($entertainmentBudget);

$addUser = "INSERT INTO CUSTOMER(CustomerID, CustomerFirstName, CustomerLastName, CustomerEmail, FoodBudget, UtilityBudget, EntertainmentBudget) VALUES (001, '" . $firstName . "', '" . $lastName . "', '" . $emailAddress . "', '" . $foodBudget . "', '" . $utilityBudget . "', '" . $entertainmentBudget . "')";

删除 let articleSurveyOrProject = content[0]