Fetch CKAsset List Swift

215 views Asked by At

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

1

There are 1 answers

1
Oleg Gordiichuk On

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] {

}