如何找到数组中每个项目的字符数
How do I find the character count of each item in an array
我正在编写一个函数,用于打印字典中字符数超过 8 的字符串值。以下是我目前所拥有的,但我不确定如何制定我的 where 条件以使其看起来数组中每个字符串值的字符数。
var stateCodes = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
let fullStateNames = Array(stateCodes.values)
for _ in fullStateNames where fullStateNames.count > 8 {
print(fullStateNames)
return fullStateNames
}
return fullStateNames
}
printLongState(stateCodes)
如果你想用for循环,那么你可以这样做。
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
var fullStateNames = [String]()
for (_, value) in dictionary where value.characters.count > 8 {
fullStateNames.append(value)
}
return fullStateNames
}
但这不是 SwiftSwift 中的方法你可以做的是你可以使用 flatMap
和你的 Dictionary
来制作 [=15= 的数组] 或使用 dictionary.values.filter
将 flatMap 与字典一起使用
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.flatMap { .characters.count > 8 ? : nil }
}
// Call it like this way.
var stateCodes = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
print(printLongState(stateCodes)) //["Wisconsin", "New Jersey"]
在 dictionary.values
上使用过滤器
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.values.filter { [=12=].characters.count > 8 }
}
只是 filter
你的结果而不是使用 for-loop
:
如果你想 return 字典使用以下:
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
let overEightChars = stateCodes.filter({ [=10=].value.characters.count > 8 })
return overEightChars
}
如果您想 return 一个字符串数组,请使用以下内容:
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.values.filter { [=11=].characters.count > 8 }
}
尝试像这样将 filter
与 characters.count
一起使用:
var states = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
states.filter({ (_, value) -> Bool in
return value.characters.count > 8
}).map({ (_, value) in
print(value)
})
我正在编写一个函数,用于打印字典中字符数超过 8 的字符串值。以下是我目前所拥有的,但我不确定如何制定我的 where 条件以使其看起来数组中每个字符串值的字符数。
var stateCodes = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
let fullStateNames = Array(stateCodes.values)
for _ in fullStateNames where fullStateNames.count > 8 {
print(fullStateNames)
return fullStateNames
}
return fullStateNames
}
printLongState(stateCodes)
如果你想用for循环,那么你可以这样做。
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
var fullStateNames = [String]()
for (_, value) in dictionary where value.characters.count > 8 {
fullStateNames.append(value)
}
return fullStateNames
}
但这不是 SwiftSwift 中的方法你可以做的是你可以使用 flatMap
和你的 Dictionary
来制作 [=15= 的数组] 或使用 dictionary.values.filter
将 flatMap 与字典一起使用
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.flatMap { .characters.count > 8 ? : nil }
}
// Call it like this way.
var stateCodes = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
print(printLongState(stateCodes)) //["Wisconsin", "New Jersey"]
在 dictionary.values
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.values.filter { [=12=].characters.count > 8 }
}
只是 filter
你的结果而不是使用 for-loop
:
如果你想 return 字典使用以下:
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
let overEightChars = stateCodes.filter({ [=10=].value.characters.count > 8 })
return overEightChars
}
如果您想 return 一个字符串数组,请使用以下内容:
func printLongState (_ dictionary: [String: String]) -> (Array<Any>) {
return dictionary.values.filter { [=11=].characters.count > 8 }
}
尝试像这样将 filter
与 characters.count
一起使用:
var states = ["NJ": "New Jersey", "CO": "Colorado", "WI": "Wisconsin", "OH": "Ohio"]
states.filter({ (_, value) -> Bool in
return value.characters.count > 8
}).map({ (_, value) in
print(value)
})