I am trying to use Swift to create a reference to an image and storing it in documents using a dictionary and JSON. I believe that I created the correct syntax for dictionary based on another SO answer that I found. The error message below happens when I press the button with function addButtonClicked
. What am i doing wrong?
Error message:
Invalid (non-string) key in JSON dictionary
// Inside UICollectionViewCell
var representedAssetIdentifier: String? = nil
// Inside UIViewController
var count: Int = 0
var dictionary: [Int:String] = [:]
@objc func addButtonClicked() {
do {
let fileURL = try FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("example.json")
try JSONSerialization.data(withJSONObject: dictionary).write(to: fileURL)
} catch {
print("Error report: \(error.localizedDescription)")
}
let newViewController = PickerTest3()
self.navigationController?.pushViewController(newViewController, animated: true)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? ImageCell {
let newCount = count
dictionary.updateValue(cell.representedAssetIdentifier!, forKey: newCount)
count += 1
selectedCells.append(indexPath)
cell.index = selectedCells.count
}
}