Swift version

This sample is wrote by Swift language but implement the same function.