Check if class is equal swift - swift

I have a class where I have an extension to check equality but on test, the equality crashes.
here's my code
extension LPItemAction {
public override func isEqual(_ other: Any?) -> Bool {
if (other as? LPItemAction) == self {
return true
} else if !(other is LPItemAction) {
return false
} else {
let otherAction = other as? LPItemAction
return hash == otherAction?.hash
}
}
}
and my test case is like this
func testIsEqualSelf() {
// Given
let action = LPItemAction()
action.type = .account
// When
let equal = action.isEqual(action)
// Then
XCTAssertTrue(equal)
}
I got a crash with error Thread 1: EXC_BAD_ACCESS (code=2, address=0x16e747fc0)

Since this is obviously a NSObject, you are probably right to override isEqual. There are some rules though.
You cannot use ==. This operator invokes the Equality protocol, which, on NSObject is implemented using isEqual, therefore you end up with infinite recursion.
Another thing is that using hash to compare equality is just wrong.
// test type first
guard let otherAction = other as? LPItemAction else {
return false
}
// test reference equality
if self === otherAction {
return true
}
// test equality of all properties
return type === otherAction.type

Related

A way to compare generics in Swift only if they conform to Equatable

Wondering if there's a good way to do this or not:
I have a #propertyWrapper named "Enhanced" that I use. I use the wrappedValue.set to do some actions, and I would also like to do some further actions if the property is Equatable.
Currently, the code looks like this:
#propertyWrapper
class Enhanced<T: Equatable>: Codable
{
private var value: T
var projectedValue: Enhanced<T> { self }
var wrappedValue: T
{
get { value }
set { set(newValue, notify: nil) }
}
func set(_ proposedValue: T, notify: Bool?)
{
let oldValue = value
let newValue = proposedValue
let changed = newValue != oldValue
if changed { /* more magic here */ }
value = newValue
}
}
Now I would like to remove the Equatable conformance over the generic T, but still be able to compare the old and new values IF the generic T conforms to Equatable.
I've tried a handful of techniques, all of which dead end somewhere. My latest was this:
let changed: Bool
switch T.self
{
case let equatableType as any Equatable.Type:
if
let oldEquatableValue = oldValue as? any Equatable,
let newEquatableValue = newValue as? any Equatable
{
changed = newEquatableValue != oldEquatableValue
}
default:
changed = true
}
...but the error is an understandable Binary operator '!=' cannot be applied to two 'any Equatable' operands.
I tried different patterns to cast the generic type T into an Equatable and silently fail if the generic does not conform, but even if they do, the resulting "cast" types I get back aren't equatable themselves.
Any revelations to the proper pattern would be great!
After some deep web-sleuthing, I came across a snippet of code that does the magic I need:
private extension Equatable
{
func isEqualTo(_ rhs: Any) -> Bool
{
if let castRHS = rhs as? Self
{
return self == castRHS
}
else
{
return false
}
}
}
(HT to neonichu/equalizer.swift on GitHub)
With this bit of pseudo type-erasure, I can make this work:
let changed: Bool
if let oldEquatableValue = oldValue as? any Equatable,
let newEquatableValue = newValue as? any Equatable
{
changed = oldEquatableValue.isEqualTo(newEquatableValue) == false
}
else
{
changed = true
}
By using an extension on Equatable that does further casting of the values, this allows for these two values to be compared (and fail if they are not the same).
Hope this helps someone else!

'[Task]?' is not convertible to 'Optional<[Any]>'

I've made extension
extension Optional where Wrapped == [Any] {
var isNilOrEmpty: Bool {
get {
if let array = self {
return array.count == 0
} else {
return false
}
}
}
}
Then I try to use it like this
if fetchedResults.fetchedObjects.isNilOrEmpty { ... }
I'm getting error
'[Task]?' is not convertible to 'Optional<[Any]>'
But, by specification
Any can represent an instance of any type at all, including function types.
What is my mistake here?
Task is subclass of NSManagedObject if it matters.
Well, [Task] and [Any] are two different types, and Wrapped == [Any] won't work.
Proper way would be to limit Wrapped by protocol, not specific type.
extension Optional where Wrapped: Collection {
var isNilOrEmpty: Bool {
get { // `get` can be omitted here, btw
if let collection = self {
return collection.isEmpty // Prefer `isEmpty` over `.count == 0`
} else {
return true // If it's `nil` it should return `true` too
}
}
}
}

