restkit Mapping CLLocationCoordinate2D from response - ios

I have some geolocation data, and I need to parse it to CLLocationCoordinate2D from object geo
"geo": [
49.233083,
28.46821699999998]
I found some information on how this could work with RKCLLocationValueTransformer, but I don't understand how to get this to work for my situation.

You can't go direct to a CLLocationCoordinate2D because it's a struct, not a class.
RKCLLocationValueTransformer works in the same way as the value transformer for dates (which convert from NSString to NSDate instances) except that it converts from NSDictionary to CLLocation.
Your problem is that your JSON provides the location as an array, not a dictionary...
As such, you will need a different solution. You could look at making a modification to RKCLLocationValueTransformer (and raising a pull request). Your modification would add a different creation method (perhaps locationValueTransformerForArrayRepresentation:, which takes an enum parameter describing if the latitude or longitude comes first in the array). Then modify transformValue:toValue:ofClass:error: to check the configuration and make the appropriate mappings.
After you've done that the github page describes how to use the transformer.

Related

Get GMSPlace from longitude/latitude coordinates?

I looked at the API here, but it only mentions that you can get the GMSPlace for the user's current location. Is there any way to create my own custom GMSPlace object or get the GMSPlace for the lat/long I want? I looked at previous solutions but didn't find anything.
As per Google's docs:
Represents a particular physical place. A GMSPlace encapsulates
information about a physical location, including its name,
location, and any other information we might have about it. This
class is immutable.
Apparently there is no way to make your own GMSPlace object from the coordinates, nor there' a method that Google's SDK provide.
What I did before, I search for places, the one that Ajay user above discussed.
So the process is:
Get coordinates.
Reversegeocode to get the string address.
Search the string address using the GooglePlaces autocomplete.
From the first result in #3, the placeId is used to get the GMSPlace object using the lookUpPlaceID method.
Beware though that sometimes it returns a wrong address. I've tried it.
GMSPlace is a class provided by Places SDK which provides information about a specific place. You can not create your own custom object.
EDIT: To find a place other than your current location it provides Autocomplete api which automatically return location suggestions while users type.

NSMutableDictionary contents inconsistent with output of allValues

