Reputation: 2568
I try to use dispatch_once, but I got this kind of error
var onceToken : dispatch_once_t = 0
dispatch_once(&onceToken, { () -> Void in
self.myCollectionView.scrollToItemAtIndexPath(NSIndexPath.indexAtPosition(1), atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
})
Upvotes: 0
Views: 261
Reputation: 21229
First of all, you can't use onceToken
in this way. As I wrote in my comment, read this.
Swift compiler errors/warnings are misleading sometimes. They're improving them, but ... When this kind of error happens and I don't see a problem in my code, I'm going to add simple return
at the end of my closure (to match closure type signature). Like this ...
dispatch_once(&onceToken, { () -> Void in
self.myCollectionView.scrollToItemAtIndexPath(NSIndexPath.indexAtPosition(1),
atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
return
})
... this makes compiler happier and now you see your real problem ...
Cannot invoke 'indexAtPosition' with an argument list of type '(Int)'
... and that's because you're calling method indexAtPosition
, which is not class method, on NSIndexPath
class. And you have to pass NSIndexPath
object there.
If you would like to scroll to the first item, you have to call it in this way:
dispatch_once(&onceToken) {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.myCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
}
Upvotes: 3