1

I'm new to the swift language. I'm trying to use CollectionView but I'm running into some trouble. Here's the tutorial I am following.

but I can't get past 6:20 since this error message appears:

'Anyobject' is not convertible to 'UICollectionViewCell'

The only thing I did different from the tutorial is that I did not delete ViewerController class to change it to CollectionViewController.

Instead, I created a new class called CollectionViewController and followed step by step from then on. I did this because my CollectionViewController is to be added to an existing project and I don't want it to be the initial View Controller.

Let me know if you want to see my code. I only didn't paste it for simplicity

Thanks in advance

2 Answers 2

1

Try this code:

var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell 

difference it as or as!. It's because of swift version.

Sign up to request clarification or add additional context in comments.

2 Comments

Swift editor did suggest that the first time and I did so but the error just changed. This time it worked perfectly. OK thank you very much mr Ryosuke
Stack Overflow has a great way of letting other people know that an answer worked, if you click on the Checkmark then people who come to this question for help in the future will know that this answer worked for you.
1

If you're using Xcode 6.3 or later, you will need to use as! rather than as.

That's because Swift 1.2 change the downcast from as to as! or as?

To look detail you can see this

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.