Compare / Equatable weak generics in Swift

I want to create an array of weak referenced delegates like so...
fileprivate class WeakDelegate<T:AnyObject> {
weak var value:T?
init (value:T) {
self.value = value
}
}
class Radio {
private var delegates = [WeakDelegate<AnyObject>]()
}
So far so good...? what I'd like to do also is tidy up my array in these two ways...
1.
func removeDeadDelegates() {
let liveDelegates = delegates.filter { $0.value != nil }
delegates = liveDelegates
}
and 2.
func remove<T>(specificDelegate:T) {
let filteredDelegates = delegates.filter { $0.value != specificDelegate }
listeners = filteredDelegates
}
Says Cannot convert value of type 'T' to expected argument type '_OptionalNilComparisonType'
Now I can just add this to make the warning go away like this...
let liveDelegates = delegates.filter {
if let d = specificDelegate as? _OptionalNilComparisonType {
return $0.value != d
}
return true
}
but this cast doesn't work...
I'm concerned because I'm not sure what this means... can anyone explain why I can't compare generics with == and why this cast is failing?
Thanks for your time
EDIT
Like this?
func remove<T:AnyObject>(delegate:T) {
let filteredDelegates = delegates.filter { $0.value != delegate }
delegates = filteredDelegates
}
No joy sadly...
Instances of a class type can be compared with the “identical to”
=== and “not identical to” !== operators:
func remove(specificDelegate: AnyObject) {
let filteredDelegates = delegates.filter { $0.value !== specificDelegate }
delegates = filteredDelegates
}
The same works for a generic method
func remove<T:AnyObject>(specificDelegate: T) {
let filteredDelegates = delegates.filter { $0.value !== specificDelegate }
delegates = filteredDelegates
}
(but I do not yet see the advantage of doing so).

Swift indexOf, == operator override not getting called

