-2

enter image description here> Iam trying to insert values into dictionary . I tried 2 ways one in which the 2 variables are used and other in which i directly pass the value inside dictionary . Both are not working

for index in 0..<classes.count{
                var classification = classes[index].className
                var score = classes[index].score
                //not working
                self.classificationDictionary[classification] = score

                //not working
                self.classificationDictionary[classes[index].className] = classes[index].score
            }

After the closure this seems to work. I dont understand how to append values into dictionary .

classificationDictionary["abc"] = 131
        print("dictionary : ",classificationDictionary)
        print(classificationDictionary.count)

1 Answers1

0

Try this one:

classificationDictionary.updateValue(classes[index].score, forKey: classes[index].className)

you code will be :

for index in 0..<classes.count{
                var classification = classes[index].className
                var score = classes[index].score
                classificationDictionary.updateValue(score, forKey: classification)

            }    

For example:

class ViewController: UIViewController {

    var classes = [
        AnyClass(name: "pizza", score: 2.0),
        AnyClass(name: "sushi", score: 3.0),
        AnyClass(name: "potatoe", score: 4.0)
    ]
    var classificationDictionary: [String: Double]!


    override func viewDidLoad() {
        super.viewDidLoad()
        any()
    }

    func any() {
        classificationDictionary = [String: Double]()
        for index in 0..<classes.count {
            if let name = classes[index].name, let score = classes[index].score {
                classificationDictionary.updateValue(score, forKey: name)
            }
            print(classificationDictionary)
        }
    }

}    

code of custom class:

class AnyClass: NSObject {

    var name: String?
    var score: Double?

    init(name: String?, score: Double?) {
        if let name = name {
            self.name = name
        }
        if let score = score {
            self.score = score
        }
    }
}   

result: Screenshot

Sergey Hleb
  • 152
  • 1
  • 16
  • thanks for reply. I tried what u told. but it is still not adding the value in dictionary .Tried debugging, The classes seems to work it shows 12 found but the index is not changing. – user9473922 Mar 19 '18 at 06:33
  • @user9473922, is the classes - array of custom objects? every object in classes consist of name: String and score: Double? – Sergey Hleb Mar 19 '18 at 06:41
  • no , there might be more objects inside of classes . [VisualRecognitionV3.ClassResult(className: "pizza", score: Optional(0.68300000000000005), typeHierarchy: nil) reponse is usually like this. and iam tapping into the classname and score – user9473922 Mar 19 '18 at 06:53
  • @user9473922, I edited my answer, this code worked. check it. I implement array of custom objects, and insert objects into dictionary. on screenshot u will see how dictionary edited – Sergey Hleb Mar 19 '18 at 07:10