Override old object in array swift ios - ios

I have an array of custom objects. For simplicity, lets say each object has 3 properties : id, timestamp and text. I am now obtaining a JSON response from my server which contains updated text for my objects in the array. I need to do the following :
Obtain the object with the given id from my array.
Set the text of the object to the updated text WITHOUT changing any of its other properties.
Override the old object with the new and updated one.
I have found the following extension which allows me to find the object in my array.
extension CollectionType {
func find(#noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
return try indexOf(predicate).map({self[$0]})
}
}
I am then using the following logic to obtain the item from the array.
let my_object = questionImageObjects.find({$0.id== myId})
Now I set the text using my_object.text = currText.
The last step is to override the old object in the array with the updated one. This is where I am stuck.

The extension find will return a copy of the original struct. Whatever you do to it, the original won't be affected. You can modify it through the index:
struct DataModel: CustomStringConvertible {
var id: Int
var timestamp: NSDate
var text: String
init(id: Int, timestamp: NSDate, text: String) {
self.id = id
self.timestamp = timestamp
self.text = text
}
// For debug purposes
var description: String {
get { return "( id = \(id), timestamp = \(timestamp), text = \(text) )" }
}
}
// Initial data
var arr = [
DataModel(id: 1, timestamp: NSDate(), text: "Anakin Skywalker"),
DataModel(id: 2, timestamp: NSDate(), text: "Luke Skywalker")
]
// Now the server returns a JSON that says Anakin has turned to the Dark Side
let jsonString = "{ \"id\": 1, \"text\": \"Darth Vader\" }"
let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
let json = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
// Update the text
if let dict = json as? [String: AnyObject],
let id = dict["id"] as? Int,
let newText = dict["text"] as? String,
let index = (arr.indexOf { $0.id == id }) {
arr[index].text = newText
}
print(arr)
If those if lets confuse you, here's a step-by-step guide:
if let dict checks that the JSON can be converted into a Dictionary
let id checks that it has an id key of type Int
let newText checks that it has a text key of type String
let index checks that the array contains an element with that id

Related

Firebase print snapshot works fine and fetches the dictionary but when tried to access individual field data, returns nil for certain fields

I have a problem accessing values from certain fields in my firebase database. Right now this is how my structure looks in firebase:
messages:
messageId:
fromId:
text:
timestamp:
toId:
I am able to successfully upload the data to firebase when a user inputs a message to another user. And I am also able to successfully print the snapshot. But when I set the dictionary values and access it, only "fromId" and "toId" works but "timestamp" and "text" returns a nil value.
Pretty sure there is some sort of a wrong implementation in terms of taking the snapshot values and setting it. For your reference, I have included 3 files, one where the data model is defined, one where I upload data to firebase and one where I am trying to print it but I get nil.
The file where I am trying to print data but I get nil. Note: I am only getting nil when I am trying to print "text" and "timestamp" field values. "fromId" and "toId" works.
import UIKit
import Firebase
class MessagesController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
observeMessages()
}
var messages = [Message]()
func observeMessages(){
let ref = Database.database().reference().child("messages")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as?
Dictionary<String, AnyObject>{
let message = Message(dictionary: dictionary)
print(message.text)
print(message.fromId)
print(message.toId)
print(timestamp)
}
})
}
}
This is how I am uploading the data to firebase using a handle send function once the user has entered some text in the text box
#objc func handleSend(){
let ref = Database.database().reference().child("messages")
let childRef = ref.childByAutoId()
let toId = user!.uid!
let fromId = Auth.auth().currentUser!.uid
let timestamp: Int = Int(NSDate().timeIntervalSince1970)
let values = ["fromId": fromId, "text":
inputTextField.text!, "timestamp": timestamp, "toId": toId] as
[String : Any]
childRef.updateChildValues(values)
}
Finally this is how I have declared my messages class:
class Message{
var fromId: String!
var text: String!
var timestamp: Int!
var toId: String!
init(dictionary: Dictionary<String, AnyObject>) {
if let text = dictionary["messageText"] as? String {
self.text = text
}
if let fromId = dictionary["fromId"] as? String {
self.fromId = fromId
}
if let toId = dictionary["toId"] as? String {
self.toId = toId
}
if let timestamp = dictionary["creationDate"] as? Int {
self.timestamp = timestamp
}
}
}
When I print message.fromID, I get the data results in the console but when I print message.text or message.timestamp I get
nil
nil
nil
So in summary snapshot works, fromId, toID fields also work but for some reason the data from the text and timestamp fields are returned as nil
your are accessing values from dictionary with invalid key use text instead of messageText and use timeSamp instead of creationDate. like below
class Message{
var fromId: String!
var text: String!
var timestamp: Int!
var toId: String!
init(dictionary: Dictionary<String, AnyObject>) {
if let text = dictionary["text"] as? String {
self.text = text
}
if let fromId = dictionary["fromId"] as? String {
self.fromId = fromId
}
if let toId = dictionary["toId"] as? String {
self.toId = toId
}
if let timestamp = dictionary["timestamp"] as? Int {
self.timestamp = timestamp
}
}
}

How do I retrieve specific values from a firebase database and assign it to specific variables in Swift 4?

I have a database in firebase which looks as follows:
I need to get the values of nameID, tutorID, and imageURL and assign them to variables in Swift 4. Here is what I have so far in XCode:
let ref = Database.database().reference().child("students").child("student1")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject] else {
return
}
print (dictionary)
let Obj = studentInformation(nameID: " ", tutorID: " ", imageURL: " ")
Obj.imageURL = dictionary["photoID"] as? String
Obj.nameID = dictionary["nameID"] as? String
Obj.tutorID = dictionary["tutorID"] as? String
self.studentInfo.append(Obj)
}, withCancel: nil)
For the studentInformation class, I have declared it as such:
class studentInformation {
var nameID: String?
var tutorID: String?
var imageURL: String?
init(nameID: String?, tutorID: String?, imageURL: String?) {
self.nameID = nameID
self.tutorID = tutorID
self.imageURL = imageURL
}
}
I can't seem to get it to work correctly, as it's able to get the values from the database, but it is not able to assign it to the local variables I have in XCode. Any help would be appreciated. Thanks in advance
Create an optional initializer for in the Object and determine which variables should be optional (ex: only the imageURL is optional in the example below, and the nameID and tutorID have to be Strings otherwise the init will return nil):
init?(dictionary: [String : Any]) {
guard let nameId = dictionary["nameID"] as? String,
let tutorID = dictionary["tutorID"] as? String else { return nil }
let imageURL = dictionary["imageURL"] as? String
self.init(nameID: nameID, tutorID: tutorID, imageURL: imageURL)
}
Then, in the Firebase listener you can create the object like this:
// Returns Optional(obj)
let obj = studentInformation(dictionary: dictionary)
or
// Add object to array
if let obj = studentInformation(dictionary: dictionary) { self.studentInfo.append(obj) }

Swift 4 Key Value Dictionary stored in User Defaults

I am new to Swift and have a requirement to store a database of key value pairs. The key value pairs are a name with a corresponding 4 digit number in database that remains in memory after the app is excited. I am thinking to use a dictionary with the name as the key and the 4 digit numbers as the value. These are then stored in the iPad flash memory using the user defaults class.
Below is the code that I’ve currently developed. The code that adds to the database compiles ok but the code that checks the name and number for a match in the database won't compile due to the following message (Value of optional type '[Any]?' not unwrapped; did you mean to use '!' or '?'?) which is because of this line of code (if let databaseCheck = database[name]). Ive obviously tried unwrapping but can't seem to shake the error message.
Anyone got any ideas whats causing the error or any issues with the approach?
public func checkDatabaseMatch( _ name: String, _ number: String) -> Bool
{
var foundInDatabaseFlag: Bool = false
let database = UserDefaults.standard.array(forKey: "Database")
if let databaseCheck = database[name]
{
if (databaseCheck == number)
{
foundInDatabaseFlag = true
}
}
return foundInDatabaseFlag
}
public func saveToDatabase( _ name: String, _ number: String)
{
var newEntry: [String: String] = [:]
newEntry[name] = number
UserDefaults.standard.set(newEntry, forKey: "Database")
}
There is a major mistake. You save a dictionary but retrieve an array.
Apart from that a dictionary retrieved from UserDefaults is [String:Any] by default, you have to conditional downcast the object.
The code checks if there is a dictionary in UserDefaults and if there is the requested key in one expression
public func checkDatabaseMatch( _ name: String, _ number: String) -> Bool
{
guard let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String],
let databaseCheck = database[name] else { return false }
return databaseCheck == number
}
Another mistake is that you are always overwriting the entire dictionary in UserDefaults. If you want to save multiple key-value pairs you have to read the dictionary first.
public func saveToDatabase( _ name: String, _ number: String)
{
var newEntry : [String: String]
if let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String] {
newEntry = database
} else {
newEntry = [:]
}
newEntry[name] = number
UserDefaults.standard.set(newEntry, forKey: "Database")
}
Side note: The parameter labels are highly recommended in Swift for better readability.

Array appending overwrites last index of Realm object

I have an array of Realm objects and before i save them in Realm DB i have my own array of objects in for loop:
var objs = [self.friendsObject] //0 values at first
for i in (0..<json.count) { //counts 2
let _id = json[i]["_id"] as? String
let userName = json[i]["userName"] as? String
let profile_pic = json[i]["profile_pic"] as? String
let phone = json[i]["phone"] as? String
self.friendsObject.id = _id!
self.friendsObject.username = userName!
self.friendsObject.profilepic = profile_pic!
self.friendsObject.phone = phone!
objs.append(self.friendsObject) //2nd element overwrites 1st one
}
self.friendsObject.save(objects: objs)
So i can see the first object with correct items inside objs before i insert second array, but in second index there are 2 array of objects with same values. i appreciate any help.
Note: It is not duplicate, i have already checked some similar questions but it doesn't apply to my issue.
As Vadian commented, the problem is that the code is not creating new instances of friendsObject but appending the same instance with different values.
Edit
Below an example of how to copy JSON to a class based on the information provided in the question:
// Simulating a JSON structure filled with some data.
var jsonData = [Int: [String: String]]()
for index in 0..<10 {
var values = [String: String]()
values["id"] = "id\(index)"
values["username"] = "username\(index)"
values["profilepic"] = "profilepic\(index)"
values["phone"] = "phone\(index)"
jsonData[index] = values
}
// Friend sample class where JSON data will be copied to.
class Friend {
var id: String
var username: String
var profilepic: String
var phone: String
init(_ id: String, _ username: String, _ profilepic: String, _ phone: String) {
self.id = id
self.username = username
self.profilepic = profilepic
self.phone = phone
}
}
// The array where to copy the values from the JSON data.
var friends = [Friend]()
// Looping through the JSON data with a sorted key.
for jsonSortedKey in jsonData.keys.sorted(by: <) {
// Obtaining a JSON element containing friend data.
let jsonFriend = jsonData[jsonSortedKey]!
// Creating a new friend's instance from the JSON friend's data.
let friend = Friend((jsonFriend["id"]!), jsonFriend["username"]!, (jsonFriend["profilepic"]!), (jsonFriend["phone"]!))
friends.append(friend)
}
The result is this:

Swift 3: sum value with group by of an array of objects

