iOS Clean Bad XML - objective-c

Sometimes we receive incorrect XML back from a web service. Are there any libraries out there for objective-c, and specifically for iOS that can clean up XML to make it valid for a parser? Something like NSXMLDocumentTidyXML but for iOS?

Take a look at TouchXML, specifically CXMLDocumentTidyXML. Being a JSON fan myself, I've never used it, but it seems to be capable of tidying up xml. You would do something like this:
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:#"<foo></bar>"
options:CXMLDocumentTidyXML
error:&theError];

Related

ARC Ready XML Parser

Ok I just tried to use 3 different xml parsers like TBXML and others. Does anybody know an arc ready xml parser. I tried to convert the files to make them arc ready but its not working correctly, as the process creates new errors. Also to disable the ARC mechanism for the specific files does not work for me.
Is there any XML parser out there, that is not older than 2 years?
Cheers
I've been looking for that too, and I found KissXML.
It's last update was 2 years ago, but it was to convert it to ARC.
I've been using TBXML, but it's not ARC-compatible, and I don't want to have memory leaks because of it, so I'm planning to replace it with KissXML and see how it goes.

Hot Code Swapping in Xcode?

I am doing iOS game development using the cocos2d framework, and I tremendously envy the ability for Eclipse Java programmers to hot swap their code while they are debugging their application (i.e. change out variable values within a method and have the program live update as if you had a REPL without having to recompile).
This seems like something that would be absolutely tremendously helpful when it came to iOS development, where my development environment is (obviously) Xcode and the language I am programming in is Objective C. I have googled around but havent been able to find anything - so I thought I would ask the community.
Does anyone know if there a way to Hot Swap code in Xcode while programming in Objective C?
Thanks in advance
There is a great plugin which allow changing code in live, working APP. It is called InjectionPlugin.
As FAQ says:
How does it work? The Objective-C runtime allows you to load a new version of a class into an application using a bundle even if there is already an implementation linked into the application. Swizzling is used as the bundle is loaded to switch the existing class to use the methods of the new implementation. This can be done repeatedly and works for OSX and iOS applications and on iOS devices.
I made some small video which shows how to install and use this plugin
http://nomtek.com/developers/how-to-install-and-use-injection-plugin-for-xcode/
Hope it helps!
Not possible with the current tools.
Keep in mind that iOS applications are signed -- if you change a single byte you'd have resign the whole thing. One could imagine making it work using runtime's support for dynamically adding and removing methods. But that'd surely require adding some extra stuff to support it on the device, and that's something that malware could easily take advantage of. So it's probably not a feature you'll be likely to see anytime soon.
By the way, Xcode versions 1.x-3.x did have a "Fix and Continue" feature. You could edit as you were debugging, use the Fix and Continue command, and continue running the updated code. I believe it was removed at some point, perhaps due to some combination of: requiring that your project be configured to use "zero link" and perhaps some other things; being less than completely reliable; probably not supporting iOS; the switch to llvm; other changes in Xcode 4. Maybe it'll come back someday -- if you want it back, file a bug -- but again, I think supporting it on iOS would be a challenge.
If you're just talking about changing variable values then you can achieve that surreptitiously via lldb (or, presumably) gdb. Supposing you had:
- (void)uselessMethod
{
NSString *localString = #"I'm some local text";
NSLog(#"%#", localString);
}
And put a breakpoint on the NSLog, at that point you could ask lldb to evaluate a reassignment of localString as a means of performing it. E.g.
po localString = #"Hat"
If you then allow program execution to continue, you should find that the reassignment has stuck. Similarly you can call any method or perform any other sort of assignment.
I've just tested this against Xcode 4.3.2.
You can hot swap a variable value in Xcode by:
expression <variable> = <value>;.
By having a break point in the place where you wanna change the value and doing the command in Xcode console.
Example:
// Messages From Console
(lldb) expression graphFlag = #"X"; // Update variable value
(__NSCFConstantString *) $0 = 0x36f95718 #"X" // Xcode prints the updated value
(lldb) expression graphFlag; // Printing value through expression command
(__NSCFConstantString *) $1 = 0x36f95718 #"X" // Hot Swapped variable value

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

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.

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.

SQLite Access in Objective-C and Cocoa

I am in the process of learning Objective-C for Mac/iPhone development and decided to try and write something useful without looking at the bible (Aaron Hillegass: Cocoa Programming 3rd Edition).
I'm writing a simple puzzle game. The data that defines the levels is stored as a string in a SQLite database and read into a level object with this line of code:
tempLevel.levelData = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
I have two other lines that read in other properties from the database (integers this time, not strings) and they work fine so I am wondering if anyone can help me with the problem.
When this line of code executes I get the following error:
*** +[NSString stringWithUTF8String:]: NULL cString
I would greatly appreciate any help you can give. If you need any more information I would be glad to provide it.
Thanks!
Not really an answer, but there's no good reason to deal with sqlite directly when there are some great wrappers available:
http://cocoaheads.byu.edu/resources/sqlite
Most likely it is an error in compiledStatement. As a general point, you should check that you get an actual value from sqlite3_column_text first, since getting back NULL is often the "correct" response to what you ask for.