简体   繁体   中英

How to compare a variable String() to a string in swift?

I have a class in which I need to check a URL for json data compile that in an array and see if the latest content is an article/project/survey. My code compiles but it compare the strings to see if its an article/project/survey. Not sure what im doing wrong?

my code is

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")
        }

    }
}

Note: Im working in swift2

The problem is this line:

    let articleSurveyOrProject = content[0]

Delete let .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM