What's the right way to parse an ISO8601 date in cocoa? - objective-c

I would like to parse ISO8601 dates in Cocoa, both for iOS 4+ and OSX 10.6+
There are a few questions about this on StackOverflow already, but in my opinion none of them contain good answers. Here's what I think constitutes a good answer:
The answer should point to code with support for ISO8601. This code should compile cleanly under XCode 4 for both iOS 4+ and OSX 10.6+.
The code should support all possible ISO8601 date formats.
Please note that there are many, many possibilities here. Simply answering with one or two format strings for NSDateFormatter is not going to cut it.
The answer should not be this library. That's because it is riddled with dangerous 32-bit assumptions, it's far more complicated than necessary, and it doesn't compile clean with XCode4/Clang. Bottom line: I don't trust it at all!
Thanks, fellow Cocoa-ites. I'm excited to find out if there's a real answer here!

The best way is this library. ☺
I should add a link on that page to the Bitbucket repo, which contains newer source code (including 32-bit and Clang fixes!) and has an issue tracker. If you find any other bugs in it, please file them.
I'd also like to know what you mean by “more complicated than necessary”. Normal usage is very simple:
ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease]; //Or, if you prefer, create it once in -init and own it until -dealloc
NSDate *parsedDate = [formatter dateFromString:inString];
NSString *unparsedString = [formatter stringFromDate:inDate];
You can switch out dateFromString: or stringFromDate: for one of the longer methods if you need more information (e.g., to preserve the time zone).
If you mean something else, I want to hear it so I can improve the library.

Related

Difference between NSSpellchecker and UITextChecker

So I'm getting ready to implement a spellchecker into my app, and although other people have already asked about how to do this, I couldn't really get a clear picture on the advantages/differences between NSSpellChecker and UITextChecker. Would someone mind just taking a moment to clarify this for me? It seems from the other answers that I have read that UITextChecker is the best thing to use, I'm just trying to make sure I have a complete understanding as to why this is before implementing.
Thanks!!!
NSSpellChecker is part of AppKit and only available on the Mac, whereas UITextChecker is part of UIKIt and thus only available on iOS. So, depending on which platform you're writing for, you'll need to use the appropriate class for that platform.
For one, UITextChecker returns word completions sorted alphabetically. NSSpellChecker returns the word completions sorted by word frequency.
The (incorrect) docs for completionsForPartialWordRange:inString:language: say:
The strings in the array are in the order they should be presented to the user—that is, more probable completions come first in the array.
UITextChecker word completions for "th":
thalami,
thalamic,
thalamus,
thalassic,
thalidomide,
thallium,
...
the,
...
NSSpellChecker word completions for "th":
the,
this,
that,
they,
thanks,
there,
that's,
...

Best way to store and reference a list of 3rd party API keys and constants in code?

I've got a list of constants for numbers api's that I'd like to collect in one location in code and am wondering about the best ways to do this. Here's what I have so far..
A static array with all the constants...
static NSDictionary* kApiConstants =
[NSDictionary dictionaryWithObjectsAndKeys:
#"crittercism-app-id", #"myAppId",
#"crittercism-key", #"myAppKey",
#"crittercism-secret", #"mySecretKey",
#"content-server-url-dev", #"http://my-dev-url/",
#"content-server-url-stg", #"http://my-staging-url",
#"content-server-url-pro", #"http://my-production-url",
nil];
I then have a macro for quick retrieval of items in the array...
#define MYAPIKEY(x) [kApiConstants objectForKey:x]
I like this setup. It makes code cleaner to read overall and makes merging easier for my purposes between branches in our git repo. One feature I would love to have at build/compile time is if a string is not in the dictionary then a build and/or compiler error would get flagged to indicate this.
I'm sure others have run into this situation before with so many 3rd party libraries, sdk's and what have you in a project it's hard to keep track of them all. For those willing to share what systems have you come up with to help with this?
In my case this is for an iOS project but the situation applies for any kind of project really.
What advantage do you see in using a dictionary over just making them constants on their own? Your wish:
One feature I would love to have at build/compile time is if a string is not in the dictionary then a build and/or compiler error would get flagged to indicate this.
would be solved by just making them constants in their own right.

OSX Quicksilver plugin, Objective C guidance

