0

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

1
  • What is content in content.append(id)? Commented Apr 27, 2016 at 2:58

1 Answer 1

1

The problem is this line:

    let articleSurveyOrProject = content[0]

Delete let.

Sign up to request clarification or add additional context in comments.

1 Comment

I ran his code in a playground, and this was one of the issues. However it still didin't fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.