Home>

I'm studying Swift as an introduction to Swift programming, but it goes away when I copy the unit that implements UICollectionView.
Even if I copied and pasted all the sample code of the textbook, it fell out. I don't know why my app doesn't work even though the distributed app works.
The part where the storyboard is set is just entered reuseIdentifier in the Identifier of Atributes inspector. I copied and pasted this part from the sample program, but it still falls off at startup without any change

2018-09-02 18: 05: 26.069069 + 0900 collectionView [45451: 2443478] *** Assertion failure in-[UICollectionView _dequeueReusableViewOfKind: withIdentifier: forIndexPath: viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.54.4/UICollectionView.m:5283
2018-09-02 18: 05: 26.074040 + 0900 collectionView [45451: 2443478] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier reuseIdentifire-must register a nib or a class for the identifier or connect a prototype cell in a storyboard '
*** First throw call stack:
(
    0 CoreFoundation 0x00000001133b61e6 __exceptionPreprocess + 294
    1 libobjc.A.dylib 0x000000010f32f031 objc_exception_throw + 48
    2 CoreFoundation 0x00000001133bb472 + [NSException raise: format: arguments:] + 98
    3 Foundation 0x000000010edd2652-[NSAssertionHandler handleFailureInMethod: object: file: lineNumber: description:] + 193
    4 UIKit 0x00000001106a4cc0-[UICollectionView _dequeueReusableViewOfKind: withIdentifier: forIndexPath: viewCategory:] + 2475
    5 UIKit 0x00000001106a4f4e-[UICollectionView dequeueReusableCellWithReuseIdentifier: forIndexPath:] + 169
    6 collectionView 0x000000010ea1e497 _T014collectionView0B10ControllerCAASo012UICollectionB4CellCSo0dB0C_10Foundation9IndexPathV13cellForItemAttF + 375
    7 collectionView 0x000000010ea1e8bc _T014collectionView0B10ControllerCAASo012UICollectionB4CellCSo0dB0C_10Foundation9IndexPathV13cellForItemAttFTo + 92
    8 UIKit 0x000000011068e756-[UICollectionView _createPreparedCellForItemAtIndexPath: withLayoutAttributes: applyAttributes: isFocused: notify:] + 295
    9 UIKit 0x000000011068e629-[UICollectionView _createPreparedCellForItemAtIndexPath: withLayoutAttributes: applyAttributes:] + 35
    10 UIKit 0x00000001106938ae-[UICollectionView _updateVisibleCellsNow:] + 4505
    11 UIKit 0x0000000110699688-[UICollectionView layoutSubviews] + 364
    12 UIKit 0x000000010fc9a7a8-[UIView (CALayerDelegate) layoutSublayersOfLayer:] + 1515
    13 QuartzCore 0x0000000116749456-[CALayer layoutSublayers] + 177
    14 QuartzCore 0x000000011674d667 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
    15 QuartzCore 0x00000001166d40fb _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343
    16 QuartzCore 0x000000011670179c _ZN2CA11Transaction6commitEv + 568
    17 UIKit 0x000000010fbe5269 __34- [UIApplication _firstCommitBlock] _block_invoke_2 + 141
    18 CoreFoundation 0x0000000113358b0c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    19 CoreFoundation 0x000000011333d2db __CFRunLoopDoBlocks + 331
    20 CoreFoundation 0x000000011333ca84 __CFRunLoopRun + 1284
    21 CoreFoundation 0x000000011333c30b CFRunLoopRunSpecific + 635
    22 GraphicsServices 0x00000001158d2a73 GSEventRunModal + 62
    23 UIKit 0x000000010fbcb057 UIApplicationMain + 159
    24 collectionView 0x000000010ea20697 main + 55
    25 libdyld.dylib 0x00000001141c9955 start + 1
    26 ??? 0x0000000000000001 0x0 + 1
)
libc ++ abi.dylib: terminating with uncaught exception of type NSException
(lldb)

        

import UIKit
class ViewController: UIViewController {
    let items: [CGFloat] = [
        1.0,
        2.0,
        3.0,
        4.0,
        5.0,6.0,
        7.0,
        8.0,
        9.0
    ]
    let reuseIdentifier = "reuseIdentifire"
    
    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad () {
        
        
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.allowsSelection = true
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that ca
        collectionView.dataSource = self
        collectionView.delegate = self
    }
    
}
extension ViewController: UICollectionViewDataSource {
        
        func collectionView (_ collectionView: UICollectionView, numberOfItemsInSection section: Int)->Int {
            return items.count
        }
        func collectionView (_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)->UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell (withReuseIdentifier: reuseIdentifier, for: indexPath)
            cell.contentView.layer.borderColor = UIColor.black.cgColor
            cell.contentView.layer.borderWidth = items [indexPath.row]
            
            let bgView = UIView ()
            bgView.backgroundColor = .lightGray
            cell.selectedBackgroundView = bgView
        
         return cell
        }
}
extension ViewController: UICollectionViewDelegate {
    func collectionView (_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let alertController = UIAlertController (title: nil, message: "The thickness of the border is \ (items [indexPath.row])", preferredStyle: .alert)
        alertController.addAction (UIAlertAction (title: "ok", style: .default, handler: nil))
        present (alertController, animated: true, completion: nil)
    }
}