Swift – StringLiteralConvertible

Today I want to have a look at StringLiteralConvertible. The concept for StringLiteralConvertible is to convert a inline string to a object type.

Alternatively, you can give string function extra functionality

To adapt class to StringLiteralConvertible, the class need to conform to the protocol StringLiteralConvertible and convertFromExtendedGraphemeClusterLiteral and have a required initalizer

Now Account class can do something like these

j j j

Autolayout contraint

This week I had an idea to revive my blog. I going to writting a series of articles on let’s write Objective C. This was inspired by Youtube series like “let’s play Simcity”. So let’s get started

So my first article is on auto layout. I played with auto layout before for my work project, but I only used it for Storyboard. So this week I started to explore how to programmably use auto layout.

There are a few issues you need to keep in mind when using auto layout:

  • You can programmably add view using frame, the system will translate it to auto layout
  • Constraints added to views need to be fully realised, UIKit need to know excatly how to render the view
  • For example: (top, left, right bottom) or (top, left, width, height)
  • You MUST turn off translatesAutoresizingMaskIntoConstraints on the view
  • View need to be added to the super view before constraints can be added
  • Positional constraints are added to the parent view

Simple right

j j j

How to restore windows instance from a snapshot in Amazon Web Service

Recently my company suffered a major crash in Amazon Web Service (AWS) after a AWS outage. We have to restore all data from backup, it was a painful experience with many glitches here and there, luckily we were able to recover.

So we re-evaluated the our backup strategy and most simple method was using AWS volume snapshot to take regular backup snapshot of the instance volume. The AWS snapshot feature is very cool. First it creates a complete backup store of your instance, then the subsequent snapshots are all created as incremental thus they are extremely fast. You can also remove any increment snapshots and AWS will manage state of the snapshot automatically in the backend.

We ran into a problem when we try to recreates an instance using the snapshot. For linux instance it’s simple, the AWS console handles everything for you, but for windows instance the process is not as trivial. So here is the steps to recreate an windows instance from AWS snapshot:

First create a volume or volumes from snapshots, make sure they are in the same region zone for the new Windows Instance. Write down the volume id.
create volume from snapshot

Then you need to launch a new windows instance. Make sure to pick the same ami as the original instance from the snapsnot. You can find the ami id in in the AWS console Instance section.
find instance ami and instance id

Once the instance has started and make sure it’s online health check passes then turn it off (do not terminate). Write down the instance id then go to volume section and detach root volume from the instance (use instance id to find the volume).

Find the root volume created from the snapshot, attach it to the instance. When ask for Device id write “/dev/sda1”. Attach all other volumes.
attach root volume

Start the instance and now you have restored your instance from a snapshot

j j j