Swift - can not get value of dictionary class extend from class - swift

Simple problem, but i don't know why!!
Create an class - A, and class B extend from B.
Create a collection
public var hm : [B : Int] = [:]
Class A - defined:
class A : Hashable {
var x : Double = 0
var y : Double = 0
init(x : Double, y : Double) {
self.x = x
self.y = y
}
var hashValue : Int {
get {
// IMPORTANT!!! Do some operations to generate a unique hash.
return ObjectIdentifier(self).hashValue
}
}
static func == (lhs: A, rhs: A) -> Bool {
//return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
return (lhs.x == rhs.x && lhs.y == rhs.y)
}
}
class B : A {
}
A function check to get value of collection - hm.
func check(a1 : B, a2 : B){
print("\na1: \(a1.x) \(a1.y)")
if let y1 = hm[a1] {
print(y1)
}else{
print("false a1")
}
print("\na2: \(a2.x) \(a2.y)")
if let y2 = hm[a2] {
print(y2)
}else{
print("false a2")
}
}
and, finally create a function test() is main for testing.
func test() -> Void {
let a1 : B = B(x: 9.0, y: 12.0)
let a2 : B = B(x: 19.0, y: 42.0)
hm[a1] = 99
hm[a2] = 20
let a3 : B = B(x: 9.0, y: 12.0)
let a4 : B = B(x: 19.0, y: 42.0)
print("----Content of hm-----")
for (key, val) in hm {
print("x: \(key.x) | y: \(key.y) | val: = \(val)")
}
check(a1: a3, a2: a4)
}
On main thread call test().
Output is:
----Content of hm-----
x: 19.0 | y: 42.0 | val: = 20
x: 9.0 | y: 12.0 | val: = 99
a1: 9.0 12.0
99
a2: 19.0 42.0
false a2
Why, print "false a2" -> not found a2 inside of collection hm?
For compare 2 objects i use: (lhs.x == rhs.x && lhs.y == rhs.y).
(if use ObjectIdentifier then result always false for compare)
Thank you for explaining.

I don't know why it behaves like that exactly, but I can tell what the issue is: you're implementing Hashable wrong.
One important condition of conforming to Hashable is that "equal objects have the same hashValue", which is not true in your case. Once you break this fundamental assumption, all code that depends on it will likely break (like Dictionary).
In this example you should either make A work on identity (in this case == would be the same as ===), or work on values (in this case hashValue would depend only on x and y).
The important thing to note here is that Hashable doesn't just mean "it's a value that can produce a hash of itself", but it's actually a subprotocol of Equatable. As such, it really means "An object that can be checked for equality, and can also do a preliminary equality check with a hash value that it can produce".

Delete your implementation of hashValue and replace insert this instead:
func hash(into hasher: inout Hasher) {
x.hash(into:&hasher)
y.hash(into:&hasher)
}

Related

Synchronising combined Properties in ReactiveSwift

