Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

SwiftGen – Statically reference resources

by fragi
February 28, 2022
in Tools
Share on FacebookShare on Twitter

In this article we can see how we can statically reference strings.

Let’s look on the following example:

Swift
1
2
3
4
5
6
7
8
9
10
11
struct ContentView: View {
    var body: some View {
        HStack {
            Text("Hello, world!")
            Spacer()
            Text("Today is a sunny day")
        }
        .padding()
 
    }
}

As we described in the http://trikalabs.com/project-mess-1-no-localised-strings/ article we can use localised files so we can have all the user facing strings in one place and also to support localisation easier.

Let’s crate in a similar way our localised file:

Now we can use them in our view:

Swift
1
2
3
4
5
6
7
8
9
10
struct ContentView: View {
    var body: some View {
        HStack {
            Text(NSLocalizedString("WELCOME_GREETING", comment: ""))
            Spacer()
            Text(NSLocalizedString("WELCOME_WEATHER_UPDATE_TEXT", comment: ""))
        }
        .padding()
    }
}

But there are still error prone since we have to hardcode the key.

Let’s now see how we can use the SwiftGen to enable us to statically reference the strings.

  1. Install SwiftGen. In this article we will do it through homebrew
    Swift
    1
    brew install swiftgen
  2. Next create a yml file for the configuration
    Swift
    1
    2
    3
    4
    5
    6
    7
    8
    9
    strings:
      inputs:
        - Accessible/Localizable.strings
      outputs:
        - templateName: structured-swift5
          params:
            publicAccess: true
            enumName: Strings
          output: Accessible/Strings+Generated.swift
  3. Drag the file into Xcode, but don’t add in any target.
  4. Now we can add a run script in the build phases

    Swift
    1
    2
    3
    4
    5
    if which swiftgen >/dev/null; then
      swiftgen
    else
      echo "warning: SwiftGen not installed, download it from https://github.com/SwiftGen/SwiftGen"
    fi
  5. By building the project we can see the autogenerated file
  6. Lets add it to the main target, and let’s open it
  7. And we can now use it to statically refer to our strings
    Swift
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    struct ContentView: View {
        var body: some View {
            HStack {
                Text(Strings.welcomeGreeting)
                Spacer()
                Text(Strings.welcomeWeatherUpdateText)
            }
            .padding()
        }
    }

SwiftGen allow us to create and use our own templates. We will explore this option in another article.

Resources:
https://github.com/SwiftGen/SwiftGen
https://www.raywenderlich.com/23709326-swiftgen-tutorial-for-ios

fragi

fragi

Related Posts

Delete Derived Data – zsh command
Tools

Delete Derived Data – zsh command

April 22, 2022

There are some times that we have to delete the derived data that Xcode produces.There are many ways to achieve...

Postman – Monitors
Postman

Postman – Monitors

February 27, 2022

So we have created our collection with the API request, we an up and running mock server http://trikalabs.com/postman-mock-server/  and a...

Xcode custom template
Tools

Xcode custom template

February 25, 2022

In this article we will create a template that creates a view and a viewModel files.In the previous article http://trikalabs.com/xcode-templates/...

Xcode Templates
Tools

Xcode Templates

February 24, 2022

Xcode provides with a lot of custom templates. In this article we will explore how we can change them. In...

Newman – Run Postman collections tests
Postman

Newman – Run Postman collections tests

February 27, 2022

Newman is a command-line Collection Runner for Postman. We can use newman to run postman collection tests through cli, which...

Postman – Schema Validation
Postman

Postman – Schema Validation

February 27, 2022

Schema validation of an API request can be used to validate if the API returns the expected response. It is...

Next Post
Blake2b Algorithm

Blake2b Algorithm

Blake2b Algorithm

Bech32 Algorithm

Blake2b Algorithm

Operators precedence

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.