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

insert(_:)

Inserts the given element in the set if it is not already present.

Declaration 宣言

mutating func insert(_ newMember: AccessibilityTraits) -> (inserted: Bool, memberAfterInsert: AccessibilityTraits)

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から区別可能であるかもしれません。

Parameters パラメータ

newMember

An element to insert into the set.

Discussion 議論

If an element equal to newMember is already contained in the set, this method has no effect. In this 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.


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


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


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