I'm considering converting a project using my own custom signal framework to use ReactiveSwift instead, but there is a fundamental issue I've never figured out how to resolve in ReactiveSwift:
As a simplified example, let's say you have two mutable properties:
let a = MutableProperty<Int>(1)
let b = MutableProperty<Int>(2)
Then, we derive a property that combines both to implement our logic:
let c = Property.combineLatest(a, b).map { a, b in
return a + b
}
Later, we receive some information that causes us to update the values of both a and b at the same time:
a.value = 3
b.value = 4
The problem now is that c will inform its listeners that it has the values 3 -> 5 -> 7. The 5 is entirely spurious and does not represent a valid state, as we never wanted a state where a was equal to 3 and b was equal to 2.
Is there a way around this? A way to suppress updates to a Property while updating all of its dependencies to new states, and only letting an update through once you are done?
combineLatest‘s fundamental purpose is to send a value when either of its upstream inputs send a new value, so I don’t think there’s a way to avoid this issue if you want to use that operator.
If it’s important that both values update truly simultaneously then consider using a MutableProperty<(Int, Int)> or putting the two values in a struct. If you give a little more context about what you’re actually trying to accomplish then maybe we could give a better answer.
Pausing Updates
So I really don't recommend doing something like this, but if you want a general purpose technique for "pausing" updates then you can do it with a global variable indicating whether updates are paused and the filter operator:
let a = MutableProperty<Int>(1)
let b = MutableProperty<Int>(2)
var pauseUpdates = false
let c = Property.combineLatest(a, b)
.filter(initial: (0, 0)) { _ in !pauseUpdates }
.map { a, b in
return a + b
}
func update(newA: Int, newB: Int) {
pauseUpdates = true
a.value = newA
pauseUpdates = false
b.value = newB
}
c.producer.startWithValues { c in print(c) }
update(newA: 3, newB: 4)
But there are probably better context-specific solutions for achieving whatever you are trying to achieve.
Using a sampler to manually trigger updates
An alternate solution is to use the sample operator to manually choose when to take a value:
class MyClass {
let a = MutableProperty<Int>(1)
let b = MutableProperty<Int>(2)
let c: Property<Int>
private let sampler: Signal<Void, Never>.Observer
init() {
let (signal, input) = Signal<Void, Never>.pipe()
sampler = input
let updates = Property.combineLatest(a, b)
.map { a, b in
return a + b
}
.producer
.sample(with: signal)
.map { $0.0 }
c = Property(initial: a.value + b.value, then: updates)
}
func update(a: Int, b: Int) {
self.a.value = a
self.b.value = b
sampler.send(value: ())
}
}
let x = MyClass()
x.c.producer.startWithValues { c in print(c) }
x.update(a: 3, b: 4)
Using zip
If a and b are always going to change together, you can use the zip operator which waits for both inputs to have new values:
let a = MutableProperty<Int>(1)
let b = MutableProperty<Int>(2)
let c = Property.zip(a, b).map(+)
c.producer.startWithValues { c in print(c) }
a.value = 3
b.value = 4
Use zip with methods for each type of update
class MyClass {
let a = MutableProperty<Int>(1)
let b = MutableProperty<Int>(2)
let c: Property<Int>
init() {
c = Property.zip(a, b).map(+)
}
func update(a: Int, b: Int) {
self.a.value = a
self.b.value = b
}
func update(a: Int) {
self.a.value = a
self.b.value = self.b.value
}
func update(b: Int) {
self.a.value = self.a.value
self.b.value = b
}
}
let x = MyClass()
x.c.producer.startWithValues { c in print(c) }
x.update(a: 5)
x.update(b: 7)
x.update(a: 8, b: 8)
Combining the values into one struct
I thought I would provide an example of this even though you said you didn't want to do it, because MutableProperty has a modify method that makes it less cumbersome than you might think to do atomic updates:
struct Values {
var a: Int
var b: Int
}
let ab = MutableProperty(Values(a: 1, b: 2))
let c = ab.map { $0.a + $0.b }
c.producer.startWithValues { c in print(c) }
ab.modify { values in
values.a = 3
values.b = 4
}
And you could even have convenience properties for directly accessing a and b even as the ab property is the source of truth:
let a = ab.map(\.a)
let b = ab.map(\.b)
Creating a new type of mutable property to wrap the composite property
You could create a new class conforming to MutablePropertyProtocol to make it more ergonomic to use a struct to hold your values:
class MutablePropertyWrapper<T, U>: MutablePropertyProtocol {
typealias Value = U
var value: U {
get { property.value[keyPath: keyPath] }
set {
property.modify { val in
var newVal = val
newVal[keyPath: self.keyPath] = newValue
val = newVal
}
}
}
var lifetime: Lifetime {
property.lifetime
}
var producer: SignalProducer<U, Never> {
property.map(keyPath).producer
}
var signal: Signal<U, Never> {
property.map(keyPath).signal
}
private let property: MutableProperty<T>
private let keyPath: WritableKeyPath<T, U>
init(_ property: MutableProperty<T>, keyPath: WritableKeyPath<T, U>) {
self.property = property
self.keyPath = keyPath
}
}
With this, you can create mutable versions of a and b that make it nice and easy to both get and set values:
struct Values {
var a: Int
var b: Int
}
let ab = MutableProperty(Values(a: 1, b: 2))
let a = MutablePropertyWrapper(ab, keyPath: \.a)
let b = MutablePropertyWrapper(ab, keyPath: \.b)
let c = ab.map { $0.a + $0.b }
c.producer.startWithValues { c in print(c) }
// Update the values individually, triggering two updates
a.value = 10
b.value = 20
// Update both values atomically, triggering a single update
ab.modify { values in
values.a = 30
values.b = 40
}
If you have the Xcode 11 Beta installed, you can even use the new key path based #dynamicMemberLookup feature to make this more ergonomic:
#dynamicMemberLookup
protocol MemberAccessingProperty: MutablePropertyProtocol {
subscript<U>(dynamicMember keyPath: WritableKeyPath<Value, U>) -> MutablePropertyWrapper<Value, U> { get }
}
extension MutableProperty: MemberAccessingProperty {
subscript<U>(dynamicMember keyPath: WritableKeyPath<Value, U>) -> MutablePropertyWrapper<Value, U> {
return MutablePropertyWrapper(self, keyPath: keyPath)
}
}
Now instead of:
let a = MutablePropertyWrapper(ab, keyPath: \.a)
let b = MutablePropertyWrapper(ab, keyPath: \.b)
You can write:
let a = ab.a
let b = ab.b
Or just set the values directly without creating separate variables:
ab.a.value = 10
ab.b.value = 20

