Swift - Update an array of NSDictionary with values from another NSDictionary - ios

Basically, I have an NSarray of dictionaries coming as a JSON response only once when view appears. Now, I need to update a particular value of a key in those dictionaries with another set of dictionary coming separately in every few second such that I can update my array of dictionaries continuously with those values and show some realtime set of data on the screen.
Example: This is the array of dictionary I am getting from backend
[
{item_id: 1001, name: "Apple", current_price: "$10"},
{item_id: 1009, name: "Orange", current_price: "$15"},
{item_id: 1004, name: "Mango", current_price: "$5"}
]
Current price is something which varies continuously, whose data I am receiving in NSDictionary format separately.
["1009": "$16", "1004": "$3", "1001": "$11"]
As you can see the new NSDictionary is mapped with the item_id to the current price value.
I need a way out to make sure the current price gets updated from this NSDictionary to the array of dictionary so that I can reuse that array to reload my UITableview and show the updated realtime price.

You can do it like this:
let items = [["item_id": 1001, "name": "Apple", "current_price": "$10"], ["item_id": 1009, "name": "Orange", "current_price": "$15"], ["item_id": 1004, "name": "Mango", "current_price": "$5"]]
let prices = ["1009": "$16", "1004": "$3", "1001": "$11"]
let updatedItems = items.map { itemDict -> [String: Any] in
var updatedItem = itemDict
if let idKey = itemDict["item_id"] as? Int {
prices["\(idKey)"].flatMap { updatedItem["current_price"] = $0 }
}
return updatedItem
}

Related

Taking out array from dictionary in swift 3

