Tag Archives: language

The Prodigal Tongue

Lynne Murphy writes a great blog, which this book is at least partly based on, about the differences between American and British English. Or should that be that this book is based on?

Books like this really make you question everything you write.

Some of the material I was already familiar with, having read it elsewhere, possibly even on her blog, but there were plenty of new facts to keep me entertained. Sometimes it’s difficult to remember where a word of phrase came from. Did I always say that or did I learn it more recently? How do you pronounce words? When it came up in conversation a few months ago, I genuinely couldn’t remember whether I normally said “skedule” and “shedule.” I kept second-guessing myself!

I didn’t, for example, realise that I’d learned so much American baking terminology. I guess I’ve made more cakes since I got married and picked up the lingo. Part of me did think that “batter” was what you use for pancakes and Yorkshire puddings, but, equally, I couldn’t think of a better word for cake mixture. (Turns out there’s a reason for that.)

The other thing I realised is that one consequence of such a heavy US bias on the Internet is that companies such as Grammarly are continually suggesting American English grammar and spelling corrections. (The spelling I’m normally confident enough to override, punctuation less so.) Before reading this book I always thought that the advice was suspect but I thought it was mostly a matter of taste rather than geography.

Even if you’re not into the details of how the two nationlects — a word she coins to distinguish between American and British English — differ, the last chapter still might be of interest. It’s about the growth of the language outside the UK and the US, how it’s now the most popular second language.

Native speakers in Britain and the US make the mistake of thinking that they have no language learning to do: everyone speaks English, so we’ve got it made.

This is absolutely my experience. Many people — including past me — thought that this was true. I guess it’s where the stereotype of Brits talking slower and louder to foreigners comes from. (I don’t think I ever went that far!)

I’ve learned the hard way that just because people speak English, it’s not necessarily my English. They may understand most of the words but there are idioms and pronunciations that don’t translate. Dealing with non-native speakers requires care and thought, which, frankly, is the least we can do since they made the effort to speak our language. I can’t say I always get it right, but I’m conscious of it and make an effort.

Anyway, if you like the blog, you’ll probably like the book. As you’d imagine, it’s well written. It has lots of nice, little facts you can sprinkle into conversation and it’s nicely structured and feels fairly complete.

Learning Swift

Swift is a new programming language designed by Apple for development on OS X and iOS. I thought that I should try to learn it a little so I decided to convert a non-trivial collection of classes from one of my apps (www.cut) into Swift. I always find it better to work on a real project rather than just to play around with things aimlessly. Also, by re-working an old project, I knew that all the problems I would find would be language related rather than anything to do with the architecture.

The classes are also data related rather than being UI, so it is mostly a test of the language itself rather than how it interfaces with Objective C.

First impressions are good. Swift is mostly nice and consistent, which although sounding like damning with faint praise, is actually a compliment. I read a little of the language guide and dove straight in. A lot of the attempts to get the following right were me just typing stuff, guessing the syntax rather than looking it up.

Quite by accident I think my code sample inadvertently shows an area of strength for Objective-C and weakness for Swift.

The idea of the code is that it reads a Plist, instantiates a class based on that configuration and fills in a number of properties.

The first half of the code looks like this:

        NSError* error = nil;
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"XXX" ofType:@"plist"];
        NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
        NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:plistXML
                                                                                       options:NSPropertyListMutableContainersAndLeaves
                                                                                        format:NULL
                                                                                         error:&error];
        NSArray* values = [temp objectForKey:@"Entries"];

This was pretty straight forward to convert into Swift, though the type system gave me issues:

    let plistPath =  NSBundle.mainBundle().pathForResource("XXX", ofType: "plist")
    let plistXML = NSFileManager.defaultManager().contentsAtPath(plistPath)

    var error:NSError? = nil
    var format:CMutablePointer<NSPropertyListFormat>? = nil
    let immutable:NSPropertyListReadOptions = 0
    var pList = NSPropertyListSerialization.propertyListWithData(plistXML,
        options:NSPropertyListReadOptions(NSPropertyListMutabilityOptions.Immutable.toRaw()),
        format:format!,
        error: &error) as NSDictionary

Getting the options property seems very clumsy; I’m sure that there must be a better way of doing it.

I had a real problem with the in/out parameters format and error. Not only was the documentation confusing but the Swift Playground kept crashing making it difficult to distinguish between what I was doing wrong and where the compiler itself was messing up. It’s also a bit odd that, though both are in/out parameters, that they both need different methods to extract the values.

(To be fair, this is a beta and it is the first version of a whole language and compiler. I mention the crashes not because they’re unexpected or even especially bad, just as an honest description of the difficulty I had.)

The next section, using the data in the plist, was much more problematic. The code looks like this, but I’ve trimmed a lot so what we have here isn’t terribly useful now!

        proxy = nil;
        for (NSDictionary* i in values) {
            if ([thing isEqualToString:[i objectForKey:@"Class"]]) {
                dynamicClass = NSClassFromString([i objectForKey:@"BaseClass"]);
                proxy = [[dynamicClass alloc] init];
            }
        }

I didn’t do the straight conversion. A few more years of Cocoa programming allowed me to notice an optimisation:

    let valueList = values.filteredArrayUsingPredicate(NSPredicate(format: "Class = %@", value))
    let valueData = valueList[0] as NSDictionary

This same approach would work in Objective-C.

Next I tried:

  let dynamicClass = NSClassFromString(valueData["BaseClass"])
  let proxy = dynamicClass()

The first line works as expected. The second line doesn’t compile.

Is there anything else that we can do with dynamicClass? Let’s see. It’s an AnyClass which is a type alias for AnyObject.Type. Which doesn’t really help.

I tried casting it to a base class but no matter what I tried I couldn’t alloc/init it (in Objective C terms).

Josh Smith figured out how to do it by creating a factory class in Objective-C.

I tried (and failed) to get it to work by calling some of the Objective-C runtime directly:

var bytes:Byte[] = [0,0,0,0]
let b = objc_constructInstance(a, &bytes)

But the second line doesn’t work when using ARC. (To be fair, Xcode struck through the definition so I didn’t have much confidence that it would work!)

So that leaves Josh’s call out to Objective-C to be the best method that I’m aware of.

In the end I just used a switch statement to select between the relatively limited number of options that I had to choose between. Not as clever, but maybe that’s a good thing?

My delicious.com bookmarks for April 17th through April 18th

Translations

www.cut Icon
One of the nice things about Apple’s App Store for iOS devices is that it’s available in every country that has the iPhone. One of the unfortunate things is that the people in most of those countries don’t speak English as a first language and I, as a typical Englishman, know no other languages.

Then I heard about GetLocalization (whose name needs localising!), a site which allows you to “crowd-source” translations. I honestly don’t know if it will work but I thought it was a good idea and something worth trying and supporting.

To that end I have uploaded the text used in www.cut and added the languages that I think there would be the most benefit in having. I would be happy if people added others, especially if the suggestion came with some translations!

My guess is that I will need to change some of the text in the application itself. For example, one block looks like this: “%1$i character%2$@ remaining.” When viewed on the iPhone looks like either “100 characters remaining” or “1 character remaining” but I’m sure that form of pluralisation doesn’t work in most languages. Changing it is not a problem but I will need suggestions as to what the “generic” version should be. Or do I need two versions, one for singular and one for plural?

So far I have only added www.cut and I’m classifying it as an experiment, though if it works well I will likely use the same method for Yummy.

Thanks for your help.

My delicious.com bookmarks for December 4th through December 9th

My delicious.com bookmarks for May 8th through May 18th