Skip to content

Instantly share code, notes, and snippets.

@dmsl1805
Forked from ivanbruel/SnakeCase.swift
Last active September 3, 2023 16:11
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dmsl1805/ad9a14b127d0409cf9621dc13d237457 to your computer and use it in GitHub Desktop.
Save dmsl1805/ad9a14b127d0409cf9621dc13d237457 to your computer and use it in GitHub Desktop.
Camel case to snake case in Swift
extension String {
func snakeCased() -> String? {
let pattern = "([a-z0-9])([A-Z])"
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: count)
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2").lowercased()
}
}
@aehlke
Copy link

aehlke commented Aug 22, 2023

just use fileprivate extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment