Learning Swift

Now that Swift 3 is out and the language is stable, I thought I’d write my next app using it. I watched the first few videos of Paul Hegarty’s CS193P class and then started to read The Swift Programming Language (Swift 3). My first inclination, since this is the replacement for Objective C, was to relate the language features of Swift to those of Objective C. Both languages will have to handle strings and arrays so I started there. It didn’t go as well as I thought it would. An immutable string in Swift is initialized as


let mySwiftString:String = "This is a string."

whereas an immutable string in Objective C is initialized as

NSString *myObjCString = @[This is a string.];

So far so good, but the similarity breaks down quickly.

mySwiftString = "The string now has new content." //  Cannot assign to value: 'myString' is a let constant
myObjCString = "The string now has new content." //  No problem

Change the ‘let’ to ‘var’ for the Swift string and it works fine but then you no longer have an immutable string.

In Objective C, ‘myObjCString’ is an object created by the String Class. Swift strings are represented by the String type. In Swift, strings, arrays, dictionaries, sets, and numbers are value types.
When they are passed around, their value is passed around, not a reference to an object. Not so with Objective C where some of these are passed by value and some by reference. And numbers can be either ‘primitives’ or NSNumbers.

Another big difference is type safety. Swift is a type-safe language so you must explicitly define the type of everything or else the complier will object. Objective C puts the onus on you to make sure that the type of your object is what the code expects.

I quickly decided that trying to fit Swift into my mental model of Objective C wasn’t going to work. Sure they both handle operations, collection types, control flow, and classes, but their basic approach is so different that trying to draw analogies between the two is counter-productive.

LIFE Magazine Aug 27, 1956

Lots of people on-line claim that global warming is a recent invention of scientists who just want to keep getting government grants. I stumbled across this article The Weather IS Changing in Life Magazine. Lots of anecdotes about warmer weather. Several hypotheses were explored, including carbon dioxide buildup, so clearly it isn’t a recent invention.

Life Magazine Excerpt 1956-08-27

Here’s some more anecdata. Usually these don’t bloom until February but they’ve been blooming for the last couple of weeks.

Cymbidium
Apple Tree

Xcode Error: Finder information, or similar detritus not allowed

I haven’t updated my apps lately and I thought I’d see how much work it would be to update to Xcode 8. Well, there is a lot of deprecated code in my apps, since I am still compatible with iOS 5.1.1. I fixed the easy ones but there are still 48 issues left. However, the apps won’t compile now because of this error:
Finder information, or similar detritus not allowed

The error message indicated that the file in question is:
/Users/username/Library/Developer/Xcode/DerivedData/Words-boikejhzvtrlltffksnstzlbjcpf/Build/Products/Debug-iphonesimulator/AppName.app

But that is a package, so the problem must be in a file somewhere in there.

Fortunately, this is an error that others have encountered and there is a solution.

First cd to the above named directory and run the following code to find all of the files in the director that have extended attributes.
ls -al@ *

When I ran it I had a bunch of files that have an @ at the end file permissions section. In my case they are all images—in fact all of them are app icon or launch images. My other images are fine.


-rw-r--r--@ 1 username  staff   153369 Jan 12  2013 AppName LaunchImage-Landscape~ipad.png
  com.flyingmeat.Acorn.lastScale        8
        com.flyingmeat.Acorn.selectedLayerId       36
-rw-r--r--@ 1 username  staff    11122 Mar  9  2013 AppName AppIcon72x72@2x~ipad.png
  com.apple.FinderInfo       32
  com.apple.quarantine       60
-rw-r--r--@ 1 username  staff     3958 Sep  4  2013 ShowMe AppIcon40x40@2x.png
  com.apple.metadata:kMDItemWhereFroms      169

The files for this project are all in the Words directory so I recursively removed the attributes from all of the files. Thanks to cwd at StackOverflow for the info on how to do it. I have tens of thousands of files, so it took a while.

xattr -rc /Words

The Sense of Style

I somehow made it through four years of High School English, Freshman Composition, Shakespeare, and Comparative Literature without being exposed to traditional grammatical terms. I can usually identify nouns, verbs, and adjectives but would have trouble identifying adverbial phrases, case markers, and the various kinds of verb tenses. I suspect, as Steven Pinker says, that I “pick up the nuances of words by focusing on their makeup and their contexts over the course of tens of thousands of hours of reading.” Over the years I have read many style guides, particularly those written by actual writers—like Stephen King and Lawrence Block. I also kept the Chicago Manual of Style handy on my bookshelf, although lately I tend to use on-line resources like the Purdue OWL. So when The Sense of Style came out, I immediately put it on my to-read list.

I love the Oxford comma, singular they, and have no problem starting a sentence with a conjunction. Likewise, ending a sentence with a preposition and splitting infinitives seems perfectly natural to me. It turns out that Pinker agrees with me that these elements of my style are perfectly fine. But he goes further than just validating my stylistic choices and explains why they are perfectly reasonable. He also provides a framework for understanding why some sentences work and some do not. Most of the book is easy to follow if you have an interest in writing well, but he does use grammatical terms—generative, nominative, supplement, adjunct—that I was not familiar with and needed to look up. He explains many of the terms in the text and provides a glossary of terms for others, but he does presuppose a level of grammatical knowledge that the casual reader does not necessarily possess. I still have trouble remembering some of the terms and need to refresh my memory when encountering them later in the book. That’s why, now that I’ve read the book, I am revisiting and carefully studying parts of it. I’m sure that the effort will improve my writing, but require a bit of work on my part to fully incorporate.

The grammatical concept that seems most useful for writing understandable prose is the tree metaphor. The tree metaphor is useful for understanding complicated sentence structures and is especially useful for matching verb tenses with the subject. Since most of my writing is expository, it was interesting to learn that I write in the classic style. And for the most part I follow left-to-right ordering which means that readers can process the sentence without having to jump back and forth in the sentence to decipher the point.

A large part of the book deals with linguistic theory and about a third of the book, Chapter 6: Telling Right From Wrong, deals with specific rules of grammar, word choice, and punctuation. So even if you are not interested in learning why good writing is good, you can use the book as a reference.

As I said earlier, I just started studying parts of the book. I’ll update this post with my thoughts on specific ideas in the book as I run across them. But for now I will comment on one thing that was a bit off-putting on the first read. I am a long-time user of singular they. In my experience, readers don’t even notice that I am doing it. Even the authors of my manuals, who object strenuously to it, don’t notice that I have changed their pronouns to they. And that’s the biggest reason that I use it. Alternatives like he/she, he or she, and ze interrupt the flow of the sentence and distract from the concepts that you are trying to get across. Which is why it is puzzling to me that Pinker, who supports the use of singular they, switches back and forth between he and she. In each chapter the generic writer is of one sex and the generic reader is of the other. Then in the next chapter it is the reverse. If you are familiar with the linguistics literature it may seem natural but it was confusing to me.