Swiftpack.co - Package - 0xLeif/KPEdit

KPEdit

Quick KeyPath Editting

Example Usage

/*
class SomeObject {
  var value = ""
}
*/
let someValue = SomeObject()

print(someValue.value) // Output: ""

/*
(ObjectForKeyPath) + (KeyPath) - (ValueOfKeyPath)
*/
someValue + \.value - "Hello World"

print(someValue.value) // Output: "Hello World"

Known Issue

CleanShot 2020-08-21 at 17 20 26

error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions dumb + .string - "Hello!" + .double - 3.14 + .array - [1, 43, 6, true] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~

Solution

Make sure to use parentheses!

var dumb = DumbStruct()
        
(((dumb + \.string - "Hello!")
     + \.double - 3.14)
     + \.array - [1, 43, 6, true])

XCTAssertEqual(dumb.string, "Hello, World!")

Github

link
Stars: 1

Dependencies

Used By

Total: 0

Releases

Basic Key Path Edit - 2020-08-21 22:14:08

Basic Example

XCTAssertEqual((DumbStruct() + \.string - "Hello!").string, "Hello!")
XCTAssertEqual((DumbClass() + \.string - "Hello!").string, "Hello!")

Struct Example

let dumb = DumbStruct()
        
dumb + \.string - "Hello!"
        
XCTAssertEqual(dumb.string, "Hello, World!")

Class Example

let dumb = DumbClass()
        
dumb + \.string - "Hello!"
        
XCTAssertEqual(dumb.string, "Hello!")