Blog

Did my Regattakalender App explode in size – due to using Swift?

My App Regattakalender used a slim ~10MB in size on your iPhone in 2015. I wrote the app in 2013 or so and did not change much all the years. This year I wanted to change the data input format, I added new parts to the app and I decided to write the new app parts in Swift rather than Objective-C.

I did not imagine any bad consequences. So I was really shocked, when the archive that I need to upload to apple for submitting the app was suddenly 156MB in size. Uploading the app took really long. A few days later I submitted an Objective-C only app – which uploaded in just about three seconds.

That was the point that triggered my interest and I compared the sizes. I had a look inside the big archive and could see about 72MB being used for Swift Libraries. Strange.

Then I downloaded my own app from the store and checked my App size. 9MB on my iPhone. OK. That means all is OK for my customers and all I have to accept is, that Apple needs an archive with some libraries being uploaded. That’s OK for me.

So for me Swift, again, was very “exciting” to use. 😉

Crash in NSLog when used in swift – and how to avoid it

As a senior iOS programmer, I have learned to use and love a logging feature method called NSLog(). It is usually called with a format string and some arguments. If I have a function callback that comes with two parameters called response and error and I want to print them out, I can do that in Objective-C with the following line:

NSLog(@”response %@, error %@”, response, error);

In that line, the pattern %@ is replaced by the arguments added behind the format string. This also means, it is not so easy to print the pattern %@, because that has a special meaning. But back to my todays problem.

In swift, the same method is very similar:

NSLog(“response %@, error %@”, response, error)

But due to a nice feature in the string class of swift which allows it to include variables in strings, if they are surrounded by backspace and parenthesis, e.g. “\(variable)”, I was writing the above line in the following way:

NSLog(“response \(response), error \(error)”)

It always worked – until today – where it crashed. Why? Because the first string given to NSLog is the format string. This means, that if the content of \(response) creates a special meaning string, e.g. %@ then NSLog tries to interpret that according to the rules. So one safe way to print the same line is, by giving a format string:

NSLog(“%@”, “response \(response), error \(error)”)

This prevents the real string to be interpreted as a format string, because now it is an argument to the format string.

Of course, I had a distant memory, that I had made a similar error when using printf in my first year(s) as a C programmer. By changing to swift, I just had forgotten about checking for this side effect, that I am aware now for so long. A nice example of risk management and how to forget about the risks of a situation when you change the environment.

Xcode replace old style code to new style

Assume you have

[personData setValue:someProperty forKey:someKey]

and you want to change that to

personData[someKey] = someProperty

…and that for about 100 lines ?

Easy! Just type Find (cmd-f in your Xcode, change it to replace, then click on the magnifying glass and select edit find options, and then regular expression for “matching style”. Then type in

Find: \[(.*) setValue:(.*).*forKey:(.*)\];

Replace: $1[$3] = $2;

The ()-part in the find finds the first text addressed by $1, the second ()-part is addressed later by $2

You gotta love regular expressions!

China Domain name registration scam

In the past years, we received at least once a year an email where some nice registration company from china asks us if we would mind, if some client of theirs would register the domain small-apps.cn. Today was the day for 2016.

Let me tell you that registering your domain name in china is easy and cheap – about 12-20 $/€ a year I read in a price list of nicenic.net. German reseller companies offer it for 60€/year.

Swift code comment special words

Swift can help you a lot to document your code – it allows you to use markup. However, in order to use it not only as formatted text but as source code commentary, it is nice to use the special keywords known.

I created my own cheat sheet for these keywords, which create their own left column or blocks. Always use them with a preceding minus, like this

– returns: the object or nil, if not successful

  • returns:
  • throws:
  • parameter <param>:
  • parameters:
    • <param1>:
    • <Param2>:

You can also use the following keywords to have bold text formatted inside the description block:

  • Author:
  • important:
  • version:
  • Attention:
  • todo:

For more keywords, see the reference, linked above.