Instance Method インスタンスメソッド

insert(_:)

Inserts the given element in the set if it is not already present. それが既に存在するのでないならば、与えられた要素を集合へと挿入します。

Declaration 宣言

@discardableResult mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element)

Parameters パラメータ

newMember

An element to insert into the set. この集合に挿入されることになる要素。

Return Value 戻り値

(true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means. (true, newMember)、もしnewMemberがこの集合の中に含まれなかったならば。newMemberに等しい要素が既に集合に含まれたならば、メソッドは(false, oldMember)を返します、ここでoldMembernewMemberに等しかった要素です。いくつかの場合には、oldMemberは同一性比較または何らかの他の意味でnewMemberから区別可能であるかもしれません。

Discussion 解説

If an element equal to newMember is already contained in the set, this method has no effect. In the following example, a new element is inserted into classDays, a set of days of the week. When an existing element is inserted, the classDays set does not change. newMemberに等しい要素が既にこの集合に含まれるならば、このメソッドは効果を持ちません。以下の例において、新しい要素が、一揃いの曜日classDaysへ挿入されます。既存の要素が挿入される時、classDays集合は変化しません。


enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday
}


var classDays: Set<DayOfTheWeek> = [.wednesday, .friday]
print(classDays.insert(.monday))
// Prints "(inserted: true, memberAfterInsert: DayOfTheWeek.monday)"
print(classDays)
// Prints "[DayOfTheWeek.friday, DayOfTheWeek.wednesday, DayOfTheWeek.monday]"


print(classDays.insert(.friday))
// Prints "(inserted: false, memberAfterInsert: DayOfTheWeek.friday)"
print(classDays)
// Prints "[DayOfTheWeek.friday, DayOfTheWeek.wednesday, DayOfTheWeek.monday]"

Relationships 関係

From Protocol 由来プロトコル

See Also 参照

Adding Elements 要素の追加