Swift不好理解的地方

1.String(类名) 。即 "类名",例如

private struct Storyboard {

    static let CellIdentifier = String(AnimationCollectionViewCell)

    static let NibName =    
    String(AnimationCollectionViewCell)
    static let a = String(ViewController)

}

2.images.count?? 不存在为0

return imageCollection?.images.count ?? 0

3.闭包的妙用 在 使用UICollectionView的VC里实现
backButtonTapped

class AnimationCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var animationImageView: UIImageView!
    @IBOutlet weak var animationTextView: UITextView!

    var backButtonTapped: (() -> Void)?//闭包

    func prepareCell(viewModel: AnimationCellModel) {

animationImageView.image = UIImage(named: viewModel.imagePath)
animationTextView.scrollEnabled = false
backButton.hidden = true
addTapEventHandler()

    }

    func handleCellSelected() {

    animationTextView.scrollEnabled = false
    backButton.hidden = false
    self.superview?.bringSubviewToFront(self)
    }

    private func addTapEventHandler() {

    backButton.addTarget(self, action: #selector(AnimationCollectionViewCell.backButtonDidTouch(_:)), forControlEvents: .TouchUpInside)

    }

    func backButtonDidTouch(sender: UIGestureRecognizer) {

        backButtonTapped?()

    }

}

3.map和闭包的连用

struct AnimationImageCollection {

    private let imagePaths = ["1", "2", "3", "4", "5"]

    var images: [AnimationCellModel]

    init() {
        //$0值imagePaths的元素
        images = imagePaths.map { AnimationCellModel(imagePath: $0) }
    }
}

struct AnimationCellModel {

    let imagePath: String

    init(imagePath: String?) {

    self.imagePath = imagePath ?? ""

    }

}

你可能感兴趣的:(Swift不好理解的地方)