converted chapter 18 example to swift