Beton is a Swift library built on top of
the Foundation framework, that provides an additional layer of
functionality, including easy localization, performance test measurement support, and convenience functionality.
For us, Beton is primarily, but not exclusively, useful for server-side Swift engineering.
Modules
Beton: Generic purpose functionalities that may be useful for every application.
XCTBeton: Extends the capabilities of XCTest by
providing assertions for performance measurements.
Using the Beton Module
Importing
To use Beton simply import it. If you need anything
from Foundation you do not need to explicitly import it. You get
it for free by importing Beton.
Using Beton it is quite easy to get localized bundles and values from them.
Suppose you have a localization bundle in your project for the hu_HU locale, with a translation for "Apple" = "Alma"
, but you don’t have one for "Banana" (which would be "Banán"). The following example finds the bundle, gets the
localized version of "Apple", and falls back to the given key "Banana".
let bundle = Bundle.module.localizationBundles["hu_HU"]
let localizedApple = bundle?.localizedString("Apple")
// localizedApple == "Alma"
let localizedBanana = bundle?.localizedString("Banana")
// localizedBanana == "Banana"
In Beton measurements have default units and they conform
to AdditiveArithmetic.
let sum = [1, 2, 3].map { Measurement<UnitLength>(value: $0, unit: .default) }.sum()
// sum == 6.0 m
Using the XCTBeton Module
Let’s say you have a simple performance test that measures some code.
Using XCTest there is no easy,
straightforward way to make assertions to the performance results.
To use the Beton library in a SwiftPM project, add it to the dependencies for your package and your target. Your
target can depend on either the Beton or XCTBeton modules, or both.
Beton
Beton
is a Swift library built on top of the Foundation framework, that provides an additional layer of functionality, including easy localization, performance test measurement support, and convenience functionality. For us,Beton
is primarily, but not exclusively, useful for server-side Swift engineering.Modules
Using the Beton Module
Importing
To use
Beton
simply import it. If you need anything from Foundation you do not need to explicitly import it. You get it for free by importingBeton.
Convenience API for
Bundle
Using
Beton
it is quite easy to get localized bundles and values from them.Suppose you have a localization bundle in your project for the
hu_HU
locale, with a translation for"Apple" = "Alma"
, but you don’t have one for"Banana"
(which would be"Banán"
). The following example finds the bundle, gets the localized version of"Apple"
, and falls back to the given key"Banana"
.Convenience API for
Locale
Locales in
Beton
are expressible by string literals.?!
operatorThe
?!
operator unwraps anOptional
value if is notnil
, otherwise throws the given error.sum
extension onSequence
sCalculates the total of all elements in a sequence. Available on any sequence with values that conform to
AdditiveArithmetic
Convenience for
Measurement
sIn
Beton
measurements have default units and they conform toAdditiveArithmetic
.Using the XCTBeton Module
Let’s say you have a simple performance test that measures some code. Using XCTest there is no easy, straightforward way to make assertions to the performance results.
You can turn this code into an
XCTBeton
test by simply changing the import. Yes, that’s it. You can now make assertions!If you want to control the type of measurements, and how many times the tests run you can do that using the same API as you would in regular XCTest.
Adding
Beton
as a DependencyTo use the
Beton
library in a SwiftPM project, add it to the dependencies for your package and your target. Your target can depend on either theBeton
orXCTBeton
modules, or both.