vankuik.nl

Latest weblog entries

2017-07-25 Swift dictionary keys to array

The following code can be pasted into a Swift playground, and demonstrates how you can get a mutable array of (a copy of the) dictionary keys.

    let dict = ["Fritz": "Senior Engineer",
                "Mary": "Director of Safety",
                "John": "VP of this and that"]
    var keyArray: [String] = dict.keys.map { $0 }
    print(keyArray)
    if let index = keyArray.index(of: "Fritz") {
        keyArray.remove(at: index)
    }
    print(keyArray)

If you don't cast to an array of Strings, you'll get the following error:

    error: value of type 'LazyMapCollection<LazyMapCollection<Dictionary<String, String>, String>, String>' has no member 'remove'

2017-06-07 Make a list of files left after installing software

Sometimes, Mac software is distributed as an installer. Potentially that leaves a bunch of files strewn all over your Mac its filesystem. One tip is to run the following command before installation:

 $ find -x . /  > before.txt

Then install the software, and run it once. Quit, then run the following two commands:

 $ find -x . /  > after.txt
 $ diff before.txt after.txt > filelist.txt

Now open the filelist.txt and you'll see a rough list of all installed files. Both as a result of the installation, but also takes into account whatever the software wrote in your ~/Library folder.

2017-06-06 Always leave a view in a stackview

When you hide/unhide an element in a UIStackView, it will nicely animate. However when you hide the only element in the stackview, the animation will go wrong. To see what happens, run the following project:

https://github.com/bvankuik/NestedStackViewsShowAndHide

Click the "Toggle Visibility" button. Then tap the "Add dummy view" button and try again.

2017-05-29 Creating a Swift module

When creating a custom view, I find it very useful to make it into a framework and include an example project. This enables quickly iterating over the new component. Plus, when you're done, it's very easy to import the result into other projects. To set this up, take the following steps:

  • Create new project, and choose Cocoa Touch Framework
  • Type some name that reflects a library name such that "import yourlibname" looks logical and good
  • Create a new Swift file, and add your code there
  • Then go to menu File -> New -> Target and create a Single View Application
  • Name it YourlibnameExample or something
  • Select the project, then the new target, and in the Embedded Binaries section, click the plus
  • Select the previously created framework, and click Add
  • Then edit the new ViewController and type "import yourlibname"
  • Then instantiate a class from your framework somewhere; no errors should occur

2017-05-09 Check PDF file header with Swift

Here's a little Swift playground that shows how you can check whether a Data object (for example from a file) is a PDF. It's done by checking the first couple of bytes.

    import UIKit
    // http://stackoverflow.com/a/26503955/1085556
    func dataWithHexString(hex: String) -> Data {
        var hex = hex
        var data = Data()
        while(hex.characters.count > 0) {
            let c: String = hex.substring(to: hex.index(hex.startIndex, offsetBy: 2))
            hex = hex.substring(from: hex.index(hex.startIndex, offsetBy: 2))
            var ch: UInt32 = 0
            Scanner(string: c).scanHexInt32(&ch)
            var char = UInt8(ch)
            data.append(&char, count: 1)
        }
        return data
    }
    struct HeaderError: Error {
    }
    // http://stackoverflow.com/a/17280876/1085556
    let smallestPDFasHex = "255044462D312E0D747261696C65723C3C2F526F6F743C3C2F50616765733C3C2F4B6964735B3C3C2F4D65646961426F785B302030203320335D3E3E5D3E3E3E3E3E3E"
    let data = dataWithHexString(hex: smallestPDFasHex)
    let headerRange: Range<Data.Index> = 0..<4
    let header = data.subdata(in: headerRange)
    guard let headerString = String(data: header, encoding: .ascii) else {
        print("Header not found")
        throw HeaderError()
    }
    if headerString == "%PDF" {
        print("It's a PDF")
    } else {
        print("It's NOT a PDF")
    }

More...

Weblog Archive

Weblog entries 2017

Weblog entries 2016

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: