0

I am trying to learn self-sizing UICollectionView Cell techniques using a certain tutorial. I can't figure out how to write this following swift code in objective-c:

if let cvl = collectionViewLayout as? UICollectionViewFlowLayout {
    cvl.estimatedItemSize = CGSize(width: 150, height: 75)
  }

Can someone please help?

2
  • Could you post some more code? How do you get collectionViewLayout? Also, just out of curiosity, why are you trying to convert from the newer language to the older language? Do you need backwards-compatibility?
    – Arc676
    Commented Nov 15, 2015 at 7:11
  • I do not know swift so while following the tutorial, I am doing it in objective-c. There is not much code actually mentioned in the tutorial that I have mentioned and linked in my question. Commented Nov 15, 2015 at 7:14

1 Answer 1

1

In Swift, the if-let construction is equivalent to nil-checking in Obj-C. The as keyword is used for casting. The question mark makes it an optional type, which is a value that can hold nil or a value. Non-optional values cannot hold nil, and if you attempt to "unwrap" a nil optional you will get a runtime error.

In Obj-C (this should be equivalent):

UICollectionViewFlowLayout * cvl = (UICollectionViewFlowLayout)collectionViewLayout;
if (cvl){
    cvl.estimatedItemSize = CGSizeMake(150, 75);
}

CGGeometry documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.