Objective-C
Xml parsing and serialization for Mac OS X and iPhone OS
Apple provides the NSArchiver and NSUnachriver for object serialization / deserialization, but this can not handle any custom xml schema. So filling an object structure with the data of any custom xml schema has to be made manually. Since the iPhone developer community is rapidly growing, a lot of newbie programmer are despairing to deal with the available xml parsing possibilities.
The iPhone SDK only provides NSXmlParser for xml parsing, which is more useful to read certain parts of an xml file, than filling a whole object structure, which really is a pain.
The other possibility is the famous libxml library, which is written in ANSI C - not easy to use for someone who starts programming with objective-c and never learned proper C before. Event there are a lot of wrappers available, dealing with xml can be a pain for newbies.
And here my idea takes place. An XmlSerializer library which fills an object structure automatically could makes it a lot easier and increase the app quality for many programmers. My Idea should work like this:
The xml file:
<Test name="Michael" uid="28">
<Adress street="AlphaBetaGammastrasse 1" city="Zürich" postCode="8000" />
<Hobbies>
<Hobby describtion="blabla"/>
<Hobby describtion="blupblup"/>
</Hobbies>
</Test>
The business objects:
@interface Test : NSObject {
NSString *name;
Adress *adress;
NSArray *hobbies;
int uid;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) Adress *adress;
@property (nonatomic, retain) NSArray *hobbies;
@property (nonatomic, readwrite) int uid;
@end
@interface Adress : NSObject {
NSString *street;
NSString *city;
int postCode;
}
@property (nonatomic, copy) NSString *street;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, readwrite) int postCode;
@end
How the xml serializer should work:
XMLSerializer *serializer = [[XMLSerializer alloc] init]; NSData *data = [NSData dataWithContentsOfFile:@"~/Documents/Test.xml"]; Test *test = [serializer deserializeWithData:data];
So I started a project for this idea. If you are interested in, the project can be found here. If you would like to join the project, so don’t hesitate to use the provided mailing list and ask for access.
Debbuging trick for Xcode
If you make an error which throws an exception, its sometimes quite hard to trance the error. Usually you have to set a breakpoint and step thru the code to find the line which throws the exception.
Woudn’t it be better, if the debugger stops automatically at the line of code, which throws the exception? I say yes, such things can make life easier and it is possible, since Xcode provides a feature called symbolic breakpoints.
To achieve this behaviour go to Build/Manage breakpoints/Add symbolic breakpoint and enter objc_exception_throw.
Now create a programmatical error in your code (e.g. try to get an object in a NSArray with a non existing index) and start the debugger (Apple + Y). The debugger will stop exactly on the line of code, where the exception has been thrown.
Project Objective-C
A few years ago, I learned the basics of mac programming with objective-c and cocoa. I was very fascinated about the technology, which implements the object orientated thought similar to SmallTalk and which separates the MVC parts like I’ve never seen before.
Unfortunately, non of my colleagues were interested in mac programming, my university has been entirely within the windows world. So I never could gather a small team for mac open source development, all my projects have been written in java and my objective-c knowledge started to drift away.
Since than, I always wanted to start with objective-c again, but I never had the courage to read the antiqued book. Know with Xcode 3.0, it is very difficult to find some good literature about the newest technology. But in september 2008 a new book shall be released. Objective-C und Cocoa Band 1 describes the newest Xcode 3.0 from the basics to key bindings and there will be a second book fro advanced level.
So I decided to program a cooking and baking recipe management application for my love
If i have the time, I’ll going to post the project state and some small tutorials.