How to print all available information of a locale? - iphone

The locale object must contain more information than apple talks about in the documentation. i.e. it must contain several date format strings. How can I print all this stuff to see where they are? Is there a method that would print out all information about an object?

Why do you believe the NS/CFLocale object has date format strings in it? I would assume these strings are stored in the NSDateFormatter class, probably as static data rather than instance data. Just my guess, but based on my experience with NSCalendar.

Related

Playground simple Date() is printing value taking into cosideration my time zone

It seems that for first three lines in the screenshot the TimeZone is not applied(ok - according to documentation it should be like this), but for the last one it seem to be GMT+2 (my current time zone).
Anyone has explanation why for the last line playground is presenting date taking into consideration my system TimeZone?
Playground has its own custom rules for the gutter display. They are generally more friendly forms of the output. For another example, see UIColor, which has a radically different (and very custom) output in Playgrounds.
See the docs for CustomPlaygroundDisplayConvertible for the full list of types that get special Playgrounds handling, and how to provide custom handling for your own types.
Keep in mind, of course, that all of these descriptions are purely for informal and debugging use, and none are appropriate for actually displaying to users. See DateFormatter for how to format a date for humans. (You likely know this already, but for people reading along it can be a helpful reminder.)

How to add NSDate+BSJSONAdditions to the BSJSON project

I need to know how to get BSJSON to handle the NSDate that is in coredata
in the json it returns null for the date, what needs to go into the category to make this work?
Assuming you're talking about http://github.com/blakeseely/bsjsonadditions, there's really no easy way to get a JSON object and dump it into NSDate. You need some intervening logic - like asserting a common date format, and then do the NSDate <-> String conversion yourself, and letting the JSON format represent the string.
Common methods for doing this are to use "# of seconds since Epoch" to represent the time, or to just fully qualify out an ISO-8601 time string. Which is easier often depends on what other systems you're working with and which they can easily convert and use. The first is accomplished super-easy with Objective-C by using NSDate dateWithTimeIntervalSince1970:
Going in and out of an ISO-8601 string is a bit more work, and you'll head into the realm of NSDateFormatter

Easy way to feed in a decimal number as string into an NSDecimalNumber?

Reading the documentation, I would have to do something ugly like this:
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:#"0.00001" locale:usLocale];
But: Isn't there a nicer way of telling that NSDecimalNumber class to look out for the period (.) instead of anything else? Locales feel so unsafe to me. What if one day a new president thinks to be so cool to change the period symbol from . to ,? Then my app crashes. Sure this won't happen so likely. But there is a little chance it could. Enough to scare me using a locale for this ;-)
Any idea? Or is that my only option? And if it is the only option: Is the above approach correct? Or is there even a better one?
And: If I get that right, the NSDecimalNumber object will read that string, parse it and create the mantissa, exponent and isNegative values out of it for internal use, right?
EDIT: I should have mentioned that I add those values programmatically / manually (in my code), they're not user input.
Specifying the locale is only necessary if you're passing in a string that isn't a valid decimal number in the default locale. If your users are creating the strings, it's highly likely that they'll format them properly for the locale they're using.
Using locales is far safer than not using them. Trying to parse the "." out of "1,50" is not likely to succeed.
And considering that
[NSDecimalNumber decimalNumberWithString:#"0.00001" locale:usLocale]
is described in the docs as
Creates and returns an NSDecimalNumber
object whose value is equivalent to
that in a given numeric string.
I think it's a safe bet that it creates and returns an NSDecimalNumber whose value is equivalent to that of the string.
Locales are a Good Thing; if the president were to change the decimal point from . to ,, the en_US locale would be updated by the next release of the OS -- you don't explicitly mention the locale's decimal point in the above code, so you're OK there. That being said you might want to get the system locale instead of specifying en_US explicitly, which will be wrong anywhere outside the US.
If you're worried about the string, it should be coming from a user in which case they'll use their locale-specific decimal point, which should match up. If you're trying to initialize an NSDecimalNumber this way I suppose you could, but I would imagine there are easier ways to skin that cat.
And yes, if you get it right, the result will be an NSDecimalNumber object whose value is equivalent to the string you passed in.
Locales are a Good Thing. Changeable locales applied to static internal data are a Bad Thing, and you're right (if possibly paranoid :D) to be concerned about applying a locale to data you (rather than the user) provides. Here are some solutions that do not rely on applying locales to your internal data.
number = [NSDecimalNumber decimalNumberWithMantissa:1 exponent:-5 isNegative:NO];
number = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:0.00001] decimalValue];
The second would be very easy to turn into a NSDecimalNumber category implementing -decimalNumberWithDouble:. Probably quite useful.

How to convert an NSTimeInterval into an date with time?

I have an NSTimeInterval value, or more precisely an NSTimeInterval "since reference date". I think that's a value in seconds from 1970 or something like so. Pretty standard in most programming languages, I think.
So now I have that ugly value which the user doesn't understand, and I'd like to display a date + time. Is there a useful method or function that would do that, maybe by specifying formats or a locale as well? Maybe the iphone also has built-in support for this kind of stuff so that the date+time is displayed automatically like the user likes it in his/her settings?
You want to create an NSDate with +[NSDate dateWithTimeIntervalSince1970:]. You can then get the date's -descriptionWithLocale: or use the Date and Time Programming Guide from the iPhone library to find out more options for displaying the date.

Is there an overview of all codes that can be used inside NSLog()?

i.e. %# for strings, %f for doubles... I don't know the word for these placeholders but it would be great to have list for how to print booleans and other values.
Since NSLog takes a NSString as its argument, it uses the NSString format specifiers. This is virtually identical to the common printf specifiers. Also, the %# specifier is not limited to NSString objects, but is for any Objective-C objects. The base NSObject class provides a generic description of the object consisting of its class and its address, but many objects will supply information specific to their type, such as the collection classes (NSArray, NSDictionary) which will supply nicely formated dump of their contents. You can provide this for your own objects that you create by overriding -description (see the documentation for more info, including localization capability).
See also: NSString Format Specifiers
It is a normal C format string with the extension of %# (which prints any NSObject by querying its -description method, not just NSStrings).
You can see an overview in printf manpage
Also, there's a very nice overview, as well as some tips and tricks, in the most recent "Friday Q&A" posting on Mike Ash's NSBlog blog:
http://www.mikeash.com/?page=pyblog/friday-qa-2009-07-17-format-strings-tips-and-tricks.html
Here is a little snapshot from "Programming in Objective-C 2.0"
alt text http://img361.imageshack.us/img361/1438/picture1rze.png