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

Separate configuration from code – xcconfig

by fragi
January 27, 2022
in Tools
Share on FacebookShare on Twitter

It is a best practice the separation of configuration from code.
In this article we will use Configuration Settings File to achieve this.

According to Apple (https://help.apple.com/xcode/mac/current/#/dev745c5c974)

A Configuration Settings File (a file with a .xcconfig file extension), also known as a build configuration file or xcconfig file,
is a plain text file that defines and overrides the build settings for a particular build configuration of a project or target.
This type of file can be edited outside of Xcode and integrates well with source control systems. Build configuration files adhere to specific formatting rules, and produce build warnings if they do not.

And that brings us to what is a build setting?
According to Apple (https://help.apple.com/xcode/mac/current/#/dev382dac089)

A build setting provides information necessary for building the product of a target. For each task performed during the build process—such as compiling Swift code—build settings control how that task is performed.

Values on the xcconfig files will override the values from the build settings.

The syntax is simple
e.g

Swift
1
A_KEY = 1

every value si considered string and “//” is for commnets
If we want to have a url as value we have to escape it:

Swift
1
https:/$()/google.com

some extra capabilities are:

– include build settings from other xcconfig files

Swift
1
#include "AnotherFile.xcconfig"

– reference other values

Swift
1
2
A_KEY = 1
ANOTHER_KEY = $(A_KEY)

 

To add an xcconfig we have to crate a Configurations Settings File:

The we have to add the configuration file  to the configuration set of the target:

In order to have access to the config values we have to add them to the Info.plist:

And we can add write code to access them:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Code copied from: https://nshipster.com/xcconfig/
 
enum Configuration {
    enum Error: Swift.Error {
        case missingKey, invalidValue
    }
 
    static func value<T>(for key: String) throws -> T where T: LosslessStringConvertible {
        guard let object = Bundle.main.object(forInfoDictionaryKey:key) else {
            throw Error.missingKey
        }
 
        switch object {
        case let value as T:
            return value
        case let string as String:
            guard let value = T(string) else { fallthrough }
            return value
        default:
            throw Error.invalidValue
        }
    }
}

 

Resources:

https://nshipster.com/xcconfig/

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...

SwiftGen – Statically reference resources
Tools

SwiftGen – Statically reference resources

February 28, 2022

In this article we can see how we can statically reference strings. Let's look on the following example: As...

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...

Next Post
Separate configuration from code – xcconfig

XcodeGen

BIP39

BIP39

How to wrap c code in a swift package

How to wrap c code in a swift package

  • 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.