Formatting & Manipulation database data for display in tableview - ios

I am working on an application to display user information stored in a database in an iOS app in a table view. Currently i am logging all information from the server to the console (in the debug area) using
NSLog(#"user information is as follows: %#", JSON);
Which provides output as such:
{ email = “andrew.smith19381223#gmail.com”;
“first_name” = Andrew;
“Last_name” = Smith
id = 12
user_info= (
{
"Room" = "Lab 2";
DayNumber = 1;
lesson = 2;
id = 12;
Instructor = "MR BEERRY";
Group_No = 7;
},
{
"class_room" = "S GEO";
DayNumber = 1;
lesson = 2;
id = 12;
teacher = "Mr RING";
Group_No = 7;
},
...
I would like to use this information in table view columns and as parameters (for eg. the day no.) for the tableview.How should i store/format this information in my iOS application so it can be used for a tableview.I have been googleing for a while without luck :(

Use a JSON parser (e.g. NSJSONSerialization) in order to parse the JSON text and create a representation. The generic representation is a hierarchy of Foundation objects. In your case, the root element is a JSON Object which maps to a NSDictionary in the representation. That dictionary contains the keys #"email", #"first_name", #"Last_name", #"id", #"user_info", etc. You can access the corresponding value for a key with the method -objectForKey: which you should be familiar with.
The corresponding object for key #"user_info" is a NSArray, whose elements are objects of kind NSDictionary.
And so force.
Since the JSON is a dynamic data structure you need some knowledge in the application what that JSON actually shall be and what you expect when you receive that. That is, you may expect a certain structure and keys and certain values. In order to make your live easier, you may use that representation to initialize your "custom model" - which has properties and behavior as you require.
Once you have your model, display it as usual in a UITable view.

Related

Add objects to NSMutableArray that don't reference each other

First question:
Basically I'm attempting to add NSMutableDictionary objects to an NSMutableArray by using a method addItem.
-(void)addItem:(id)ObID name:(id)ObName description:(id)ObDesc menuName:(id)ObmName menuID:(id)ObmID mid:(id)Obmid pod:(id)pod type:(id)obType price:(id)price
{
NSMutableDictionary *itemToAdd = [[NSMutableDictionary alloc]init];
[itemToAdd setValue:ObID forKey:#"id"];
[itemToAdd setValue:ObName forKey:#"name"];
[itemToAdd setValue:ObDesc forKey:#"description"];
[itemToAdd setValue:ObmName forKey:#"mname"];
[itemToAdd setValue:ObmID forKey:#"menu_id"];
[itemToAdd setValue:Obmid forKey:#"mid"];
[itemToAdd setValue:pod forKey:#"POD"];
[itemToAdd setValue:obType forKey:#"Type"];
[itemToAdd setValue:price forKey:#"Price"];
[itemToAdd setValue:#"1" forKey:#"Amount"];
[items addObject:itemToAdd];
}
However when this method is called again later the next object that is added to the overwrites the value of an object with the same keys. Im aware this is because an array simply retains a memory address, hence when the same object simply with a different type "obType" is added all of those values change.
How am I able to add objects to an NSMutableArray without them referencing each other?
Example:
Output after adding one object:
{
Amount = 1;
POD = BOTH;
Type = {
0 = Vegetarian;
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7596;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = "Hungry? Two Large Pizzas!";
}
)
Output after adding another object of same ID however of a different type:
{
Amount = 1;
POD = BOTH;
Type = {
0 = "Plain";
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7596;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = "Two Large Pizzas!";
},
{
Amount = 1;
POD = BOTH;
Type = {
0 = "Plain";
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7555;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = Two Large Pizzas!";
}
)
As can be seen both object types have changed.
Ok so I solved the problem, not 100% sure why.
I was passing in the type object as an NSMutableDictionary which if an item had 2 variations, one object in the dictionary would have a key of 0 the other 1 when passed in like this they overwrite each other.
Passing just the values in as an NSArray fixes this.
Done by passing in:
NSArray * values = [selectedItemOptions allValues];
Thanks all who helped.
I can't give you the answer, there is not enough information provided to do that, but maybe I can help you figure it out yourself. You write:
Im aware this is because an array simply retains a memory address, hence when the same object simply with a different type "obType" is added all of those values change.
That is not what happens in your code, though there are situations where what is in an array may appear to change - and that is what you are probably seeing. The line:
NSMutableDictionary *itemToAdd = [[NSMutableDictionary alloc]init];
generates a new, never before seen, mutable dictionary. You can log the address (effectively the identity(1)) of this object by adding:
NSLog(#"Created itemToAdd: %p", itemToAdd);
after the above line. The %p format will produce the address of the newly created object, and every object has a unique address. When you then get to the line:
[items addObject:itemToAdd];
The new dictionary is added to whichever array is referenced by items - and that is very important. Any variable, such as items and itemToAdd, references an object, it is not the object itself. So the array being referenced by items could change between calls to addItem:. You should check this and you can do that by adding(2):
NSLog(#"addItem called, items = %p containing %ld element", items, items.count);
for(id someThing in items)
NSLog(#" element: %p", someThing);
to the start of your method. This will first tell you the address of the array currently referenced by items and how many items that array contains. The for loop will then output the address of each element of the array (if there are any).
Further note that addObject: will always add an item to the array, it doesn't matter if the same item is already in the array - add it twice and the array appears to have two elements, both of which reference the same item. When the above code displays the count it should be increasing unless you are removing elements from the array somewhere.
Add those statements and look at the results. Among the possibilities that would produce the results you are reporting:
The array referenced by items is changing
The dictionary you add is being removed
Etc.
HTH
(1) Addresses can be reused, after an object is no longer required and destroyed by ARC its memory may be reused for a new object. So the address does not strictly identify a unique object, you just need to be aware of this
(2) I am assuming 64-bit here, if not you need to change the %ld format or add a cast.

Get whole object from NSMutableDictionary for current cellForRowAtIndexPath and assign it to a CustomCell

I have the following type of a NSMutableDictionary
(
{
id = 1;
key = value;
key = value;
},
{
id = 2;
key = value;
key = value;
}
)
It contains multiple data of an own Object. Now, in cellForRowAtIndexPath. I created a CustomCell that has a field CustomCell.customObject that should get this object. What I'm trying to do is the following. I want to assign the current entry of the NSMutableDictionary to this field.
Alternatively I could do this (and am doing it right now)
I'm getting the ID like this
NSString *objectId = [[dict valueForKey:#"id"] objectAtIndex:indexPath.row];
And then I'm loading the object from the database. The problem I'm seeing in this, is the doubled request. I mean, I already have the data in my NSMutableDictionary, so why should I request it again?
I don't want to just assign a certain key-value pair, I want to assign the whole current object entry of the NSMutableDictionary. How would I do this?
If the above code (dictionary) is used to show information on tableview there is a mistake in it dictionary should always starts with "Key" not with index. So better make the dictionary to array and then you will have index to get complete information in indexes write this code
NSLog(#"%#",[[newArray objectAtIndex:0]objectForKey:#"id"]);
Hope this will help..

RestKit post with NSArray of NSDictionary numbering

I am using RestKit to Post an object to the server.
The object contains an NSArray, which I convert to NSDictionaries before adding it to the parameters list (an NSDictionary)
the NSDictionary queryParams looks like this (printed out using NSLog, and removing some fields for clarity)
requestParams = {
"app_version" = "v1.0(1.006)";
"extended_information" = {
"travel_locations" = (
{
"Aircard_or_Tablet" = 0;
Country = "United States";
"End_Date" = "04/12/2013";
"Start_Date" = "03/12/2013";
}
);
"using_voice" = 0;
};
}
I then send it:
[objectManager postObject:nil path:server_path parameters:requestParams success:… failure:…]
The problem is that when RestKit creates the query string, it seems to increment the counter for each key in the dictionary, rather than per item in the array. The following is what is received by the server. I added a line break after each parameter for clarity:
app_version=v1.0(1.006)
&extended_information[travel_locations][0][Aircard_or_Tablet]=0
&extended_information[travel_locations][1][Country]=United States
&extended_information[travel_locations][2][End_Date]=04/12/2013
&extended_information[travel_locations][3][Start_Date]=03/12/2013
&extended_information[using_data]=0
The server is expecting (and I would expect) travel_locations to remain at [0] for all the keys in that one object, like this:
app_version=v1.0(1.006)
&extended_information[travel_locations][0][Aircard_or_Tablet]=0
&extended_information[travel_locations][0][Country]=United States
&extended_information[travel_locations][0][End_Date]=04/12/2013
&extended_information[travel_locations][0][Start_Date]=03/12/2013
&extended_information[using_data]=0
And, of course, if a second travel_location were there it would have [1], the third would have [2], and so forth.
Is this a bug in RestKit? Is there a better way I can be sending this request so that it does work? I do not have the option of changing the server.
To question is:
How can I have RestKit convert the NSDictionary for parameters properly so that it does not increment the array index identifier for each key in the array object?
Thanks,
-Nico

IOS Objective C - Updating NSArray values from another NSArray

I have a NSArray with objects Called Houses, each object has 3 fields = id,address,price
I can set the value by using
[house setValue:#"£100k" forKey:#"price"];
However I have another NSArray called Movements, each object has 2 fields, id & price
so how do I update the 1st array with details from the 2nd array.. in english it I trying to do "Update house price with movement price where house id = movement id"
Thanks in advance
Sounds like you want a double loop:
for (House* house in houses)
for (Movement* movement in movements)
{
if (house.id == movement.id)
house.price = movement.price
}
If there will only be one such instance you may want to break early, you'll need an extra BOOL for this:
BOOL breaking = false;
for (House* house in houses)
{
for (Movement* movement in movements)
{
if (house.id == movement.id)
{
house.price = movement.price
breaking = true;
break;
}
}
if (breaking) break;
}
Edit: If you are doing this kind of thing frequently, you probably want to construct a dictionary keyed on the id field so you can look up an object quickly rather than by looping the whole array. NSDictionary is the class you want for this, also note that keys must be objects, so if your id field is an int you'll want to convert it to an NSNumber with [NSNumber numberWithInt:] before using it as a key.

accessing json content from dictionary

I have a dictionary containing Json data. I am not able to handle the data inside it. I tried enumerating but keeps failing.
This is the data format I am getting back using NSLog:
2013-03-21 12:48:36.973 SaleItems[21009:c07] {
article = "Surface's other shoe is about to drop: the full Windows 8, Intel Core-driven Surface is headed to stores in just a few weeks.";
id = 2;
title = "Microsoft Surface Pro hits U.S. and Canada ";
}
2013-03-21 12:48:36.974 SaleItems[21009:c07] {
article = "Hit by growing demand for Samsung devices, the iPhone will lose smartphone market share for the current quarter, says a Citi analyst";
id = 3;
title = "iPhone to shed market share";
}
2013-03-21 12:48:36.974 SaleItems[21009:c07] {
article = "The carrier says it plans to buy wireless spectrum and network assets from Atlantic Tele-Network, aka Alltel, in an effort to boost its spectrum coffers.";
id = 4;
title = "AT&T spends $780 million ";
}
I know from the looks of it that I am getting back a dictionary of arrays. How can I access the arrays and elements inside them?
It looks like you have key as article, id and title, simply access the object in loop as:
*I am assuming that you have a Class called SalesItem and the above log are from each object of that
for(SalesItem *object in SalesItem){
NSLog(#"Article : %#",object[#"article"]);
NSLog(#"Id : %#",object[#"id"]);
NSLog(#"Title : %#",object[#"title"]);
}

Resources