So long story short, there's a discrepancy between the output of a NSMutableDictionary's contents and the result of calling allValues on the same object. Below is some debugger output after inspecting the object which demonstrates my problem: (made generic of course)
(lldb) po self.someDict.allKeys
<__NSArrayI 0xa5a2e00>(
<SomeObject: 0xa5a2dc0>,
<SomeObject: 0xa5a2de0>
)
(lldb) po self.someDict.allValues
<__NSArrayI 0xa895ca0>(
0.5,
0.5
)
(lldb) po self.someDict
{
"<SomeObject: 0xa5a2dc0>" = (null);
"<SomeObject: 0xa5a2de0>" = (null);
}
So as we can see, the actual output of the NSMutableDictionary contains null values for both its entries, but the contents of .allValues contains the proper data. These three outputs were taken at the same time in execution.
I'm not sure why this is happening, but I think it may have something to do with the fact that I'm encoding/decoding the object which this dictionary is a property of using CoreData. I believe I'm doing this properly:
[aCoder encodeObject:self.someDict forKey:#"someDict"];
and to decode
self.someDict = [aDecoder decodeObjectForKey:#"someDict"];
The weird thing is that if I inspect the dictionary before it ever gets encoded, it is still in the state described at the beginning of the post, so this is why I'm doubtful the CoreData actions are screwing with the contents.
As always, please don't hesitate to request additional information.
EDIT: The problem was as answered below. I was using a custom class which didn't cooperate with isEqual, so my solution was to change the storage and structure of my logic, which made using a Dictionary unnecessary.
I have not been able to duplicate the problem using NSString as keys and NSNumber as values. I suspect that your custom class does not properly implement hash and/or isEqual. Specifically, the results from those two methods must be consistent, meaning that if isEqual returns true, then the hash values for the two objects must be identical.
You also need to ensure that your class implements NSCopying properly and that a copy is equal to the original.
As a general rule, don't use custom objects for dictionary keys. Just use strings and be done with it.
As user3386109 points out, custom objects must properly implement the -hash and -isEqual methods in order to be used as dictionary keys, and even then, custom objects don't work correctly for dictionary keys for things like key/value coding.

Serializing a custom object into NSDictionary or NSArray to use NSJSONSerialization

I've been looking for a way to serialize custom objects with NSJSONSerialization avoiding the use of third-party libraries, and I couldn't find any example. Is there any way of "automatically" create an NSDictionary and NSArray from an object, without having to create it typing in code all the object's member names yourself one by one and setting manually the values? I found a related post, but it is pretty old, things may have now changed.
Thanks
You can use KVC to ask any object for dictionaryWithValuesForKeys: which will give you a dictionary representation of the object.
If you need to change the property / key names then you want to do some mapping and (depending on what you're using the JSON for) you may find RestKit useful.

NSDictionary Vs. NSArray

I am reading on objective-c (a nerd ranch book), and I can't help thinking about this question: How do I decide which collection type, NSArray or NSDictionary (both with or w/o their mutable subclasses), to use when reading content from URL?
Let's say am reading JSON data from a PHP script (a scenario am dealing with), which to use? I know it is stated in many references that it depends on structure of data (i.e. JSON), but could a clear outline of the two structures be outlined?
Thank you all for helping :)
NSArray is basically just an ordered collection of objects, which can be accessed by index.
NSDictionary provides access to its objects by key(typically NSStrings, but could be any object type like hash table).
To generate an object graph from a JSON string loaded via a URL, you use NSJSONSerialization, which generates an Objective-C object structure. The resulting object depends on the JSON string. If the top-level element in your JSON is an array (starts with "["), you'll get an NSArray. If the top-level element is a JSON object (starts with "{"), you'll get an NSDictionary.
You want to use NSArray when ever you have a collection of the same type of objects, and NSDictionary when you have attributes on an object.
If you have, lets say a person object containing a name, a phone number and an email you would put it in a dictionary.
Doing so allows the order of the values to be random, and gives you a more reliable code.
If you want to have more then one person you can then put the person objects in an array.
Doing so allow you to iterate the user objects.
"withContentOfURL" or "withContentOfFile" requires the data in the URL or the file to be in a specific format as it is required by Cocoa. JSON is not that format. You can only use these methods if you wrote the data to the file or the URL yourself in the first place, with the same data. If you write an NSArray, you can read an NSArray. If you write an NSDictionary, you can read an NSDictionary. Everything else will fail.

RestKit: Map 2-Dimensional Array (Collection in Collection)

I was just trying to parse a JSON-Object which includes a 2-dimensional array.
Example:
{
"2dimarray": [
[{"key": "val"}, {"key": "val"}],
[{"key": "val"}, {"key": "val"}]
]
}
Assuming the contents of 2dimarray[x][y] are only of one type, I added the mapping:
[objectMapping mapKeyPath:#"2dimarray" toRelationship:#"2dimarray" withMapping: myMappingForIncludedObjects];
In the log RestKit tells me:
W restkit.object_mapping:RKObjectMappingOperation.m:438 WARNING: Detected a relationship mapping for a collection containing another collection. This is probably not what you want. Consider using a KVC collection operator (such as #unionOfArrays) to flatten your mappable collection.
But actually it is what I want. Basicly I assumed that the object mapper would fill my Objective-C property NSArray* 2dimarray with NSArray*s that include objects that are mapped with myMappingForIncludedObjects. Instead, each array is mapped (which fails, of course) with myMappingForIncludedObjects.
What am I doing wrong? Or better: What do I need to do to archive the behavior I expected?
I believe that the issue you cite is Blake explaining the problem, not a solution. I don't think RestKit is set up to handle the mapping that you describe (an array of arrays of objects). You can walk through an example of what he describes in the issue as well as looking at his commit, and you'll see that the introduced logic was aimed at detecting the problem and logging it for debugging purposes.

Resources