Archive for January, 2015

Swift: Fun with Ranges

Screen Shot 2015-01-22 at 8.47.26 AM

Update: Easy fix from Joachim and Jacob, proving that my brain is fried. Joachim notes: ” Range and Interval have been separated into separate classes during the beta. … Creates either depending on context.”

I’ve been playing recently with custom operators. The operator grammar in the Swift Programming Language is a bit obtuse as it lists ranges of unicode characters instead of just showing you a table of what the legal ones are. (There are a lot.) So I hopped into a playground hoping to iterate through them to see if any nice/intriguing/helpful characters popped up. What followed was an interesting exploration of arrays of ranges.

I started by creating a basic list of items I wanted to look at, using the list of “operator-head”-compatible hex values.

var ranges : [Range<Int>] = [
    0xA1...0xA7,
    0xA9...0xA9,
    0xAB...0xAB,
    0xAC...0xAC,
    0xB0...0xB1,
    0xB6...0xB6,
    0xBB...0xBB,
    0xBF...0xBF,
    0xD7...0xD7,
    0xF7...0xF7,
    0x2016...0x2017,
    0x2020...0x2027,
    0x2030...0x203E,
    0x2041...0x2053,
    0x2055...0x205E,
    0x2190...0x23FF,
    0x2500...0x2775,
    0x2794...0x2BFF,
    0x2E00...0x2E7F,
    0x3001...0x3003,
    0x3008...0x3030,
]

But when I attempted to iterate, I ran into errors that ClosedInterval<Int> items do not conform to SequenceType protocols:

for range in ranges {
    for item in range {
        var unicode = String(UnicodeScalar(item))
        println(String(format:"0x%X: \(unicode)", item))
    }
}

Instead, I had to create a new range out of the interval as follows:

for range : ClosedInterval<Int> in ranges {
    for item in range.start...range.end range {
        var unicode = String(UnicodeScalar(item))
        println(String(format:"0x%X: \(unicode)", item))
    }
}

Thanks Joachim and Jacob for fixes.

I think my brain is too dead this morning to really think about this in depth but it seems to me a bit odd that I had to work around this in this way, since a closed interval is intrinsically range-y. (Update: Fixed.)

Most of the operator-ready characters by the way are pretty meh but that’s an entirely different post.

 

Converting the Swift Programming Language to PDF

Screen Shot 2015-01-08 at 11.52.19 AM

I hate that Apple has more or less stopped its practice of including PDF links in its reference library online docs. I hate even more that I don’t have a PDF version of the Swift Programming Language to search through and mark up using Preview. (I don’t really like the iBooks interface.)

Today, I spent a while googling and figuring this out so you don’t have to.

  1. Find the epub. Easiest way to do this is to delete your copy from iBooks and re-download. Then hop over to ~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books and sort by date modified.
  2. Adjust the epub to something Calibre can read. Let me save you a lot of time. The .epub in the Books folder is actually a folder itself, and not a renamed zip file per standards. So create a zip file (zip out.zip -r 881256329.epub), rename it from zip to epub, and throw it into Calibre.
  3. Convert with Calibre. Start here. Add the new epub and convert it to a PDF destination. I used the default settings. When the conversion job finishes, open in Preview.

As you see, there’s really not that much involved. It was the time figuring out why the epub wouldn’t open that really drained my morning.