kevinabraham kevinabraham - 1 year ago 65
Swift Question

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 = "";
var jsonData:NSData?

let url = "";
var jsonData:NSData?
var latestContentDates = [String]()

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{

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

let articleSurveyOrProject = content[0]


func sendLocalNotification() {

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


Note: Im working in swift2

Answer Source

The problem is this line:

    let articleSurveyOrProject = content[0]

Delete let.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download