-4

I have NSMutableDictionary with (key,value) pairs.For JournalId the key is "journalId".I just want to check whether my dictionary contains specific "journalId", for eg, 29. If it exists, need to print the corresponding title and userId. eg.

{
    journalId = 28;
    title = Creed;
    userId = 105; } {
    journalId = 29;
    title = Fg;
    userId = 105; } {
    journalId = 30;
    title = Dhh;
    userId = 105; }

I want to check in my dictionary whether it has journalId value = 28.

if (dictValues["journalId"] == 28){
print(dictValues["title"]
}

The above method shows error. Thanks in advance.

thipoo24
  • 138
  • 15

3 Answers3

1
guard dictValues["journalId"] as! Int == 28 else {
    print("key Not found")
    return // exit the function
}
let title = dictValues["title"] as! String
print("\(title)")
itscoderslife
  • 356
  • 1
  • 9
0

Try this:

if let journalId = dictValues["journalId"] as? Int, journalId == 28 {
  print(dictValues["title"])
}
dasdom
  • 13,975
  • 2
  • 47
  • 58
0

NSDictionary subsctiption returns an object of type AnyObject?. This code should resolve your problem

if (dictValues["journalId"] as? Int == 28) {
   print(dictValues["title"])
}