How to give relation among group of NSMutableArray? - iphone

my application is world guide. there is a 4 tableview and 4 mapview.
Continent
Country
City
Place
I have taken NSMutableArray for each above item and allocate them in view did load . when Asia is selected from the Continent table then only Asian countries should display from the country array. if country is selected then only city of respective country should be display in city table view from the city array like vice for place. it's like a tree. it is too lengthy to work with an array. Please guide me if another way is exist that make this task easy.

I think the better way to implement should be:
Use NSMutableArray only to store the top level Object.
Use NSMutableDictionary to store the mid & lower level objects.
Store the NSMutableDictionary in your NSMutableArray at each index of your top level Object.
Put the key as the Continent name in top NSMutableDictionary & value as country name & follow the same for next level.
In this case, you have to maintain only a single NSMutableArray to work. No need to handle 4-5 different arrays.
Hope it works for you.

To handle the data or categorize the data with the way you have the data about all four objects is quite complex because there is no key value pair by which you can identify that which country comes under which continent and which city comes under which country and so on.
So what you have to do is, use web service by which you can fetch the categorized data.
Here is link of web service by which you can simply hit the URL and catch the XML as the response.
URL : http://ws.geonames.org/countryInfo?
Above URL gives you the XML as the response. Like this :
<geonames>
<country>
<countryCode>AD</countryCode>
<countryName>Andorra</countryName>
<isoNumeric>020</isoNumeric>
<isoAlpha3>AND</isoAlpha3>
<fipsCode>AN</fipsCode>
<continent>EU</continent>
<continentName>Europe</continentName>
<capital>Andorra la Vella</capital>
<areaInSqKm>468.0</areaInSqKm>
<population>84000</population>
<currencyCode>EUR</currencyCode>
<languages>ca</languages>
<geonameId>3041565</geonameId>
<west>1.4071867141112762</west>
<north>42.65604389629997</north>
<east>1.7865427778319827</east>
<south>42.42849259876837</south>
</country>
.
.
.
and so on...
or if you want JSON format. take the following URL :
URL api.geonames.org/countryInfoJSON?username=demo&formatted=true
So now you are just few steps away from your solution.
Now Do the XML parsing and filter the data according to your requirement.
or if you want to have the detail knowledge about this web service
just visit : http://geonames.r-forge.r-project.org/
Hope it works for you.

I suppose you have all the data about Continent, Country, City and Place. In this situation the way of connecting all 4 arrays is totally depends upon the format of data you have for displaying on TableViews.
Whether it is a array of string or array of dictionary for each item or JSON or XML.
Please tell me the data format. After that i'll try my best.

Related

REST API structure for multiple countries

