2016-04-27 74 views
0

我有一個類,我需要檢查一個數組的json數據編譯的URL,看看最新的內容是否是文章/項目/調查。我的代碼編譯,但它比較字符串,看看它的文章/項目/調查。不知道我做錯了什麼?如何將變量String()與swift中的字符串進行比較?

我的代碼是

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

    } 
} 

注:IM在swift2

+0

content.append(id)中的內容是什麼? –

回答

1

問題的工作是這一行:

let articleSurveyOrProject = content[0] 

刪除let

+0

我在操場上運行他的代碼,這是其中一個問題。但它仍然沒有解決它。 –

+0

@matt工作。非常感謝! – kevinabraham

相關問題