Swift 2.2, Contains Method not working
OPTION 1
The reason is that your class is inheriting from NSObject
, therefore, you must fulfill the NSObjectProtocol
instead of Equatable
:
override func isEqual(object: AnyObject?) -> Bool {
if let object = object as? Generic {
return self.genericCode == object.genericCode
}
return false
}
OPTION 2
Inherit from Equatable
instead of NSObject
(you probably can't use this option, since you seem to need NSCoding
)
Note that with newer versions of Swift you probably should use
OPTION 3: Use Codable
instead of NSCodable
Array contains method is not working properly with custom models and predicate
The code is correct.
You are checking
Does
a
contain aHello
whose phone number is not 123.
This is true
because there are even two items with a different phone number
Can't use method contains in Swift 2
This is because the contains()
method is defined in a protocol extension of Sequence
.
So you should call it this way:
currentCardValues.contains(randomNumber + 1)
Swift 4 contains method doesn't work truly for short words like as
You need to use a regular expression to make sure the word you're looking for has word boundaries on both sides (instead of other letters). Use:
if a.range(of: "\\bas\\b", options: [.regularExpression], range: (a.startIndex..<a.endIndex), locale: nil) != nil {
The regular expression keyword \\b
is to match a word boundary.
Set's contains method returns different value at different time
The synthesized implementation of the Hashable
requirement uses all stored
properties of a struct
, in your case string
and number
. Your implementation
of ==
is only based on the string:
let first = SimpleStruct(string: "a", number: 2)
let second = SimpleStruct(string: "a", number: 3)
print(first == second) // true
print(first.hashValue == second.hashValue) // false
This is a violation of a requirement of the Hashable
protocol:
Two instances that are equal must feed the same values to Hasher in hash(into:), in the same order.
and causes the undefined behavior. (And since hash values are randomized
since Swift 4.2, the behavior can be different in each program run.)
What probably happens in your test is that the hash value of second
is used to determine the “bucket” of the set in which the value
would be stored. That may or may not be the same bucket in which first
is stored. – But that is an implementation detail: Undefined behavior is undefined behavior, it can cause unexpected results or even
runtime errors.
Implementing
var hashValue: Int {
return string.hashValue
}
or alternatively (starting with Swift 4.2)
func hash(into hasher: inout Hasher) {
hasher.combine(string)
}
fixes the rule violation, and therefore makes your code behave as expected.
Swift 2 Array Contains object?
Your Dog
needs to implement Equatable
.
class Dog: Equatable {
var age = 1
}
func == (lhs: Dog, rhs: Dog) -> Bool {
return lhs.age == rhs.age
}
Why does string.contains ( text ) not work with strings in swift 5?
When you use contains()
and pass it a String
, Swift tries to use an overload of the function that takes some kind of string like contains(_ other: StringProtocol) function that is not part of the pure Swift String
. Instead it finds contains(_ element: Character) and it can't accept String as an argument and only accepts 'String.Element' (aka 'Character'). Referring to contains
The function you are looking for is defined in a protocol that String conforms to it, called StringProtocol
that lives inside the Foundation
.
So if you need it, make sure you import Foundation
or a higher level framework like UIKit
.
Related Topics
Count the Number of Lines in a Swift String
Why Use Float(Arc4Random())/0Xffffffff Instead of Drand()
Perform Migration by Adding List() and Another Model Class
Diffrence Between Function and Generic Function in Swift
Why Does My @Lazy Property Crash, But If I Make It Non Lazy It Works
Swiftui: Localizedstringkey with Indices
How to Detect the 2D Images Using Arkit and Realitykit
If-Let Any to Rawrepresentable<String>
Saving Dictionary into Nsuserdefaults
Why Doesn't Swift Force My Designated Initializer to Call Super
How to Capitalize First Word in Every Sentence with Swift
Unsafemutablepointer<Cftyperef> in Swift 3
How to Cast Generic Number Type 'T' to Cgfloat
Cursor Shifts to End on Edit of Formatted Decimal Textfield - Swift
Swiftui Casting Tupleview to an Array of Anyview
How to Open a Url in Safari Even If My Default Browser Is Not Safari Using Swift in MACos
Why Can't Swift Automatically Convert a Generic Type Parameter to Its Superclass