bitwix

Tangential comments about Software Development

Tuesday, January 20, 2015

Swift Sublime and Ridiculous

The sublime moment in Swift programming for the iPad came the other day. I had a custom layout which toggled between different cell sizes. I triggered it by calling invalidateLayout. That worked, but I wanted it animated. It was literally one line of code:

self.collectionView!.performBatchUpdates({ 
    self.collectionViewLayout!.invalidateLayout() 
}, completion: nil) 

I played with it for hours. If I can work out how to grab a movie from the screen, I will put it on Youtube so you can see the glory!

The ridiculous moment came about five minutes later. I wanted to split a string on a colon. Here's how you do it:

var r = str.rangeOfString( ":") 
if r != nil { 
    str2 = str.substringFromIndex( advance(r!.startIndex, 1 ) ) 
    str1 = str.substringToIndex(r!.startIndex) 


Really? OK, so I can use the split function, but there will be times when I want the substring approach, and that approach is too darn obscure.