I am getting frustrated with how Swift handles equality. Or I'm just missing something. How come these 2 indexOf DOESN'T work the same way?
let first = self.objects.indexOf(object) //This returns nil
let second = self.objects.indexOf{$0 == object} //This returns the index
My == override:
func ==(lhs: MyObject, rhs: MyObject) -> Bool {
return lhs.someProperty == rhs.someProperty
}
The == override doesn't get called in the first indexOf. Why is that so? This feels really dangerous.
(MyObject is a subclass of PFObject (Parse.com objects). I don't know if this is what's messing this up.)
Since your Object is subclass of PFObject, and PFObject is subclass of NSObject, and NSObject already confirm to Equatable by using the isEqual method to implement. So your == operator override is not working. You should override the isEqual method. (To be honest, it's horrible :(
sample code:
class Object: NSObject {
var value: Int
init(value: Int) {
self.value = value
}
override func isEqual(object: AnyObject?) -> Bool {
guard let obj = object as? Object else { return false }
return self.value == obj.value
}
}
// If your class is not inherit from any ObjC class:
//extension Object: Equatable {}
//func ==(lhs: Object, rhs: Object) -> Bool {
// return lhs.value == rhs.value
//}
let c = Object(value: 3)
let objs = [Object(value: 1), Object(value: 2), Object(value: 3)]
let index = objs.indexOf(c)
let index2 = objs.indexOf { $0 == c }
print(index) // Optional(2)
print(index2) // Optional(2)
The second indexOf method that you are using allows you to define a closure to indicate object equality (like you have already done). The first one according to the documentation:
Returns the first index where value appears in self or nil if value is not found.
Where value is the object you are passing as the parameter. Without seeing the implementation of this version I can only assume they compare if the two objects are equal in every way (all properties match). So in your case, if you only consider the objects equal if the somePropertys match, then you should be using the closure method with your overloaded operator.
static func == (left: MyClass, right: MyClass) -> Bool {
return left.attribute1 == right.attribute1
}
override func isEqual(_ object: Any?) -> Bool {
guard let right = object as? MyClass else {
return false
}
// call the == from above
// why couldn't apple do this ???
//
return self == right
}

Unexpected results from Swift is Operator

I'm trying to understand where this code went wrong given the code below.
In the code below, I'm trying to locate a UIViewController of a specific class in the UITabBarController's viewControllers property which is declared as:
var viewControllers: [AnyObject]?
So I'm defining two UIViewController subclasses and stuffing them to the viewControllers array and running two different methods to extract them "viewControllerInfo:" ""viewControllerInfo2".
Which both yield the same result.
My understanding is the:
if let x as? T will evaluate true and assign x as a "T" type if it is the same class.
Just as if x is T would.
Any idea why evaluation is behaving like this ?
class VC1: UIViewController {}
class VC2: UIViewController {}
let tabBar = UITabBarController()
tabBar.viewControllers = [VC1(), VC2()]
extension UITabBarController {
public func viewControllerInfo<T: UIViewController>(ofType: T.Type) -> (viewController: T,index: Int)? {
if let tabViewControllers = self.viewControllers{
for (idx, maybeVCTypeT) in enumerate(tabViewControllers) {
if let viewController = maybeVCTypeT as? T {
return (viewController, idx)
}
}
}
return nil
}
public func viewControllerInfo2<T: UIViewController>(ofType: T.Type) -> (viewController: T,index: Int)? {
if let tabViewControllers = self.viewControllers{
for (idx, maybeVCTypeT) in enumerate(tabViewControllers) {
if maybeVCTypeT is T {
return (maybeVCTypeT as! T, idx)
}
}
}
return nil
}
}
All the tests below will end up giving exactly the same result:
"<__lldb_expr_89.VC1: 0x7f85016079f0>"
if let (vc, idx) = tabBar.viewControllerInfo(VC1.self) {
println(vc)
}
if let (vc, idx) = tabBar.viewControllerInfo(VC2.self) {
println(vc)
}
if let (vc, idx) = tabBar.viewControllerInfo2(VC1.self) {
println(vc)
}
if let (vc, idx) = tabBar.viewControllerInfo2(VC2.self) {
println(vc)
}
I'm suspicious of the enumerate(x) since without it I'm getting expected results:
if testVC1 is VC2 {
println("\(testVC1) is \(VC2.self)")
}
the above code yields a warning:
Cast from 'VC1' to unrelated type 'VC2' always fails
Which is what i'm trying to achieve with enumerate...
***************** EDIT *****************
Actually my suspicion of enumerate is dissolved after running the following code which ran perfectly.
let myArray: [AnyObject] = [VC2(), VC1()]
for (idx, x) in enumerate(myArray) {
println(x)
if let xAsVC1 = x as? VC1 {
println("\(x) is supposed to be \(VC1.self)")
//"<__lldb_expr_155.VC1: 0x7fc12a9012f0> is supposed to be __lldb_expr_155.VC1"
}
if x is VC2 {
println("\(x) is supposed to be \(VC2.self)")
//"<__lldb_expr_155.VC2: 0x7fc12a900fd0> is supposed to be __lldb_expr_155.VC2"
}
}
This seems to be caused by the generic constraint, and I believe is a bug (http://www.openradar.me/22218124). Removing the generic constraint, or making it a non-ObjC class (such as AnyObject) seems to fix it:
public func viewControllerInfo<T>(ofType: T.Type) -> (viewController: T,index: Int)? {
You can also replace:
if maybeVCTypeT is T {
with:
if maybeVCTypeT.isKindOfClass(T) {
And that seems to work.