Today I worked on getting TextScroll’s extensions up-to-date and bug free. Swift removed string’s droplast() function that removes the last character of a string (“hi” -> “h”), so I wrote my own droplast() to be used in printDict(), a Dictionary extension that returns a python-friendly string. While droplast() worked great on its own, it wasn’t working inside printDict(), so I asked Prof. Medero about it. It turns out Software Development team expanded TextScroll in a way where printDict() won’t be needed anymore, so I’ve left printDict() and droplast() as they currently are.
Then, I tackled last year’s unresolved bug in findWordsBeforeIndex, which returns the number of complete words before the given index. For example, if you have “Harvey Mudd College” and pass in the index for the “o” in “College”, findWordsBeforeIndex should return 2. However, previously, the code would see 3 entire words in the string “Harvey Mudd C” and return 3. A temporary fix to this was simply subtracting 1 from the initial result, so “Harvey Mudd C” would correctly give you 2. However, if you passed in index 0, findWordsBeforeIndex would give you -1, which isn’t right. Prof. Medero mentioned other languages having regular expression functions used to find all instances of a specified pattern in a string. You could use such a function to find the indices of all the spaces in a string, get the index of the last space that was found, and count the words found prior to that last space. Now, instead of looking at “Harvey Mudd C”, the function will find the space before “C” and shorten the string into “Harvey Mudd”, making partial words a nonissue. It turns out Swift has NSRegularExpression, which could do exactly what Prof. Medero described. I customized a code sample I found on Stack Overflow, and now findWordsBeforeIndex works in all cases.
Currently, I’m trying to figure out how ScrollingLabel works and am working through Hacking With Swift’s tilt tutorial. I ran the tutorial’s end result on the iPad, and the acceleration/deceleration of the ball works very smoothly, while TextScroll’s word scrolling is rather jerky. Hopefully by the end of this tutorial, I’ll know the technique for smooth tilting and implement a much more pleasant reading experience for TextScroll.