vankuik.nl

Latest weblog entries

2015-08-31 Centered popover on the iPad

Below is a nice centered popover on the iPad:

popover1.png

You can do this by configuring the segue in Interface Builder by:

  • (obviously) configuring as "Present As Popover"
  • Uncheck all directions
  • Set the anchor to the main view of the view controller

This is in Xcode 6.4:

xcode popover.png

It also nicely shifts up when the keyboard appears:

popover2.png

2015-08-27 Swift Switch must be exhaustive

In Swift, the switch/case statement must contain a default case, otherwise you'll get the following compiler error:

    Switch must be exhaustive, consider adding a default clause

We used to put a non-sensical log statement in there, but today, a colleague found out that you can use the break keyword.

Example:

	switch (self.type!.integerValue) {
	case CustomerTypeNormal.integerValue:
		name = "\(self.lastname?.lowercaseString.capitalizedString), \(self.firstname?.lowercaseString.capitalizedString)"
	case CustomerTypeCrew.integerValue:
		name =  "Crew"
	case CustomerTypeAnonymous.integerValue:
		name = "Anonymous"
	default:
		break
	}

2015-08-27 Open underlying SQLite database when using Core Data

During development of an app using Core Data, you often want to have a quick peep in the underlying SQLite store. Paste the following bit somewhere in your AppDelegate:

	// Log path (to manually open SQLite database) when in simulator. In the File -> Open dialog of
	// SQLPro for SQLite, type Shift-CMD-G and paste this path
	if TARGET_IPHONE_SIMULATOR == 1 {
		let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as! String
		println("Documents folder: \(path)")
	}

You can use the commandline sqlite client, but I like SQLPro for SQLite. Start your app, copy the string from the console in Xcode, then go to SQLPro and click the Open button. In the dialogue, type Shift-Cmd-G and you can paste the path of the Document folder of your currently running app. You can then open the SQLite database and peek inside.

2015-08-21 lastPathComponent is unavailable

You may get something like the following error when you open your Swift code in Xcode 7 beta:

  'lastPathComponent' is unavailable: Use lastPathComponent on NSString instead.

Solution is to do something like this:

  let lastPathComponent = (fileName as NSString).lastPathComponent

2015-08-07 Swift Reflect example

Halfway last month, Erica Sadun wrote a blog entry where she used the reflect() function: Swift: Just because

She just gave the code and didn't comment any further so below, I've liberally sprinkled the code with comments:

	// Define a struct to represent a point
	struct Pt {
		let x: Int
		let y: Int
		init(_ x: Int, _ y: Int) {self.x = x; self.y = y}
		init(){self.init(0, 0)}
	}
	// Declare an instance of Pt
	let p = Pt(2, 3)
	// Any means: any reference (i.e. class) or value type (i.e. struct, number, etc)
	// So this function will take an item that can be basically anything in Swift
	// and look through the names of its members to see if the string matches.
	func valueForKey(item : Any, key: String) -> Any? {
		// The built-in Swift reflect() function is used by Xcode to aid in debugging
		let mirror = reflect(item)
		// It returns an array, walk through it
		for index in 0..<mirror.count {
			// Each item in the array has a member called 0 and 1
			// This assignment is only there for readability's sake
			let child = mirror[index]
			// The .0 member is a string and it holds the name of our struct member
			if key == child.0 {
				// The .1 member holds information about our struct member
				// If it's not an optional, return its value with the .value property
				return child.1.value
			}
		}
		return nil
	}
	// Try it out
	valueForKey(p, "x")

Instead of using the valueForKey() function, you can also open a Playground and try out a few things yourself:

	let mirror = reflect(p)
	println(mirror[0])
	println(mirror[0].0)
	println(mirror[0].1)
	println(mirror[0].1.value)

swift reflect.png

More...

Weblog Archive

Weblog entries 2015

Weblog entries 2014

Weblog entries 2013

Weblog entries 2012

Weblog entries 2011

Weblog entries 2010

Weblog entries 2009

Weblog entries 2008

Weblog entries 2007

Weblog entries 2006

Weblog entries 2005

Weblog entries 2004

All weblog entries

Articles

Articles, chronologically (latest first). This is pretty old stuff.

Scribblings

Not yet finished. Maybe will never be finished. Maybe they'll get deleted. Who knows?

Programming:

System administration:

Others:

Files: