I have two arrays of objects with different sizes.
First one with old data, second one with updated data from server (included old data with new), data can be mixed. I want to get difference between these arrays.
My class
class Station {
var dateOfIssue: Date
var region: String
var locality: String
var bsName: String
var freqIn: String
var freqOut: String
var networkType: String
var provider: String
var bsUsableName: String
...
}
Arrays I want to compare (example)
var a = [Station]()
var b = [Station]()
for _ in 0...5 {
a.append(Station(someRandomStationValue...)
}
b = a
for _ in 0...7{
b.append(Station(someRandomStationValue...) //array "b" will contain all that array "a" contains and some new values
}
How to compare these arrays comparing all fields between and get a new array with differences (like in java: b.removeAll(a))?
You can make use of Set which provides in-built .subtract() and .subtracting() methods which removes the common entries inside both the Sets
struct Station: Hashable,CustomStringConvertible {
var id: Int
var region: String
var locality: String
var bsName: String
// Just to provide a pretty print statement
var description: String {
return "ID: \(id) | region: \(region) | locality: \(locality) | bsName: \(bsName)"
}
}
var stations1 = Set<Station>()
var stations2 = Set<Station>()
for currentNumber in 0...3 {
stations1.insert(Station(id: currentNumber, region: "abc", locality: "abc", bsName: "abc"))
}
for currentNumber in 0...5 {
stations2.insert(Station(id: currentNumber, region: "abc", locality: "abc", bsName: "abc"))
}
// Caluculating the difference here
print(stations2.subtracting(stations1))
As pointed out by #koropok, a good solution is using Set. The first step is to conform your type to Hashable. For classes, you'd have to implement == and hash(into:) functions, but if you use struct you don't have to do anything else other than declaring the conformance. So:
struct Station: Hashable {
var dateOfIssue: Date
var region: String
...
}
Now you should be able to add Station into a Set. Thus:
var a = Set<Station>()
for _ in 0...5 {
a.insert(Station(...))
}
var b = Set<Station>()
a.forEach { b.insert($0) }
for _ in 0...7 {
b.insert(Station(...))
}
let c = b.subtracting(a)
Set also provides a handy initializer that you can use to turn your Station array into a set:
let s = Set(arrayLiteral: [your, station, objects])
As mentioned in comments by koropok you may use subtract method:
// Added to make original code functional
// Station must conform to Hashable protocol in order to be stored in the Set
struct Station: Hashable {
let id: Int
}
var a = [Station]()
for i in 0...5 {
a.append(Station(id:i))
}
var b = [Station]()
for i in 0...7{
//array "b" will contain all that array "a" contains and some new values
b.append(Station(id:i))
}
var c = Set(b)
// c will contain 6 and 7
c.subtract(a)
Related
Tried to combine or merging two model to one model
1st model = items [ InboxModel]. (My own Inbox)
2nd model = items2 [MoInboxModel] (SDK Inbox)
1st + 2nd -> combinedItems
private var items: [InboxModel] = []
private var items2: [MoInboxModel] = []
private var combinedItems: [combinedInboxModel] = []
struct InboxModel {
let id: String
let title: String
let message: String
let date: Date
}
struct MoInboxModel {
let id: String
let title: String
let message: String
let date: Date
}
struct combinedInboxModel {
let id: String
let title: String
let message: String
let date: Date
}
self?.combinedItems.append(self?.items). //No exact matches in call to instance method 'append
self?.combinedItems.append(contentsOf: self?.items2 ?? []) //No exact matches in call to instance method 'append
Why there is an error while merge it ? How to merge it correctly?
You have three unrelated types - InboxModel, MoInboxModel and combinedInboxModel (Which should be CombinedInboxModel. Even though they all have properties with the same name, they are different types.
There is no append function on an array of combinedInboxModel that accepts an array of InboxModel or MoInboxModel.
You could use map on each of your two input arrays to convert them to an array of CombinedInboxModel which you can then put into combinedItems.
Presumably you are writing this code in a closure, which is why you have a weak self. Best to deal with that first and then process your arrays.
guard let self = self else {
return
}
self.combinedItems = self.items.map { CombinedInboxModel(id:$0.id,title:$0.title,message:$0.message,date:$0.date) }
let items2 = self.items2.map { CombinedInboxModel(id:$0.id,title:$0.title,message:$0.message,date:$0.date) }
self.combinedItems.append(contentsOf:items2)
You haven't shown where items and items2 come from; Is it possible just to fetch them as instances of the same struct to start with?
The fact that you have three structs with the same properties is a bit fishy. I would consider a different design if I were you.
However, if you must go with this approach, you might want to consider starting with a protocol and getting rid of the combinedInboxModel struct.
protocol InboxModelable {
var id: String { get }
var title: String { get }
var message: String { get }
var date: Date { get }
}
Now make your two structs conform to InboxModelable.
struct InboxModel: InboxModelable {
let id: String
let title: String
let message: String
let date: Date
}
struct MoInboxModel: InboxModelable {
let id: String
let title: String
let message: String
let date: Date
}
Since both of your types conform to InboxModelable you can directly store both types in an array of type Array<InboxModelable> without having to map the elements.
class SomeClass {
private var items: [InboxModel] = []
private var items2: [MoInboxModel] = []
private var combinedItems: [InboxModelable] = []
func combineItems() {
doSomething { [weak self] in
guard let self = self else { return }
self.combinedItems.append(contentsOf: self.items)
self.combinedItems.append(contentsOf: self.items2)
}
}
}
I have been happily programming in Swift for a few weeks - really enjoying it, but I have hit a block. I have googled and I cannot find an explanation. I set up a much simpler test case, which didn't have the same problem.
I am getting the error "Type 'SNstorelocation' has no subscript members", and also "Value of type '[SNstorelocation]?' has no member 'append'".
I have read plenty about subscripting, but I don't think that is the problem. I want to have an array of structs, one of the elements is also an array of structs. I have seen it everywhere and my little test case it worked no problem.
So I have concluded (maybe incorrectly!) that somehow I haven't created and array. If I had, it should have an index to access the data and the code should work, but it doesn't.
Here is the little example I created, which I see as the same as my example, but simpler, which works.
struct test1 {
var t1Item1: Int?
var t1Item2: String?
var myArray = test2
}
struct test2 {
var t2Item1: Int?
var t2Item2: Int?
init(v1: Int, v2: Int) {
self.t2Item1 = v1
self.t2Item2 = v2
}
}
and I can do all the normal array things:
var myVar = [test1]()
var newItem = test1()
newItem.t1Item1 = 1
newItem.t1Item2 = "Hi"
myVar.append(newItem)
let myCount = myVar[0].myArray.count
Where as my example (just a few elements from the struct removed to keep it simple and short)
struct SNStoreItemGroups: Codable {
var Welland: [SNStoreItem]
var Other: [SNStoreItem]
init() {
self.Welland = []
self.Other = []
}
}
struct SNStoreItem: Codable {
var locations = [SNstorelocation]()
var customerName: String?
var customerPhone: String?
var customerEmail: String?
var notes: String?
}
struct SNstorelocation: Codable {
let longitude: Double
let latitude: Double
let country: String
let user: Int
let timestamp: Double = Date().timeIntervalSinceReferenceDate
init(_ lon: Double, _ lat: Double, _ ISOcountry: String, _ UserID: Int) {
self.longitude = lon
self.latitude = lat
self.country = ISOcountry
self.user = UserID
}
}
var mySNData: SNStoreItem?
var SNStore: SNStoreItemGroups = SNStoreItemGroups()
// Some code to populate the SNStore
if let locIndex = SNStore.Welland[index].locations.index(where: { $0.longitude == MyLongitude }) {
// This then causes the error
// "Type 'SNstorelocation' has no subscript members"
if SNStore.Welland[index].locations[locIndex].timestamp {
}
}
Could someone please explain why this second example has no subscripts and the first one works OK? I just don't understand - especially because of the first let which seems to be OK in finding the index - I am sure I have just done something stupid!
TIA.
Ambiguous error reporting by the Swift compiler…
A timestamp is not a Boolean so you need to compare the timestamp with something.
if let locIndex = mySNStore.welland[index].locations.index(where: { $0.longitude == 0.1234 }) {
if mySNStore.welland[index].locations[locIndex].timestamp == Double(42) {
}
}
You can see the correct error and the problem with:
let location = mySNStore.welland[index].locations[locIndex]
if location.timestamp {
}
You could raise a bug with the Swift team on this.
And as a style point Capitalised variables are horrible to read because they look like class names.
I wrote a model like this as an exercise :
struct Store {
var name : String
var bills : Array<Bill>
var category : Category?
}
struct Bill {
var date : String
var amount : Float
}
struct Category {
var name : String
var tags : Array<String>
}
and when I'm searching if a store already exist to add a bill to it instead of creating a new store, my code doesn't work. It acts like if the result of the search is a copy of the Array element . I would like to have a reference.
var stores : Array <Store> = Array()
for billStatment in billStatements! {
let billParts = billStatment.split(separator: ",")
if billParts.count > 0 {
let bill : Bill = Bill(date:String(billParts[0]), amount: Float(billParts[2])!)
var store : Store = Store(name:String(billParts[1]), bills: [bill], category: nil)
if var alreadyAddedStore = stores.first(where: {$0.name == String(billParts[1])}) {
alreadyAddedStore.bills.append(bill)
print("yeah found it \(alreadyAddedStore)") // the debugger breaks here so I know the "first" method is working. If I print alreadyAddedStore here I have one more element, that's fine.
} else {
stores.append(store)
}
}
}
print("\(stores.count)") // If I break here for a given store that should contains multiple elements, I will see only the first one added in the else statement.
Can anyone tell me what I am doing wrong?
As already noted, you're confusing value (struct) semantics with reference (class) semantics.
One simple fix would be the change stores to a dictionary with the name as your key:
var stores : Dictionary<String, Store> = [:]
and use it like this:
if(stores[store.name] == nil) {
stores[store.name] = store
}
else {
stores[storeName].bills.append(bill)
}
I have 2 Types, first is Car with property feature and it of the second type Feature.
I have a property cars: [Car], when I append new car to it, it returns nil.
I have created a sample in the snippet below:
class Car {
let make: String
var features: Feature?
let id: String
init(make: String, features: Feature?, id: String) {
self.make = make
self.features = features
self.id = id
}
}
class Feature {
let convertible: String
init(convertible: String) {
self.convertible = convertible
}
}
var cars: [Car]?
var feature: Feature?
let featureElement = Feature(convertible: "yes")
feature = featureElement
let car = Car(make: "SomeMaked", features: feature, id: "ID")
cars?.append(car)
print(cars)
My question is: Shouldn't the array increase its count after appening to it? What am I missing?
Please note that this code is just sample, so ignore typos and coding convention.
You haven't initialized your array.
Replace your var cars: [Car]? with var cars: [Car]? = []
I'm trying to delete values of a lot of variables inside an array:
var fbUserID = String()
var fbUserName = String()
var meNickname = String()
var userIDOneSignal = String()
var deleteStrings = [fbUserID, fbUserName, meNickname, userIDOneSignal]
Is it possible to do something in line of this:
for i in deleteStrings {
i.removeAll()//remove all as in remove the values of each variable
}
I've also tried using deleteStrings[i].removeAll()
Due to value semantics you cannot mutate variables (as a pointer) from an array.
Rather than an array use a struct
struct User {
var fbUserID = "12"
var fbUserName = "Foo"
var meNickname = "Baz"
var userIDOneSignal = "123"
mutating func clear()
{
fbUserID = ""
fbUserName = ""
meNickname = ""
userIDOneSignal = ""
}
}
var user = User()
print(user.fbUserID) // "12"
user.clear()
user.fbUserID = ""
print(user.fbUserID) // ""
Simply delete the array elements
deleteStrings.removeAll()
If your class inherits from NSObject, you can use key value coding to access the fields by name:
class User: NSObject {
var fbUserID = String()
var fbUserName = String()
var meNickname = String()
var userIDOneSignal = String()
var deleteStrings = ["fbUserID", "fbUserName", "meNickname", "userIDOneSignal"]
func clearAll() {
deleteStrings.forEach { setValue("", forKey: $0) }
}
}
let user = User()
user.fbUserID = "12345"
user.fbUserName = "Joseph Doe"
user.meNickname = "Joe"
print(user.meNickname) // "Joe"
user.clearAll()
print(user.meNickname) // ""
If you want to delete/remove variable value then use optional otherwise your variable value only gets removed or become non-existence when it goes out of scope.
For Example:
var a: Int? = 5 // it have default value 5
a = nil // Now a have nil which is kind of telling that it contains nothing which is what you want to achieve
Now coming to your question.
Is it possible to do something in line of this:
for i in deleteStrings {
i.removeAll()
}
You are iterating over an array of strings and then for each string, you are trying to remove all the characters. First of all you will get error
error: cannot use mutating member on immutable value: 'i' is a 'let' constant
Even though you will correct is using var it will not achieve what you are trying to do i.e. I want to delete the variables value because still your fbUserID ... all other variables will have copies of the data you initialised with.
Now how to do it?
You can use optional to achieve it.
var fbUserID: String? = String()
var fbUserName: String? = String()
var meNickname: String? = String()
var userIDOneSignal: String? = String()
// To delete you will need to assign them nil
fbUserId = nil
Again, you can't do them over loop because var are of values type and when you add it to the list their copies get added.