Ios 如何检查核心数据是否为空?
我正在使用核心数据填充集合视图。现在我想检查它是否为空。如果它是空的,我想做额外的事情。我怎样才能做到这一点Ios 如何检查核心数据是否为空?,ios,swift,core-data,Ios,Swift,Core Data,我正在使用核心数据填充集合视图。现在我想检查它是否为空。如果它是空的,我想做额外的事情。我怎样才能做到这一点 if myJokes.isEmpty == true { noFavorites.isHidden = false } 我尝试过这样检查,但不起作用: class ViewController2: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollect
if myJokes.isEmpty == true {
noFavorites.isHidden = false
}
我尝试过这样检查,但不起作用:
class ViewController2: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
var myJokes : [MyJokes] = []
override func viewDidLoad() {
super.viewDidLoad()
getData()
myCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myJokes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell2
if myJokes.isEmpty == true {
noFavorites.isHidden = false
}
cell.backView.layer.cornerRadius = 10
let task = myJokes[indexPath.row]
cell.textLabel.text = task.favoriteJokes!
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: myCollectionView.frame.width, height: 58)
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
myJokes = try context.fetch(MyJokes.fetchRequest())
} catch {}
}
@IBAction func menuBtnPressed(_ sender: Any) {
if menuShowing {
menuConstraint.constant = -171
UIView.animate(withDuration: 0.4, animations: {
self.view.layoutIfNeeded()
})
}
在我的笑话里,你得到了什么样的数据?像字典一样,数组etcAn字符串数组ID您可以尝试
mycrooks.count
?是的,不起作用either@Volkan然后使用like if myJokes.count>0{//here data}或者{//no data}