本文介绍了Swift 3/4 dash to camel Case (Snake to camelCase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对骆驼案例执行简单的破折号,因此:this-is-my-id"将在 swift 3(或 4)中变成thisIsMyId".

I am trying to perform a simple dash to camel case so:"this-is-my-id" will become "thisIsMyId" in swift 3 (or 4).

无论我做什么,我都找不到足够优雅的方式来做到这一点..:以下不起作用:

No matter what I do I can't find an elegant enough way to do it..:The following doesn't work:

str.split(separator: "-").enumerated().map { (index, element) in
    return index > 0 ? element.capitalized : element
}.reduce("", +)

它为一堆东西哭泣.我相信有一个足够简单的方法......有人吗?

It cries about all bunch of stuff. I am sure there is a simple enough way... Anyone?

推荐答案

所有这些答案的组合将导致以下最短方式(我认为 - 只有 2 个交互):

A combination of all these answers will lead to the following, shortest way (I think - only 2 interations):

let str: String = "this-is-my-id"

let result = str.split(separator: "-").reduce("") {(acc, name) in
    "\(acc)\(acc.count > 0 ? String(name.capitalized) : String(name))"
}

谢谢大家!

这篇关于Swift 3/4 dash to camel Case (Snake to camelCase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:06