Skip to content

TheAngryDarling/SwiftIndexedStringFormat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IndexedStringFormat

swift >= 4.0 macOS Linux Apache 2

Provides a String constructor where with a withIndexedFormat param and arguments param. Works much like the regular String(format) constructor but supports index mapping of the parameters. This way parametes can be used multiple times or skipped.

The idea behide this was to be able to create packages that requires using developers to generate a string based on a set of arguments when you may not want to expose the argument types themselves.

This format constructor can access properties within an object using the Mirror method, so developers can use access child properties on objects. As well, on platforms that support Objective-C runtime, this constructor can access non-argumented methods the the same format as properties by adding () to the end. This used Selectors to call the method.

As an addition, there is a key:value constructor that works similarly to the withIndexedFormat. withKeyedFormat. It works the same way but instead of using index values it uses the key names.

Usage

struct TestStruct: CustomStringConvertible {
    let string: String
    let int: Int
    let bool: Bool
    let float: Float

    var description: String { return "\(string) - \(int) - \(bool) - \(float)" }
}

class TestSwiftClass: CustomStringConvertible {
    let string: String = "TestSwiftClass"
    var description: String { return string }
}


//Indexed Way
let format: String = "%{0:d}, %{1:@}, %{2:@}, \"%{3:@}\" - %{2:@}, %{1:@}, %{0:@}, \"%{4:@.string}\", \"%{3:@.string}\", %{3:@.float%0.2f}"

let objects: [Any?] = [1, true, nil, TestStruct(string: "String Var", int: 13, bool: false, float: 1.3456), TestSwiftClass()]

let string = String(withIndexedFormat: format, objects)
print(string)


//Keyed Way
let keyedFormat: String = "%{int:d}, %{bool:@}, %{nil:@}, \"%{struct:@}\" - %{nil:@}, %{bool:@}, %{int:@}, \"%{class:@.string}\", \"%{struct:@.string}\", %{struct:@.float%0.2f}"

let keyedObjects: [String: Any?] = ["int": 1,
                                    "bool": true,
                                    "nil": nil,
                                    "struct": TestStruct(string: "String Var", int: 13, bool: false, float: 1.3456),
                                    "class": TestSwiftClass(),
                                    "nsclass": TestNSClass()]
                                    
let keyedString = String(withKeyedFormat: keyedFormat, keyedObjects)
print(keyedString)

Author

License

This project is licensed under Apache License v2.0 - see the LICENSE.md file for details

About

Provides functionality similar to C format method when constructing Swift Strings

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages