Create Set of Array in Swift - swift

I was trying to create a set out of an array to make elements unique.
I thought it would be an elegant approach to use the Set initializer to do this job. So I created an array and tried to initialize my new set using that array.
But the compiler was complaining. I tried it using Integer values and it works like a charm. But with my own class it does not work. I also thought that the Equatable Protocol is the right way to go .. but as you see, it does not solve my Problem.
Consider the following Playground:
import UIKit
internal struct Object: Equatable {
var a: Int
}
internal func == (lhs:Object,rhs: Object) -> Bool {
return lhs.a == rhs.a
}
let array = [Object(a:1),Object(a:1),Object(a:1),Object(a:1),Object(a:2)]
let set = Set(array) // ERROR
The Compiler is complaining with
can not invoke initializer Set<_> with argument of type [Object]
Which Protocol do I need to implement to makes things work?

If you 'command-click' on Set in Xcode, it takes you to the definition of the Set type. And there you go:
/// A collection of unique `Element` instances with no defined ordering.
public struct Set<Element : Hashable> : Hashable, CollectionType ...
As mentioned by Rob the elements need to confirm to Hashable (which in turn requires Equatable).
Adjusting your code:
import Foundation
internal struct Object: Hashable {
var a: Int
var hashValue: Int { return a.hash }
}
internal func == (lhs:Object,rhs: Object) -> Bool {
return lhs.a == rhs.a
}
let array = [Object(a:1),Object(a:1),Object(a:1),Object(a:1),Object(a:2)]
let set = Set(array) // SUCCESS

Related

Swift get struct properties from type reference

I'm trying to loop over the struct properties but I only have reference for struct type not fully initialized struct. if I were to have an initialized struct I can use the mirror methods but I only have a type reference from generics.
Here in my code I'm trying to generate other code for some reason 😅 I'll create function arguments from struct properties.
class LoginRequiredRule<T: Request>: Rule {
var validator: (T) -> Void
init(validator: #escaping (T) -> Void) {
self.validator = validator
}
let a = T.Type.self
func generateTree(_ depth: Int, path: String) -> String {
var o = "";
// i need to loop over the properities of T here but i only have access to T.self
return o;
}
}
I tried using the objc foundation functions to access struct but I don't know objc 🙃.
Is this even possible in swift runtime?
I know I can implement a protocol that contains static reference to properties but there has to be a swifty way 😀

Using diff in an array of objects that conform to a protocol

I'm experimenting with using Composition instead of Inheritance and I wanted to use diff on an array of objects that comply with a given protocol.
To do so, I implemented a protocol and made it comply with Equatable:
// Playground - noun: a place where people can play
import XCPlayground
import Foundation
protocol Field:Equatable {
var content: String { get }
}
func ==<T: Field>(lhs: T, rhs: T) -> Bool {
return lhs.content == rhs.content
}
func ==<T: Field, U: Field>(lhs: T, rhs: U) -> Bool {
return lhs.content == rhs.content
}
struct First:Field {
let content:String
}
struct Second:Field {
let content:String
}
let items:[Field] = [First(content: "abc"), Second(content: "cxz")] // đŸ’„ boom
But I've soon discovered that:
error: protocol 'Field' can only be used as a generic constraint because it has Self or associated type requirements
I understand why since Swift is a type-safe language that needs to be able to know the concrete type of these objects at anytime.
After tinkering around, I ended up removing Equatable from the protocol and overloading the == operator:
// Playground - noun: a place where people can play
import XCPlayground
import Foundation
protocol Field {
var content: String { get }
}
func ==(lhs: Field, rhs: Field) -> Bool {
return lhs.content == rhs.content
}
func ==(lhs: [Field], rhs: [Field]) -> Bool {
return (lhs.count == rhs.count) && (zip(lhs, rhs).map(==).reduce(true, { $0 && $1 })) // naive, but let's go with it for the sake of the argument
}
struct First:Field {
let content:String
}
struct Second:Field {
let content:String
}
// Requirement #1: direct object comparison
print(First(content: "abc") == First(content: "abc")) // true
print(First(content: "abc") == Second(content: "abc")) // false
// Requirement #2: being able to diff an array of objects complying with the Field protocol
let array1:[Field] = [First(content: "abc"), Second(content: "abc")]
let array2:[Field] = [Second(content: "abc")]
print(array1 == array2) // false
let outcome = array1.diff(array2) // đŸ’„ boom
error: value of type '[Field]' has no member 'diff'
From here on, I'm a bit lost to be honest. I read some great posts about type erasure but even the provided examples suffered from the same issue (which I assume is the lack of conformance to Equatable).
Am I right? And if so, how can this be done?
UPDATE:
I had to stop this experiment for a while and totally forgot about a dependency, sorry! Diff is a method provided by SwiftLCS, an implementation of the longest common subsequence (LCS) algorithm.
TL;DR:
The Field protocol needs to comply with Equatable but so far I have not been able to do this. I need to be able to create an array of objects that comply to this protocol (see the error in the first code block).
Thanks again
The problem comes from a combination of the meaning of the Equatable protocol and Swift’s support for type overloaded functions.
Let’s take a look at the Equatable protocol:
protocol Equatable
{
static func ==(Self, Self) -> Bool
}
What does this mean? Well it’s important to understand what “equatable” actually means in the context of Swift. “Equatable” is a trait of a structure or class that make it so that any instance of that structure or class can be compared for equality with any other instance of that structure or class. It says nothing about comparing it for equality with an instance of a different class or structure.
Think about it. Int and String are both types that are Equatable. 13 == 13 and "meredith" == "meredith". But does 13 == "meredith"?
The Equatable protocol only cares about when both things to be compared are of the same type. It says nothing about what happens when the two things are of different types. That’s why both arguments in the definition of ==(::) are of type Self.
Let’s look at what happened in your example.
protocol Field:Equatable
{
var content:String { get }
}
func ==<T:Field>(lhs:T, rhs:T) -> Bool
{
return lhs.content == rhs.content
}
func ==<T:Field, U:Field>(lhs:T, rhs:U) -> Bool
{
return lhs.content == rhs.content
}
You provided two overloads for the == operator. But only the first one has to do with Equatable conformance. The second overload is the one that gets applied when you do
First(content: "abc") == Second(content: "abc")
which has nothing to do with the Equatable protocol.
Here’s a point of confusion. Equability across instances of the same type is a lower requirement than equability across instances of different types when we’re talking about individually bound instances of types you want to test for equality. (Since we can assume both things being tested are of the same type.)
However, when we make an array of things that conform to Equatable, this is a higher requirement than making an array of things that can be tested for equality, since what you are saying is that every item in the array can be compared as if they were both of the same type. But since your structs are of different types, you can’t guarantee this, and so the code fails to compile.
Here’s another way to think of it.
Protocols without associated type requirements, and protocols with associated type requirements are really two different animals. Protocols without Self basically look and behave like types. Protocols with Self are traits that types themselves conform to. In essence, they go “up a level”, like a type of type. (Related in concept to metatypes.)
That’s why it makes no sense to write something like this:
let array:[Equatable] = [5, "a", false]
You can write this:
let array:[Int] = [5, 6, 7]
or this:
let array:[String] = ["a", "b", "c"]
or this:
let array:[Bool] = [false, true, false]
Because Int, String, and Bool are types. Equatable isn’t a type, it’s a type of a type.
It would make “sense” to write something like this

let array:[Equatable] = [Int.self, String.self, Bool.self]
though this is really stretching the bounds of type-safe programming and so Swift doesn’t allow this. You’d need a fully flexible metatyping system like Python’s to express an idea like that.
So how do we solve your problem? Well, first of all realize that the only reason it makes sense to apply SwiftLCS on your array is because, at some level, all of your array elements can be reduced to an array of keys that are all of the same Equatable type. In this case, it’s String, since you can get an array keys:[String] by doing [Field](...).map{ $0.content }. Perhaps if we redesigned SwiftLCS, this would make a better interface for it.
However, since we can only compare our array of Fields directly, we need to make sure they can all be upcast to the same type, and the way to do that is with inheritance.
class Field:Equatable
{
let content:String
static func == (lhs:Field, rhs:Field) -> Bool
{
return lhs.content == rhs.content
}
init(_ content:String)
{
self.content = content
}
}
class First:Field
{
init(content:String)
{
super.init(content)
}
}
class Second:Field
{
init(content:String)
{
super.init(content)
}
}
let items:[Field] = [First(content: "abc"), Second(content: "cxz")]
The array then upcasts them all to type Field which is Equatable.
By the way, ironically, the “protocol-oriented” solution to this problem actually still involves inheritance. The SwiftLCS API would provide a protocol like
protocol LCSElement
{
associatedtype Key:Equatable
var key:Key { get }
}
We would specialize it with a superclass
class Field:LCSElement
{
let key:String // <- this is what specializes Key to a concrete type
static func == (lhs:Field, rhs:Field) -> Bool
{
return lhs.key == rhs.key
}
init(_ key:String)
{
self.key = key
}
}
and the library would use it as
func LCS<T: LCSElement>(array:[T])
{
array[0].key == array[1].key
...
}
Protocols and Inheritance are not opposites or substitutes for one another. They complement each other.
I know this is probably now what you want but the only way I know how to make it work is to introduce additional wrapper class:
struct FieldEquatableWrapper: Equatable {
let wrapped: Field
public static func ==(lhs: FieldEquatableWrapper, rhs: FieldEquatableWrapper) -> Bool {
return lhs.wrapped.content == rhs.wrapped.content
}
public static func diff(_ coll: [Field], _ otherCollection: [Field]) -> Diff<Int> {
let w1 = coll.map({ FieldEquatableWrapper(wrapped: $0) })
let w2 = otherCollection.map({ FieldEquatableWrapper(wrapped: $0) })
return w1.diff(w2)
}
}
and then you can do
let outcome = FieldEquatableWrapper.diff(array1, array2)
I don't think you can make Field to conform to Equatable at all as it is designed to be "type-safe" using Self pseudo-class. And this is one reason for the wrapper class. Unfortunately there seems to be one more issue that I don't know how to fix: I can't put this "wrapped" diff into Collection or Array extension and still make it support heterogenous [Field] array without compilation error:
using 'Field' as a concrete type conforming to protocol 'Field' is not supported
If anyone knows a better solution, I'm interested as well.
P.S.
In the question you mention that
print(First(content: "abc") == Second(content: "abc")) // false
but I expect that to be true given the way you defined your == operator

