Code Monkey home page Code Monkey logo

swiftindexedstringformat's Introduction

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

swiftindexedstringformat's People

Contributors

theangrydarling avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.