I'm designing a REST API where you can search for data in different countries, but since you can search for the same thing, at the same time, in different countries (max 4), am I unsure of the best/correct way to do it.
This would work to start with to get data (I'm using cars as an example):
/api/uk,us,nl/car/123
That request could return different ids for the different countries (uk=1,us=2,nl=3), so what do I do when data is requested for those 3 countries?
For a nice structure I could get the data one at the time:
/api/uk/car/1
/api/us/car/2
/api/nl/car/3
But that is not very efficient since it hits the backend 3 times.
I could do this:
/api/car/?uk=1&us=2&nl=3
But that doesn't work very well if I want to add to that path:
/api/uk/car/1/owner
Because that would then turn into:
/api/car/owner/?uk=1&us=2&nl=3
Which doesn't look good.
Anyone got suggestions on how to structure this in a good way?
I answered a similar question before, so I will stick to that idea:
You have a set of elements -cars- and you want to filter it in some way. My advice is add any filter as a field. If the field is not present, then choose one country based on the locale of the client:
mydomain.com/api/v1/car?countries=uk,us,nl
This field should dissapear when you look for a specific car or its owner
mydomain.com/api/v1/car/1/owner
because the country is not needed (unless the car ID 1 is reused for each country)
Update:
I really did not expect the id of the car can be shared by several cars, an ID should be unique (like a primary key in a database). Then, it makes sense to keep the country parameter with the owner's search:
mydomain.com/api/v1/car/1/owner?countries=uk,us
This should return a list of people who own a car with the id 1... but for me this makes little sense as a functionality, in this search I'll only allow one country:
mydomain.com/api/v1/car/1/owner?country=uk

Swift: Core Data - Resolving a logic issue with object creation

So, my Swift app allows a user to choose sports teams to see historic match information for. Currently, a user selects team(s) and the JSON data file of historic matches is scanned.
If a historic match includes a name of a selected team, the details of the match are stored in a Core Data entity, which is fed into my main Table View.
However, this presents an issue I can't get my head around solving.
If a user selects team A and B, and the database contains a match where team A and B played EACH OTHER, two objects for the match details are created, and as such, Table View cell is created twice, once for team A being found in the instance of the match, and again for team B.
Is there an easy and efficient way to trim any duplicates caused in this way? I don't know whether to handle this at the object creation time, or just to find a way of removing any duplicated cells from my Table View.
Thanks so much.
I think you should redesign your setup. Have all the records to be searched stored in Core Data.
If you have a hardcoded JSON file - import it on first start. If you have retrieved JSON - insert / update the elements that are new / changed in your Core Data object graph.
You would have a Match or Game entity and it would be retrieved only once. The fetch predicate would be something like
NSPredicate(format: "homeTeam = %# || guestTeam = %#", selectedTeam, selectedTeam)

How to iterate through an NSArray of objects and create a new NSArray containing a subset of the original?

I am working on a basic iPhone app that pulls restaurant objects from a SQLite table, and displays them for the user in a table. My app is navigation based. I have no problems pulling the data from the SQLite database, and then sorting them. That I am able to do no problem. Each restaurant object has various properties, like "name", "address", "category" (i.e. the type of restaurant like greek, italian), and others. Now, after retrieving the records from the database, I am able to successfully put them into an NSMutableArray, and then sort them into an NSArray.
My question is, using this NSArray (or the original NSMutableArray), I'd like to create another array, with the first index containing "All", and then each remaining index contain the unique name of the category of restaurant from the original list, sorted in alphabetical order, and then present this as a table to the user on the next screen. From this table, if the user selects "All", the user will be taken to another screen displaying another table where they can see all the restaurants, regardless of genre.
If however, the user selects a particular category from the available list, the user will be taken to a screen that displays a table composed of only those restaurants of that type (e.g. only Italian restaurants, only Chinese restaurants, only Thai restaurants). How would I go about doing this? Any ideas or suggestions?
One possible algorithm outline:
Iterate over your NSMutableArray (e.g. for(item in array)) inserting the category property of each item into an NSMutableSet (or an NSCountedSet if you'd also like the number of times each category occurs)
Use the NSSet sortedArrayUsingDescriptors: method to obtain a sorted array of the categories.
Add the All entry in whatever way suits

iPhone UIPickerView and array

I'm looking for best way to populate UIPickerView with data from webservice. I have web service:
Country1Country1CODE
Country2Country2CODE
Country3Country3CODE
I made all parsing correct and i can reach all values of service like service.country or service.code.
Problem is what in UIPickerView I need to show list countries, but return as value selected country code. (i hope i explained understandable). All day i was looking for best way how to do it, but I didn't found nothing... Thanks for you help and wasted time :)
Once you've retrieved the data from the web server, your best bet is to use an NSDictionary as a repository for the data, which you'd use to store the country codes as objects with the country name as the key.
i.e.: ... dictionaryWithObject:#"826" forKey:#"United Kingdom"
You could then use the NSDictionary allKeys method to obtain an NSArray of country names to populate the UIPickerView with and (once the user has selected a country) can trivially retrieve the corresponding country code via the NSDictionary objectForKey method.

Tableview with sections managed by fetched results controller

I'm quite new to CoreData and need help with one issue. I have tableview which simply lists cities stored in SQLite db. City is defined by "cityId" and "name" attributes.
Data are fetched using NSFetchedResultsController and everything works fine except that I can't figure out how to make FetchedResultsController to group cities by first letter of city names.
When I supply "name" for sectionNameKeyPath argument of initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName: method, it creates section for each city, which is obviously something I don't want. I can't figure out how to create an expression which would make it work in way I need.
thanks for any tips and/or advices on this
Matthes