I'm attempting to modify a Quicksilver plugin (screen capture) in Xcode, in order to allow the screencapture destination path to be set to the defaults com.apple.screencapture path value. The plugin currently has the path hardcoded for some reason, so even when you've changed the OSX default path for screen captures, the plugin still sends them to the OSX default, which is the desktop.
I'm a literal noob with Xcode/Objective C, so any info/examples or point in the right direction would be much appreciated.
The screencapture plugin file which needs to be tweaked is the "QSScreenCapturePlugIn.m" file - here's a snippet:
(QSObject *)captureRegion:(QSObject *)dObject{
NSString *destinationPath=[#"~/Desktop/Picture.png" stringByStandardizingPath];
destinationPath=[destinationPath firstUnusedFilePath];
NSTask *task=[NSTask launchedTaskWithLaunchPath:SCTOOL arguments:[NSArray arrayWithObjects:#"-is",destinationPath,nil]];
[task waitUntilExit];
[[QSReg preferredCommandInterface] selectObject:[QSObject fileObjectWithPath:destinationPath]];
[[QSReg preferredCommandInterface] actionActivate:nil];
return nil;
}
I've searched high and low for examples demonstrating how to read from the defaults database, but it seems information is sparse.
Thanks!
I can't believe no one was able to help out with an answer or pointer on this one (or maybe I'm just not patient enough), but thankfully I was able to find the answer while reading through some other answers here on SO - unfortunately I can't reference the Question here, since I lost the page.
Anyway, this will do it:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *screenCaptureLocationString = [[defaults persistentDomainForName:#"com.apple.screencapture"] valueForKey:#"location"];
Amazingly hard to find this info for some reason, but hopefully posting it here will help someone else.

iPhone Number Text To A Full Currency Equation [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Struggling with currency in Cocoa
Re-Apply currency formatting to a UITextField on a change event
I have been all over the web and either examples are no longer valid or people didn't really know what they were doing.
So i have come to you guys for help,
I need to get a a textbox(TextField) to Display when they click into it a "$"...that was Easy lol
but i also need from the dollar sign to $0.00 then $0.01, $0.10 and so on.
And save an instance of it without the Symbols.
I am very new to Objective C and would like maybe a simple explanation or even a good blog to read about it. "only if you just recently read it and its not outdated"
You want to use NSNumberFormatter to accomplish this. The NSNumberFormatter documention from Apple is very good (I've just re-read it and it's not outdated). Specifically, look at the section on Configuring the Format of Currency. This should be enough to get you started.
Another good place to look if you prefer examples to documentation, is to read through the section on Number Formatters from Apple's Data Formatting Guide. This goes through examples of how to set up currency formatters to your specifications with concrete examples.
Finally, in order to integrate this with a UITextField, you will want to use the delegate method
– textField:shouldChangeCharactersInRange:replacementString:
This allows you to validate and update characters as the user types them in.

How do I create a Numbers spreadsheet using objective-c?

I'm writing a Cocoa application and I'd like to generate a Numbers spreadsheet from my application using Scripting Bridge. I've generated the Numbers.h file and linked the ScriptingBridge.framework per the directions in Apple's Documentation. Below is the code I'm using to try to simply create a Numbers document and save it.
NSString *path = #"/Users/username/Desktop/Test.numbers";
NumbersApplication *numbers = [SBApplication applicationWithBundleIdentifier:#"com.apple.iWork.Numbers"];
[numbers activate];
NumbersDocument *document = [[[numbers classForScriptingClass:#"document"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:project.title, #"name", nil]];
[[numbers documents] addObject:document];
[document saveAs:nil in:[NSURL URLWithString:path]];
The code compiles and runs and when I try the saveAs:in: method I get the following error:
-[SBProxyByClass saveAs:in:]: object has not been added to a container yet; selector not recognized [self = 0x2005912e0]
Is there something else I have to do besides adding the document to the [numbers documents] array?
I'm open to using AppleScript, but I'd prefer to using the Scripting Bridge if I can.
Ehh, Numbers scripting with SB; two black arts for the price of one. I would suggest trying to do it in AppleScript first, in order to narrow down the problem a bit.
If it breaks in AS too, then either you've phrased the commands wrongly or there's a problem in Numbers. Since most application scripters use AppleScript, you'll find it easier to get help if you can present code they'll recognise.
If it works, then either your translation of the commands to ObjC is incorrect or there's a problem in SB. Having a working example in AS will provide a starting point for figuring out where things are going wrong.
You might also look into objc-appscript, which provides a more reliable, less obfuscated alternative to SB. Its ASTranslate tool makes it easy to translate working AS commands to ObjC syntax.
Numbers doesn't yet support creation of documents via Applescript. You have to use GUI scripting. The new version of Numbers is supposed to be out Jan 6, 2011 and (hopefully) will fix its severely limited Applescript support.