Wrapping models in SwiftUI for Identifiable conformance

Using array of models in List view in SwiftUI requires conformance to Identifiable protocol.

Here's an example of model:

struct MyDataModel {
    let title: String
    let message: String

Example of SwiftUI view that you want to display a list:

struct TestView: View {
    var dataArray: [MyDataModel]

    var body: some View {
        List(dataArray) { data in
            VStack {

Using MyDataModel in List will show you error in Xcode:

Sometimes you can't just change the model. It might be a data model from third part SDK that you're using in you app.

But you can create a wrapper for this struct to confirm Identifiable protocol:

struct MyDataModelWrapper: Identifiable {
    var id = UUID()
    var data: MyDataModel
let testDataModel = MyDataModel(title: "Title 1", message: "I wanna be used inside of List")
let wrappedData = MyDataModelWrapper(data: testDataModel)

So view will look like this:

struct TestView: View {
    var dataArray: [MyDataModelWrapper]

    var body: some View {
        List(dataArray) { wrappedData in
            VStack {


comment comments
local_offer swiftui swift

Syntax sugar for JSON parsing in Swift

JSON parsing and encoding became easy after adding Codable protocol. But during coding I wanted to have something shorter and more elegant than Do-Catch statement that looks like this:

var myModel: MyModel?
do {
  myModel = try decoder.decode(MyModel.self, from: data)
} catch (let error) {

Or like this:

let myModel: MyModel? = try? decoder.decode(MyModel.self, from: data)

So I wrote a protocol with default implementation that allows to do just that:

let myModel = MyModel.decodeFromData(data: data)

And the same for encoding:

let data = MyModel.encode(fromEncodable: myModel)

All you need is just protocol conformance:

extension MyModel: Parseable {
  typealias ParseableType = Self

It's available on GitHub and might be used with Swift Package Manager:

comment comments

Using GitHub Actions as CI for building Swift project

Here's config for GitHub Actions workflow to build a Swift project. This example is for building a Vapor project using Swift 5.0.3 on Ubuntu 18.04:

name: Ubuntu 18.04 Swift 5.0.3
on: [push]
    runs-on: ubuntu-18.04
    - name: Install dependencies
      run: sudo apt-get update; sudo apt-get install -yq libssl-dev zlib1g-dev
    - name: Checkout
      uses: actions/checkout@master
    - name: Download Swift
      run: curl --output swift.tar.gz
    - name: Unpack Swift
      run: |
          tar xzf swift.tar.gz
          mv swift-5.0.3-RELEASE-ubuntu18.04 swift
    - name: Swift build
      run: |
          export PATH=$(pwd)/swift/usr/bin:"${PATH}"
          swift build -c release

comment comments

Adding Dark Mode support for website

iOS 13 coming this fall will include dark mode. Current macOS already has dark mode. With bringing dark mode to iOS Apple also recommends to web developers to adopt colors for dark mode.

It's pretty easy. All you need is to change CSS for your website and add something like that:

:root {
  color-scheme: light dark;
  --body-background-color: white;
  --body-text-color: #2F2F2F;

@media (prefers-color-scheme: dark) {
  :root {
    --body-background-color: #222222;
    --body-text-color: #E2E2E2;

body {
  background-color: var(--body-background-color);
  color: var(--body-text-color);

So you're adding support for color schemes with color-scheme: light dark; and use CSS variables.

If you need to test it you can try latest Safari in latest betas of iOS or macOS or (maybe) Safari developer preview. Developer tools in Safari has special button to test dark mode.

Interesting: in macOS 10.15 default body background for dark mode is also dark. But in 10.14 it will be white in Safari and Firefox. You need to specify background color for dark scheme if you want to support macOS < 10.15. Apple didn't mention that during WWDC session. All they did was just removing background-color property from CSS to let OS choose background color. They provide NSHipster website as an example but it's CSS also contains background color property in CSS to support previous macOS versions :)

I've added dark scheme support to my Space In Box site. Also check NSHipster site that has it too.

This article is some kind of summary from WWDC video Supporting Dark Mode in Your Web Content.

comment comments

Benchmarks: Vapor 3 vs. Vapor 2

After migrating (almost rewriting) my small project from Vapor 2 to Vapor 3 I've run benchmarks to compare performance. I didn't run benchmarks for the last version of Vapor 2.x so I will compare Vapor 2.1.0 to results of Vapor 3.1.0.

My server:

  • 2 GB RAM
  • 1 CPU Core
  • SSD
  • 125 MBPS Out
  • Ubuntu 16.04.2 LTS
  • CouchDB

Benchmark from other server was launched as:

wrk -t4 -c20 -d5m https://my_url

API just gets some data from CouchDB and returns it as JSON. Vapor project was compiled with Swift 4.2.

Total requests


Requests per second


Average Latency


Memory usage


Vapor 3 was rewriten and now it's based on Apple's NIO. It's faster than Vapor 2 but using more memory which is not critical.

Previous benchmarks:
Benchmarks: Vapor 2 vs. Vapor 1
Benchmarks for Vapor 1.2.5
Swift Backend with CouchDB: Kitura vs. Vapor vs. Node.js

comment comments

Select like a Boss для Safari в Mac App Store

24 сентября вышла новая версия macOS - Mojave. А в месте с ней и Safari 12, который теперь поддерживает установку расширений только из Mac App Store. Пришлось запилить приложение с расширением.


comment comments

JPG to HEIF converter

Today I've decided to experiment with HEIF that was introduced by Apple with macOS 10.13 and iOS 11. They said that it has the same quality with much lower file size.

It's supported in last versions of macOS and iOS and last models of iPhone can take pictures in that format. 

I'm thinking about compressing my home archive of photos and videos. I was experimenting with HEVC (H.265) and results was great - I had reduced size of my videos with endoding them to HEVC with awesome util called HandBrake.

I found the only way to convert JPG to HEIF (which has HEIC extension). It's possible with Preview app from macOS. But it was interesting for me if I can do it by myself in Swift. So I did :)

My folder with 64 photos from my action camera was 203 mb in JPG format. And it's only 31.3 mb in HEIC.

macOS and iOS support it from the box. And what's interesing - it's easy to add pictures in this format to Photos library and it will appear on all your devices that uses iCloud. But if you want to export this picture from Photos library - it will be converted to JPG on the fly, so it looks like you can't export original HEIC file.

The converter is free and open source (but it's for macOS only):

comment comments