import Photos import PhotosUI import UIKit class MyCell: UICollectionViewCell { let imageView = UIImageView() override init(frame:CGRect) { super.init(frame: frame) addSubview(imageView) } required init?(coder: NSCoder) { fatalError() } override func layoutSubviews() { super.layoutSubviews() imageView.frame = bounds } } class ViewController: UIViewController, PHPickerViewControllerDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { <#code#> } func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { <#code#> } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { <#code#> } private let collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let c = UICollectionView(frame: .zero, collectionViewLayout: layout) c.register(MyCell.self, forCellWithReuseIdentifier: "cell") return c }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(collectionView) collectionView.dataSource = self collectionView.frame = view.bounds title = "New Photo Picker" navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapAdd)) } @objc private func didTapAdd() { var config = PHPickerConfiguration(photoLibrary: .shared()) config.selectionLimit = 3 config.filter = .images let vc = PHPickerViewController(configuration: config) vc.delegate = self present(vc, animated: true) func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true, completion: nil) results.forEach { results in results.itemProvider.loadObject(ofClass: UIImage.self) { reading, error in guard let image = reading as? UIImage, error == nil else{ return } print(image) } } } private let images = [UIImage]() func collectionView(_ collectionView: UICollectionView, numberOfItemInSection section: Int) -> Int { return images.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? MyCell else { fatalError()} cell.imageView.image = images[indexPath.row] return cell } } }
Xcode初心者です。今写真のアプリ作ろうと思っています。
private let images = UIImageに__Attribute 'private' can only be used in a non-local scopeというエラー__が出てどうしてもこのエラーが無くなりません
解決方法を教えていただきたいです。
波括弧 { } の対応やインデントを見直してみては。(機械的に揃えるなら、Xcode で control + i です。)
https://tech.playground.style/swift/xcode-indent-tab-change/
あなたの回答
tips
プレビュー