Skip to main content
added 47 characters in body
Source Link
Leo Dabus
  • 234.8k
  • 60
  • 503
  • 594

You can extend RangeReplaceableCollection, constrain its elements to Equatable and declare your method as mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension RangeReplaceableCollection where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !containslet index = firstIndex(of: element) {
            appendreturn (elementfalse, self[index])
        } else {
  return (true, element)
        }append(element)
            return (falsetrue, element)
        }
    }
}

You can extend RangeReplaceableCollection, constrain its elements to Equatable and declare your method as mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension RangeReplaceableCollection where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}

You can extend RangeReplaceableCollection, constrain its elements to Equatable and declare your method as mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension RangeReplaceableCollection where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if let index = firstIndex(of: element) {
            return (false, self[index])
        } else {
            append(element)
            return (true, element)
        }
    }
}
added 21 characters in body
Source Link
Leo Dabus
  • 234.8k
  • 60
  • 503
  • 594

You will need tocan extend RangeReplaceableCollection, constrain your arrayits elements to equatableEquatable and makedeclare your method as mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension ArrayRangeReplaceableCollection where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}

You will need to constrain your array elements to equatable and make your method mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension Array where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}

You can extend RangeReplaceableCollection, constrain its elements to Equatable and declare your method as mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension RangeReplaceableCollection where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}
added 62 characters in body
Source Link
Leo Dabus
  • 234.8k
  • 60
  • 503
  • 594

You will need to constrain your array elements to equatable and make your method mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension Array where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}

You will need to constrain your array elements to equatable and make your method mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension Array where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> Bool {
        if !contains(element) {
            append(element)
            return true
        }
        return false
    }
}

You will need to constrain your array elements to equatable and make your method mutating. If you want to return Bool in case the appends succeeds you can also make the result discardable. Your extension should look like this:

extension Array where Element: Equatable {
    @discardableResult
    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) {
        if !contains(element) {
            append(element)
            return (true, element)
        }
        return (false, element)
    }
}
deleted 15 characters in body
Source Link
Alexander
  • 62k
  • 13
  • 103
  • 163
Loading
added 230 characters in body
Source Link
Leo Dabus
  • 234.8k
  • 60
  • 503
  • 594
Loading
Source Link
Leo Dabus
  • 234.8k
  • 60
  • 503
  • 594
Loading