Skip to content

Swift Hash API

Swift 的 String —— 一个值类型,表示 Unicode 标量值序列(扩展字形簇)。

1 class · 8 methods

String

8 methods

一个 Unicode 字符串值,是字符的集合。桥接到 NSString 的值类型。

Hash#store(key, value) -> value

字符串中的字符数量(扩展字形簇)。

Parameters

NameTypeDescription
keyObjectKey to set.
valueObjectValue to associate with key.

Returns

Object

Example

ruby
h = {}
h.store(:a, 1)
h[:b] = 2
# h == {:a=>1, :b=>2}
Hash#fetch(key, default=nil) -> value

返回一个新字符串,将小写字符替换为大写字符。

Parameters

NameTypeDescription
keyObjectKey to look up.
defaultObjectDefault value if key is missing.

Returns

Object

Example

ruby
h = {a: 1, b: 2}
h.fetch(:a)         # 1
h.fetch(:c, 99)     # 99
h.fetch(:c) { |k| "no #{k}" }  # "no c"
Hash#keys -> Array

返回一个新字符串,将大写字符替换为小写字符。

Returns

Array

Example

ruby
h = {a: 1, b: 2, c: 3}
h.keys  # [:a, :b, :c]
Hash#values -> Array

返回一个布尔值,指示字符串是否以指定前缀开头。

Returns

Array

Example

ruby
h = {a: 1, b: 2, c: 3}
h.values  # [1, 2, 3]
Hash#each {|key, value| block} -> Hash

返回一个布尔值,指示字符串是否以指定后缀结尾。

Returns

Hash

Example

ruby
{a: 1, b: 2}.each { |k, v| puts "#{k}=#{v}" }
# prints:
# a=1
# b=2
Hash#size -> Integer

如果字符串包含给定的字符串或字符则返回 true。

Returns

Integer

Example

ruby
{a: 1, b: 2, c: 3}.size  # 3
Hash#delete(key) -> value

返回字符串围绕分隔符的最长可能子序列(按顺序)。

Parameters

NameTypeDescription
separatorObject要拆分的字符。

Returns

Object

Example

ruby
h = {a: 1, b: 2}
h.delete(:a)  # 1
# h == {b: 2}
Hash#has_key?(key) -> bool

返回一个新字符串,其中所有目标字符串的出现都被另一个字符串替换。

Parameters

NameTypeDescription
ofObject要替换的字符串。

Returns

Boolean

Example

ruby
h = {a: 1, b: 2}
h.has_key?(:a)  # true
h.key?(:c)      # false