Can not check equality of values of type T: Equatable inside a generic

I'm currently trying out with some basic data structures like LinkedList. I defined a ListNode class of generics values, like this:
class ListNode<T> {
var nodeContent: T
var nextNode: ListNode<T>? = nil
init() {
// details omitted here
}
And then a linked list. I want to implement the contains() method, so I have sth like this:
func contains<T>(_ item: T) -> Bool {
var currNode = self.head
while (currNode != nil) {
if currNode?.nodeContent == item {
return true
}
currNode = currNode?.nextNode
}
return false
}
Then it's giving me error saying that '==' cannot applied to T and T types. I then looked through the language guide and changed ListNode class and LinkedList struct to this:
class ListNode<T: Equatable>{}
struct LinkedList<T: Equatable>{}
But it's not working, so I added 'Equatable' to func itself:
func contains<T: Equatable>(_ item: T) -> Bool
Still fails. I tried pasting the sample function from the language guide inside,
func findIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
No error occurs. May I know why it's like this? I tried searching, but all suggested answers like this doesn't clear my doubts. Thanks in advance!
You just don't need to make the contains method generic (twice). It's inside of your already generic class and knows about T type. It's right to require T: Equatable in the type declaration.
findIndex(of:in:) works as is, because it's not a method, but rather a standalone generic function.

Extension of Set where element is an Array of a specific type

I would like to add an extension to the Set struct, but only if the element of Set is an Array of NSOperations. Is it possible?
I want to write something like this, but it's not a valid code:
extension Set where Element : Array<NSOperation> { }
because type 'Element' constrained to non-protocol type 'Array<NSOperation>. So I wanted to create a protocol and extend Array of NSOperations with this protocol:
protocol ArrayOfOperations { }
extension Array : ArrayOfOperations where Element : NSOperation { }
It doesn't work either because Extension of type 'Array' with constraints cannot have an inheritance clause.
So I'm kinda lost here. Do you have some idea, how to solve this?
Since you cannot make Array<NSOperation> conform to Hashable you'll have to make a small wrapper-struct.
E.g.
struct NSOperationList {
var operations = [NSOperation]()
}
and then build all functionality you need on top of NSOperationList.
So if you want to add support for Set:
extension NSOperationList: Hashable {
var hashValue: Int {
return operations.reduce(0) { $0 ^ $1.hashValue }
}
}
func == (a: NSOperationList, b: NSOperationList) -> Bool {
return a.operations == b.operations
}
import Foundation
let op = NSOperation()
var arr: Array<NSOperation> = []
arr.append(op)
let set = Set(arr)
i don't know what is your trouble, but above snippet compiles without any trouble

Swift AnyObject as? Set<T> crash

I came across the following behaviour while programming in Swift:
If you run this snippet:
import Foundation
class A : Hashable, Equatable {
var hashValue: Int {
get { return ObjectIdentifier(self).hashValue }
}
}
func ==(a:A, b:A) -> Bool {
return a === b
}
typealias T = A
typealias TCollection = Array<T>
var element:TCollection = [T()]
var array:[AnyObject] = [element]
println(array[0] as? TCollection)
it prints:
Optional([SetCrashTest.A])
However if you change TCollection from Array<T> to Set<T>, it crashes with the following error:
Could not cast value of type 'SetCrashTest.A' (0x1001d1530) to 'NSObject' (0x7fff72a9d0f0).
Also, it runs correctly if I change T from A to some other type, like Int, String, CGFloat etc.
What causes this behaviour? And how can one do such optional downcasting without crashing?
Edit:
I experienced this crash when trying to read a Set<T> from an NSNotification's userInfo, which is of type [NSObject:AnyObject]. It was only after this, that I've constructed the above example; so please don't advise to use [TCollection] instead of [AnyObject], as AnyObject is dictated by the NSNotification API.