System.TimeSpan to something I can use in iOS - ios

I'm getting a bunch of information from a web server with SOAP and NSXMLParser, and one of the properties I'm getting is time for when a kid is being picked up...
e.g. a response would be: PT15H
In the documentation it's called: System.TimeSpan pickUpTime
Now, the other properties includes stuff like integers, strings and booleans and I just save them with e.g.
else if([elementName isEqualToString:#"a:LastName"])
{
aParent.lastName = currentElementValue;
}
What do I do with PickUpTime? I need to get the time and show it in a label in a clean manner, like 15:00

You can use NSDateFormatter and give it the proper structure of the strings you are getting. This way it can be converted into an NSDate object for your use, i.e. displaying it as "15:00" and such.

Decided to just use stringByReplacingOccurrencesOfString to remove the letters, since I know that I will always get certain letters... then I can convert it into a NSDate with with the format HH:mm

Related

Grails JSON Views - default date rendering

When using Marshallers, I could use something like:
JSON.registerObjectMarshaller(Date) {
return it?.format("yyyy-MM-dd'T'HH:mm:ss")
}
and it would make all Date objects render to JSON with that format. Is there an equivalent I can use with JSON Views to display Date objects?
The format used for Date is hard coded in JsonOutput.java (https://github.com/grails/grails-views/blob/master/json/src/main/groovy/grails/plugin/json/builder/JsonOutput.java)
I've created an issue about this https://github.com/grails/grails-views/issues/92
A temporary workaround is to duplicate JsonOutput.java and place it in src/main/grails/plugin/json/builder with the format modified to your need.

GKTurnBasedMatch - Obtaining the End Date

I am currently building an iPhone turn-based game that utilizes Game Center for all network functionality. I created a custom UI to display matches in a UITableView where the local player can enter, create, quit, and delete a game.
My issue is that in my cells, I want to display relevant dates (last turn taken, match created when no turn is taken yet, and match ended date for ended match status). For the life of me, I can't seem to figure out a good way to derive the exact date and time a match ended. The GKTurnBasedMatch object has the creationDate property which is great, but there is no property for when the match ended.
Currently I am just pulling the last turn date of one of the participants, but obviously that is not even close to optimal. I'm thinking the best workaround is to encode the current date in the matchData object sent when endMatchInTurn is called, but I'm hoping I'm missing a better solution somewhere in the API.
Thanks!
Corbin
After much research, there is no valid way to actually obtain the time when a GKTurnBasedMatch ended via the -endMatchInTurnWithMatchData method within the GameKit API. However, I do have a perfectly viable solution which just required a little bit of extra work. For whatever object you use to encode your matchData property, just assign another property and make it an NSDate. Then right before calling the endMatch method, set the new NSDate property to [NSDate date], which returns the current date and time, and archive it before sending as the matchData object. My own code is below:
GameState *stateToSave = [NSKeyedUnarchiver unarchiveObjectWithData:matchToQuit.matchData];
stateToSave.matchEndDate = [NSDate date];
NSData *endGameData = [NSKeyedArchiver archivedDataWithRootObject:stateToSave];
[matchToQuit endMatchInTurnWithMatchData:endGameData completionHandler:^(NSError *error) {
// Code
}

Cannot use a predicate that compares dates in Magical Record

I am making a method that will remove all of my NSManagedObjects that were not updated in the last sync.
- (void)removeStaleObjects {
// Delete objects that have not been updated in this sync.
NSPredicate *stalePredicate = [NSPredicate predicateWithFormat:#"updated < %#", self.syncStart];
NSArray *staleObjects = [Node MR_findAllWithPredicate:stalePredicate];
for (Node *n in staleObjects) {
[[NSManagedObjectContext MR_defaultContext] deleteObject:n];
}
}
The code keeps failing on the MR_findAll... line with
[__NSDate objCType]: unrecognized selector sent to instance
I have checked my syntax with the apple documentation and I am 99% positive that I am creating the predicate correctly, startDate is just
_startDate = [NSDate date];
that gets run prior to my sync. then after my sync I call
[self removeStaleObjects];
Does anyone know where I am messing up?
Update: I was able to get it to work by storing the update time as a double. However, I am still interested in getting it to work with NSDates so if anyone figures something out, please post it here.
The problem is the name "updated" of your attribute. It conflicts with the isUpdated method of NSManagedObject. (See Core Data NSPredicate "deleted == NO" does not work as expected for a similar issue with a "deleted" attribute.)
If you rename your attribute, everything works as expected.
It also looks to my that your predicate is formatted correctly. Here are a couple things you can do:
1) When debugging this, print out that predicate. You should see something like:
updatedDate < {some integer value}
Dates are stored as integers under the covers, and a predicate converts it properly as well. If your predicate isn't printable in the debugger, you'll know right away
2) Check your updatedDate type. Make sure that's a date (I trust it's already a date, but you didn't specify in your question)
3) Make sure your Node object has the updatedDate attribute on it.

Restkit custom mapping

Wondering if its possible to concatenate keyPaths to one attribute in mapping objects. Looking for something like
mapKeyPaths #"firstname", #"lastname", nil toAttribute:#"name"
Where name would then be "Bob Johnson"
** The API I am dealing with passes over a date and a startTime attribute, as 2012/02/28 and 16:12 respectively, as Strings.
It would be easier to just use startTime as "2012/02/28 16:12".
I figured I can get around this issue by leaving the date and startTime as NSDate fields, so I have tried setting up a dateFormatter per Restkits instructions. When I tried that idea, just using "HH:MM", for the startTime dateFormatter, it shoves "1970/01/01 16:12" into the startTime field.
Anyone have any suggestions, besides going through each record manually after mapping to Core Data and putting the fields in programatically?
I don't think you can do these kind of programmatic mappings yet.
Two alternative solutions come to mind:
1) In willMapData (or something like that) you can manually modify the incoming serialization before object mapping occurs. There you can specify a format you like.
2) Save both these properties in your Core Data entity and create a third transient attribute which is calculated at runtime, and when required, by passing these two values through a NSDateFormatter.

Sorting a NSSet of NSManagedObjects by a NSDate yields error

I am trying to pull in a RSS feed and sort by pubDate. When I examine the 'updated' property, most of the time it is correct and give me a proper date but when I try to convert from a set to a sorted array, I get random results from the sort. I've tracked this down to the fact that when sort is doing it's comparesion, the property (which is an NSDate, see figure1) is coming in and being compared as a __nscfnumber! (also figure2)
Any help or idea would be much appreciated.
figure1
figure2
I assume the comparator block is just for diagnostic purposes? You don't actually need to supply a comparator for NSDate or any of the provided attribute type classes.
If the debugger is reporting that the date1 object is of a NSNumber-cluster class type, then somewhere a NSCFNumber instance is being assigned to to the updated attribute. The debugger ignores factors like a cast and instead simply asked the object what its class is. If the object says it is a NSCFNumber then it is, regardless of how the code treats it otherwise.
Why that happens, I can't say based on the code provided.
You might try logging the value and class of the updated attribute before you attempt the sort to see if it reports properly. I would also recommend decomposing the entire line. Nesting all those method calls will work of course but it is error prone and hard to debug.

Resources