I have this code in my viewController
var myArray :Array<Data> = Array<Data>()
for i in 0..<mov.count {
myArray.append(Data(...))
}
class Data {
var value :CGFloat
var name :String=""
init({...})
}
My input of Data is as:
10.5 apple
20.0 lemon
15.2 apple
45
Once I loop through, I would like return a new array as:
sum(value) group by name
delete last row because no have name
ordered by value
Expected result based on input:
25.7 apple
20.0 lemon
and nothing else
I wrote many rows of code and it is too confused to post it. I'd find easier way, anyone has a idea about this?
First of all Data is reserved in Swift 3, the example uses a struct named Item.
struct Item {
let value : Float
let name : String
}
Create the data array with your given values
let dataArray = [Item(value:10.5, name:"apple"),
Item(value:20.0, name:"lemon"),
Item(value:15.2, name:"apple"),
Item(value:45, name:"")]
and an array for the result:
var resultArray = [Item]()
Now filter all names which are not empty and make a Set - each name occurs one once in the set:
let allKeys = Set<String>(dataArray.filter({!$0.name.isEmpty}).map{$0.name})
Iterate thru the keys, filter all items in dataArray with the same name, sum up the values and create a new Item with the total value:
for key in allKeys {
let sum = dataArray.filter({$0.name == key}).map({$0.value}).reduce(0, +)
resultArray.append(Item(value:sum, name:key))
}
Finally sort the result array by value desscending:
resultArray.sorted(by: {$0.value < $1.value})
---
Edit:
Introduced in Swift 4 there is a more efficient API to group arrays by a predicate, Dictionary(grouping:by:
var grouped = Dictionary(grouping: dataArray, by:{$0.name})
grouped.removeValue(forKey: "") // remove the items with the empty name
resultArray = grouped.keys.map { (key) -> Item in
let value = grouped[key]!
return Item(value: value.map{$0.value}.reduce(0.0, +), name: key)
}.sorted{$0.value < $1.value}
print(resultArray)
First of all, you should not name your class Data, since that's the name of a Foundation class. I've used a struct called MyData instead:
struct MyData {
let value: CGFloat
let name: String
}
let myArray: [MyData] = [MyData(value: 10.5, name: "apple"),
MyData(value: 20.0, name: "lemon"),
MyData(value: 15.2, name: "apple"),
MyData(value: 45, name: "")]
You can use a dictionary to add up the values associated with each name:
var myDictionary = [String: CGFloat]()
for dataItem in myArray {
if dataItem.name.isEmpty {
// ignore entries with empty names
continue
} else if let currentValue = myDictionary[dataItem.name] {
// we have seen this name before, add to its value
myDictionary[dataItem.name] = currentValue + dataItem.value
} else {
// we haven't seen this name, add it to the dictionary
myDictionary[dataItem.name] = dataItem.value
}
}
Then you can convert the dictionary back into an array of MyData objects, sort them and print them:
// turn the dictionary back into an array
var resultArray = myDictionary.map { MyData(value: $1, name: $0) }
// sort the array by value
resultArray.sort { $0.value < $1.value }
// print the sorted array
for dataItem in resultArray {
print("\(dataItem.value) \(dataItem.name)")
}
First change your data class, make string an optional and it becomes a bit easier to handle. So now if there is no name, it's nil. You can keep it as "" if you need to though with some slight changes below.:
class Thing {
let name: String?
let value: Double
init(name: String?, value: Double){
self.name = name
self.value = value
}
static func + (lhs: Thing, rhs: Thing) -> Thing? {
if rhs.name != lhs.name {
return nil
} else {
return Thing(name: lhs.name, value: lhs.value + rhs.value)
}
}
}
I gave it an operator so they can be added easily. It returns an optional so be careful when using it.
Then lets make a handy extension for arrays full of Things:
extension Array where Element: Thing {
func grouped() -> [Thing] {
var things = [String: Thing]()
for i in self {
if let name = i.name {
things[name] = (things[name] ?? Thing(name: name, value: 0)) + i
}
}
return things.map{$0.1}.sorted{$0.value > $1.value}
}
}
Give it a quick test:
let t1 = Thing(name: "a", value: 1)
let t2 = Thing(name: "b", value: 2)
let t3 = Thing(name: "a", value: 1)
let t4 = Thing(name: "c", value: 3)
let t5 = Thing(name: "b", value: 2)
let t6 = Thing(name: nil, value: 10)
let bb = [t1,t2,t3,t4,t5,t6]
let c = bb.grouped()
// ("b",4), ("c",3) , ("a",2)
Edit: added an example with nil for name, which is filtered out by the if let in the grouped() function

Resources