2

I am using the code below to fetch a CKAsset List but it keeps crashing and telling me: "fatal error: unexpectedly found nil while unwrapping an Optional value" However the asset list is not empty in the CloudKit dashboard.

Do you know where I am going wrong?

var imageAssets = record.value(forKey: "membersPhotos") as! [CKAsset]

Thanks

Tom Coomer
  • 6,227
  • 12
  • 45
  • 82

1 Answers1

0

You are forced unwrapping of the record.value(forKey: "membersPhotos") it is bad practice.

To avoid this use if let or guard conditions to always know with witch type you are working on.

Example :

if let imageAssets = record.value(forKey: "membersPhotos") as? [CKAsset] {

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100