Mar 26, 2019

Swift Race Condition: ATM Withdraw Problem

Race condition happens when 2 or more threads access the shared data at the same time and change its value at the same time resulting in unexpected state.

A simple example is the typical ATM withdraw problem.

ATM withdraw problem

Let’s say we have our own bank account system and we have ATMs depoloyed all over the town to make withdrawl easy.

Read on  

  Mar 19, 2019

Latest swift tips

It’s been a while I’ve updated my posts. Here are some new tips that makes coding with swift cool.

@autoclosure

@autoclosure attribute defines an argument that automatically gets wrapped in a closure. It’s used to defer the execution of an expression to the site of use, rather than when the argument is passed.

Example:

func evaluate(array: [String], emptyValue: @autoclosure () -> String) {
	if array.size > 0 {
		print("array size \(array.size)")
	} else {
		print(emptyValue())
	}
}

evaluate(["one", "two", "three"], "emptyArray") // array size 3
evaluate([], "blank") // blank

Difference between @escaping and nonescaping

Read on  

  Dec 04, 2016

Debug View for iOS App

It’s a common requirement to tweak different configurations of the app for debug purposes. The general way of doing it is using Settings.bundle where the Settings are located inside Setting of the iPhone. It has a few drawbacks..

  • Setting up Settings.bundle is a hassale and needs some research do to
  • Creating custom pages and changing values on the go are hard
  • There are limitations to what values you can set
  • It’s just cubersome to go back to settings to see how app behavior changes when some configs are tweaked.

**In comes PGDebugView **

Read on  

  Nov 20, 2016

A case for Plist and scripting in Swift

Property List is a convenient and flexible format for saving data. It was originally defined by apple, to use in iOS devices and later extended to various apps. Plist internally is actually an XML file and can easily be converted to JSON file format as well. Xcode provides a nice visual tool to edit and view contents of a .plist file.

Read on  

  Jan 25, 2016

Complex number

Since a complex number is comprised of a real and imaginary component, two complex numbers are equal if and only if their respective real and imaginary components are equal.

Read on