Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Swift Macro for automatically generating methods to retrieve property names as strings in a type-safe manner.

License

NotificationsYou must be signed in to change notification settings

CuriositySoftware/swift-property-name

Repository files navigation

BuildSwift Package ManagerLicense

This Swift macro simplifies the process of obtaining property names as strings in Swift. By annotating a struct or class with@PropertyNameAccessible, it automatically generates extensions and methods that provide access to property names as strings.

Quick start

To get started, import:import PropertyName, annotate your struct or class with@PropertyNameAccessible:

import PropertyName@PropertyNameAccessiblestructPerson{letname:Stringletage:Intletemail:StringletisEmployee:Bool}

This will automatically generate an extension with apropertyName(for:) function.

extensionPerson{staticfunc propertyName(for keyPath:PartialKeyPath<Self>)->String{switch keyPath{case \.name:return"name"case \.age:return"age"     case \.email:return"email"case \.isEmployee:return"isEmployee"default:fatalError()}}}

Usage examples are as follows:

print(Person.propertyName(for: \.name)) // => "name"print(Person.propertyName(for: \.age)) // => "age"print(Person.propertyName(for: \.email)) // => "email"print(Person.propertyName(for: \.isEmployee)) // => "isEmployee"

Installation

For Xcode

If you are usingGUI to set up Package Dependencies in Xcode, add the URL in Package Dependencies.

https://github.com/CuriositySoftware/swift-property-name

For Package.swift

If you are using Package.swift add:

.package(    url:"https://github.com/CuriositySoftware/swift-property-name/",.upToNextMajor(from:"0.1.0"))

and then add the product to any target that needs access to the macro:

.target(    name:"YourTarget",    dependencies:[.product(            name:"PropertyName",package:"swift-property-name")])

About

Swift Macro for automatically generating methods to retrieve property names as strings in a type-safe manner.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp