I am trying to convert this code in swift.

```
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    CGSize collectionViewSize = self.collectionView.bounds.size;
    CGFloat proposedContentOffsetCenterX = proposedContentOffset.x + self.collectionView.bounds.size.width * 0.5f;

    CGRect proposedRect = self.collectionView.bounds;

    // Comment out if you want the collectionview simply stop at the center of an item while scrolling freely
    // proposedRect = CGRectMake(proposedContentOffset.x, 0.0, collectionViewSize.width, collectionViewSize.height);

    UICollectionViewLayoutAttributes* candidateAttributes;
    for (UICollectionViewLayoutAttributes* attributes in [self layoutAttributesForElementsInRect:proposedRect])
    {

        // == Skip comparison with non-cell items (headers and footers) == //
        if (attributes.representedElementCategory != UICollectionElementCategoryCell)
        {
            continue;
        }

        // == First time in the loop == //
        if(!candidateAttributes)
        {
            candidateAttributes = attributes;
            continue;
        }

        if (fabsf(attributes.center.x - proposedContentOffsetCenterX) < fabsf(candidateAttributes.center.x - proposedContentOffsetCenterX))
        {
            candidateAttributes = attributes;
        }
    }

    return CGPointMake(candidateAttributes.center.x - self.collectionView.bounds.size.width * 0.5f, proposedContentOffset.y);

}

```

This is what I have done so far

```
func targetContentOffsetForProposedContentOffset(proposedContentOffset:CGPoint, velocity: CGPoint) -> CGPoint
{

   let collectionViewSize:CGSize  = self.collectionView.bounds.size
   let proposedContentOffsetCenterX:CGFloat = proposedContentOffset.x + self.collectionView.bounds.size.width * 0.5

   let proposedRect:CGRect = self.collectionView.bounds
   //let candidateAttributes:UICollectionViewLayoutAttributes

   var candidateAttributes:[UICollectionViewLayoutAttributes]

   /* get stuck here in for loop*/
   for (attributes: [UICollectionViewLayoutAttributes] in layoutAttributesForElementsInRect:proposedRect)
   {

   }

```

[![enter image description here](https://i.stack.imgur.com/xQBao.png)](https://i.stack.imgur.com/xQBao.png)