Hi I am trying to populate a view using the response obtained from service but not able to fetch the exact value out of the whole response ,
[
["product_id": PRO161519,
"name": clothes,
"brand_name": Levis,
"discountprice": 0,
"images": <__NSArrayM 0x6000002541c0>(
{
image = "HTTP://i.vinove.com/dnn/backend/uploads/954tshirt_PNG5434.png";
}
)
"category": Accessories,
"price": 23.00
]
]
ProductList-Model
import UIKit
import SpeedLog
let KImages = "images"
let KListImage = "image"
struct ProductList{
var images = ""
var itemArray = [String]()
func bindProductListDataToPopulateView(_ response:[[String:Any]])->[ProductList]{
SpeedLog.print("response value as result",response)
for items in response{
print("items values",items)
}
print("item array",itemArray)
return []
}
}
response value as result
[["image":
item Values
["image":
Kindly help me to get the values images here.
You have to use like this :
for product in products {
if let productImages = product["images"], let images = productImages as? NSArray {
for image in images {
if let image = image as? [String: String] {
print(image["image"])
}
}
}
}
More than likely that JSON response you posted will eventually find its way to you in the form of a key-value Dictionary. You then use a "key" from the JSON you posted to extract the key's corresponding "value". In the snippet you posted, the keys would be the values on the left of the colon (e.g. "product_id", "name", etc).
Now, lets say your dictionary of key-values was called "jsonDictionary". You then would extract the values like so:
let productId = jsonDictionary["product_id"]
let name = jsonDictionary["name"]
If, however, you don't have logic to deserialize that raw JSON data (that you posted in your question) into a Dictionary, then you'll have to start there instead.

How do I sort an array of dictionary according to an array contents in Swift

I have an array of dictionary. I need to sort that array. The sorting shouldnt be like ascending or descending but it should be based on an another array contents.
EX: Lets say I have an array nammed array_unsorted and that array contains a lot of dictionary objects like d1, d2, d3, d4 etc. Each of the dictionary object has a key called key1 and each dictionary object has different value for that key such as Kammy, Maddy, Jessy. Lets say I have anohter sorted array which Maddy, Kammy, Jessy. Now the dictionary should be sorted in a way that the first element should the dictionary object in which the value for key1 should beMaddy`.
I cannot use SortDescriptor, because this will sort as an ascending or descending order based on the key passed to it.
I have tried my solution but I am ended up using so many nested loops. I feel like the solution I made so so pathetic that I dont even want to post the code here.
Any help would be so much appreciated.
EDIT: There can be multiple sorting arrays but as of now I am considering only one sorting array and then I can write the code for multiple sorting arrays.
How about this:
Create a new, empty dictionary with a String key, and a value of type Dictionary. Call it sourceItemsDict.
Loop through the dictionaries in your source array, and add each entry to your new dictionary, using your sort key as the dictionary key, and put the array entry as the value.
Create a new, empty array of dictionaries for your sorted results. call it sortedArray.
Now loop through your array that has the desired order in it. Fetch the item with that key from sourceItemsDict and append it to the end of sortedArray.
That should do it, and it should perform in O(n) time.
Try this:
func sort<T: Equatable>(arrayOfDict arr: [[String: T]], by key: String, order: [T]) -> [[String: T]] {
return arr.sorted {
guard let value0 = $0[key], let value1 = $1[key] else {
return false
}
guard let index0 = order.index(of: value0), let index1 = order.index(of: value1) else {
return false
}
return index0 < index1
}
}
let array_unsorted = [
["name": "Kammy", "city": "New York"],
["name": "Maddy", "city": "Cupertino"],
["name": "Jessy", "city": "Mountain View"]
]
let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"])
let sortedByCity = sort(arrayOfDict: array_unsorted, by: "city", order: ["Cupertino", "Mountain View", "New York"])
print(sortedByName)
print(sortedByCity)
Your question leaves a couple of unresolved scenarios:
1: What if the key is missing from a dictionary?
let array_unsorted = [
["name": "Kammy", "city": "New York"],
["city": "Las Vegas"],
["name": "Maddy", "city": "Cupertino"],
["name": "Jessy", "city": "Mountain View"]
]
let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"])
Should Las Vegas appear at the beginning or end of the sorted array?
2: What if you don't specify an order for a value?
let array_unsorted = [
["name": "Amy"],
["name": "Kammy", "city": "New York"],
["name": "Maddy", "city": "Cupertino"],
["name": "Jessy", "city": "Mountain View"]
]
let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"])
Now where should Amy be placed?
Check out this example:
let dic1 = ["name" : "a"]
let dic2 = ["name" : "b"]
let dic3 = ["name" : "c"]
let dic4 = ["name" : "d"]
let dic5 = ["name" : "e"]
let unsorted_array = [dic2,dic5,dic1,dic4,dic3]
func sortArrayByName(_ array:[[String:String]])->[[String:String]]{
var sortedArray:[[String:String]] = [[String:String]]()
var sortingOrder:[String] = [String]()
for eachDic in array{
if let name = eachDic["name"]{
sortingOrder.append(name)
sortingOrder.sort() // sorting logic here
if sortedArray.isEmpty{
sortedArray.append(eachDic)
} else {
let index = sortingOrder.index(of: name)!
sortedArray.insert(eachDic, at: index)
}
}
}
return sortedArray
}
let sorted_array = sortArrayByName(unsorted_array)

iterate JSON, swift

I am working on swift.
The response that I get from the web server is different when i test it on Postman/ browser and mobile emulator.
Postman/browser response :
items: [
{
itemId: 500,
name: "ABC"
},
{
itemId: 500,
name: "ABC"
}
]
Response in iOS :
items: (
{
itemId: 500,
name: "ABC"
},
{
itemId: 500,
name: "ABC"
}
)
I would like to know how to iterate through the array, but since the result is not an array on iOS I am not sure on how should I go about it. Also, I retrieve a result of around 25-30 items in the array. I have a some set of variables too, that are provided in the response after the array.
There is no problem with your response in ios when you print an array it will print with () brackets instead of [] brackets and dictionary will be print with {} braces.
As you say let items : [NSDictionary]! your items array contain dictionary then you can iterate it like this
for item in items {
print(item["itemId"])
print(item["name"])
}
As others have said, your response in iOS is fine.
You can iterate over it simply with:
for item in items {
// do what you want with each item
}
It works fine in a Playground:
let items = [["itemId": 500, "name" : "ABC"], [ "itemId": 500, "name": "ABC"]] as NSArray
for item in items {
print(item as! NSDictionary) // prints each dictionary as expected
}
You have an array of dictionaries. So, you can iterate like-
for itemDict in items {
let itemId = itemDict["itemId"]
println("Item ID: \(itemId)")
let name = itemDict["name"]
println("Item name: \(name)")
}

Create a particular JSON Structure in Swift

I'm having trouble creating a specific structure in JSON with Swift. I use Swifty JSON for parsing but I can't figure out how to create one.
I have this array which is filled by Id's and quantity Ints of products in a shopping basket . I need to get the array into my JSON but I don't know how.
If you could help me with this I would be very glad :)
var productArray = Array<(id: Int,quantity: Int)>()
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": [
for product in productArray
{
"id": product.id
"quantity": product.quantity
}
]
]
You can't just start looping through stuff while defining your dictionary. Here's another approach.
First, create your array:
var productArray = Array<(id: Int,quantity: Int)>()
Add some products (for testing):
productArray += [(123, 1000)]
productArray += [(456, 50)]
Map this array into a new array of dictionaries:
let productDictArray = productArray.map { (product) -> [String : Int] in
[
"id": product.id,
"quantity": product.quantity
]
}
Use the new mapped array in your JSON object:
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": productDictArray
]
You are not supposed to do any kind of looping/condition making block of codes while creating Array's or Dictionary. For that you need to execute that piece of code outside, create a variable and use it.
Do try this way.
var productArray = Array<(id: Int,quantity: Int)>()
var prods = [[String:Int]]()
for product in productArray
{
var eachDict = [String:Int]()
eachDict["id"] = product.id
eachDict["quantity"] = product.quantity
prods.append(eachDict)
}
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": prods
]

Swift shows nil value for Dictionary type

I have a dictionary of type < String, String>. It can access by index values like dictionary[0]. But i want to access each values in this in a simple way by using the key.
var rows: [Dictionary<String, String>] = []
rows = [..] // assigned some value
var value = rows[0]["id"]
println(rows[0]) // ["id": "2", "name": "Bob", "age": "19"]
println(value) // I get nil value
How can i access by this key format. Any suggestions. Thanks in advance.
I tried to read values from a CSV file. And assigned to rows. It works fine when i print rows[0] it shows the correct value. But on the next line if i print rows[0]["id"] it gives me a nil value. And i tried with manual dictionary like
rows = [["name": "alvin"]]
var value = rows[0]["name"]
println(value) // prints alvin
Whats the difference?
This could happen if your key has a space in it. Consider the following:
var rows: [Dictionary<String, String>] = []
rows = [["id ": "2", "name": "Bob", "age": "19"]] // assigned some value
var value = rows[0]["id"]
println(rows[0]) // ["id": "2", "name": "Bob", "age": "19"]
println(value) // I get nil value
To check your keys, print them out like this to see if there is any space in them:
for key in rows[0].keys {
println("XXX\(key)XXX")
}
prints:
XXXid XXX
XXXageXXX
XXXnameXXX
showing that key id is followed by a space, but age and name are not.

Resources