我正在研究我的Udacity Nanodegree iOS项目,它要求我们使用AppDelegate的共享模型。我一直在我的代码中遇到这个错误一段时间我已经在互联网上寻求解决方案。我尝试使用get only声明,我仍然会抛出这个错误......有什么建议吗? 这是我的CollectionViewController.swift的一部分:
import Foundation
import UIKit
class SentMemesCollectionVC:UICollectionViewController{
@IBOutlet weak var imageView: UIImageView!
let object = UIApplication.sharedApplication().delegate
var appDelegate = AppDelegate() {
return object as! AppDelegate //Error get only property //error instance member 'object' cannot be used on type ___
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return appDelegate.memes.count
}
let memes = appDelegate.memes
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CustomMemeCell", forIndexPath: indexPath) as! CustomMemeCell
let meme = memes[indexPath.item]
cell.setText(meme.top, bottomString: meme.bottom)
let imageView = UIImageView(image: meme.image)
cell.backgroundView = imageView
return cell
}
答案 0 :(得分:0)
您是否尝试创建/使用计算属性!?这应该是这样的:
var appDelegate : AppDelegate {
return object as! AppDelegate
}