Swift - declare 2d array contains

I want to say that if some 2d array contains the "point" format [Int,Int], then regenerate the random numbers, not counting the iteration.
for _ in 0..<33{
let j = Int(arc4random_uniform(10))
let k = Int(arc4random_uniform(10))
while live.contains(//The point j,k){
live.append(Array(arrayLiteral: j,k))
cells[j][k] = true
}
}
From what I understood your question, you want to generate an array of 2D points excluding repetition, you can use CGPoint or define your own Point
struct Point: Equatable {
let x: Int
let y: Int
}
func == (lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
var live: [Point] = []
for _ in 0..<10{
var candidate = Point(x: Int(arc4random_uniform(10)), y: Int(arc4random_uniform(10)))
while live.contains(candidate) {
candidate = Point(x: Int(arc4random_uniform(10)), y: Int(arc4random_uniform(10)))
}
live.append(candidate)
}
or you can use tuple like so
var live: [(Int, Int)] = []
for _ in 0..<10{
var j = Int(arc4random_uniform(10))
var k = Int(arc4random_uniform(10))
while live.contains({$0 == (j, k)}) {
j = Int(arc4random_uniform(10))
k = Int(arc4random_uniform(10))
}
live.append((j,k))
}
Depending on your problem size, it might be more optimal to build an array of all possible values, and then shuffle and take first X elements every time you need new set of random points. You can optimize it further, but the code'd look similar to:
var possibleValues: [Point] = []
for x in 0..<5 {
for y in 0..<5 {
possibleValues.append(Point(x: x, y: y))
}
}
func randomPoints(numberOfPoints: Int) -> [Point] {
// shuffle original array without changing it
let shuffled = possibleValues.sorted { _ in arc4random_uniform(10) > 5 }
// take first X elements
return Array(shuffled[0..<numberOfPoints])
}
randomPoints(numberOfPoints: 10)
You can optimize this solution even further but that'd require to know more about your data set. Hope this helps

Compare 2 structs/Objects implement the same Protocol?

let's say I have the following :
protocol P : Equatable {
var uniqueID : Int { get }
}
struct A : P {
var uniqueID = 1
}
struct B : P {
var uniqueID = 2
}
func ==<T : P>(lhs:T , rhs:T) -> Bool { return lhs.uniqueID == rhs.uniqueID }
Now when I write the following:
let a = A()
let b = B()
let c = a == b
I got error: binary operator '==' cannot be applied to operands of type 'A' and 'B'
is there any way to achieve this ?
you have to define the equality function with two generic types to allow different types to be compared, like this:
func ==<T: P, T2: P>(lhs: T , rhs: T2) -> Bool { return lhs.uniqueID == rhs.uniqueID }

Swift - Struct with equatable variable

I'm trying to implement a struct which is Equatable and has a variable (in this example 'variable2') of type AnyObject that might or not be equatable.
struct MyStruct : Equatable{
var variable1 : String;
var variable2 : AnyObject;
}
func == (obj1 : MyStruct, obj2 : MyStruct) -> Bool {
if(obj1.variable2.conformsToProtocol(Equatable) && obj2.variable2.conformsToProtocol(Equatable)) {
//...
} else {
//...
}
return ...
}
At first I was trying to check if variable2 conforms to protocol Equatable, but doing so i get a compile error.
On a different approach I tried to change 'variable2' to Equatablebut even so I still have an error telling me it can only be used as a generic constraint.
struct MyStruct : Equatable{
var variable1 : String;
var variable2 : Equatable;
}
func == (obj1 : MyStruct, obj2 : MyStruct) -> Bool {
return obj1.variable2 == obj2.variable2;
}
I tried some different ways, but didn't manage to get it to work. Does anyone has a solution for this?Solving the first case would be the best situation, but the second might satisfy my needs as well.
Why not use the "is" operator:
if obj1.variable2 is Equatable && obj2.variable2 is Equatable {
...
}
(a) AnyObject doesn't conforms to Equatable
(b) If you downcast some class which conforms to Equatable to AnyObject,
the result is NOT equatable!!
class C: Equatable {
var i: Int
init(value: Int){
i = value
}
}
func ==(lhs: C, rhs: C)->Bool {
return 2 * lhs.i == rhs.i
}
let c1 = C(value: 1)
let c2 = C(value: 2)
c1 == c2 // true
let a1 = c1 as AnyObject
let a2 = c2 as AnyObject
// or if you want
// let a1: AnyObject = c1
// let a2: AnyObject = c2
// a1 == a2 // error!!!
a1 as? C == a2 as? C // true
In other words, you are not able to compare two AnyObject. You can check if two references to AnyObject are the same (representing the same instance). But that has nothing with Equatable protocol.

Is it possible to add function definitions programmatically in swift

I'm creating a struct that holds 3 float values,
struct Col {
var r: Float
var g: Float
var b: Float
}
and I'd like to add a bunch of function definitions that are equivalent to the built in math functions, but that operate piecewise on the members of my struct
I can do it by hand, eg
func pow(a: Col, b: Col) -> Col {
return Col(r: pow(a.r, b.r), g: pow(a.g, b.g), b: pow(a.b, b.b))
}
but this is tedious and error prone.
What I'd like to do is create a function to turn the original math function into my Col version, so that I could call it like this:
defineColVersion(pow, noArgs: 2)
and it defines the new version, without overwriting the built in function that operates on Doubles
Is there any way to do this in Swift?
Thanks
I actually think this is exactly what you want:
func toCol(f: (Float, Float) -> Float) -> (Col, Col) -> Col {
return { a, b in
Col(r: f(a.r, b.r), g: f(a.g, b.g), b: f(a.b, b.b))
}
}
func toCol(f: Float -> Float) -> Col -> Col {
return { c in
Col(r: f(c.r), g: f(c.g), b: f(c.b))
}
}
let pow = toCol(Darwin.pow)
let sin = toCol(Darwin.sin)
let log = toCol(Darwin.log)
let a = Col(r: 0.4, g: 0.2, b: 0.7)
let b = Col(r: 0.3, g: 0.9, b: 0.3)
pow(a, b)
sin(a)
log(b)
The two overloaded functions toCol take a unary/binary function on Floats and returns a new function which does the same on your Col type. With those two, you can easily create a pow function for your Col type.
It is not possible to programmatically define new functions in a static language like Swift. What you can do, however, is to make a higher-kinded function:
func init(a: Col, b: Col, function: (Float, Float) -> Float) -> Col {
return self.init(r: function(a.r, b.r), g: function(a.g, b.g), b: function(a.b, b.b))
}
Col(Col(1, 2, 3), Col(3, 4, 5)) { $0 * $1 }
Col(Col(1, 2, 3), Col(3, 4, 5)) { pow($0, $1) }
func init(first: Col, second: Col, function: (Float, Float) -> Float ) {
newR = function(first.r,second.r)
newG = function(first.g,second.g)
newB = function(first.b,second.b)
return self.init(r:newR,g:newG,b:newB)
}
I'm not in a position to compile this so it probably has some errors but hopefully it will be useful. You would use it like so:
first = Col(r:1,g:2,b:3)
second = Col(r:1,g:2,b:3)
combined = Col(first:first,second:second) { pow($0,$1) }

Categories