Swift JSON Parsing directly into object structure

Parsing JSON in Objective-C was always repetitive work and not straightforward at all.
With Swift and the work of David Owens (https://github.com/owensd/json-swift) I was able to implement a pretty straightforward JSON to Object mapper.

As basis I am gonna use this very small JSON Example:

{
 "employees": [
 {
 "firstName": "John", 
 "lastName": "Doe",
 "age" : 26
 }, 
 
 {
 "firstName" : "Anna", 
 "lastName":"Smith",
 "age" : 30
 },
 {
 "firstName":"Peter", 
 "lastName":"Jones",
 "age" : 45
 }
]}

Next step is to define the object model. I am gonna create two classes: EmployeeContainer and the Employee class.
The EmployeeContainer will contain as Array Employee objects.

The definition is looking like that:

EmployeeContainer.swift

class EmployeeContainer : ROJSONObject {
    required init() {
        super.init();
    }

    required init(jsonData:AnyObject) {
        super.init(jsonData: jsonData)
    }

    lazy var employees:[Employee] = {
        return Value<[Employee]>.getArray(self, key: "employees") as [Employee]
    }()
}

Employee.swift

class Employee : ROJSONObject {

    required init() {
        super.init();
    }
    
    required init(jsonData:AnyObject) {
        super.init(jsonData: jsonData)
    }
    
    var firstname:String {
        return Value.get(self, key: "firstName")
    }
    
    var lastname:String {
        return Value.get(self, key: "lastName")            
    }
    
    var age:Int {
        return Value.get(self, key: "age")
    }
}

So basically this is the object model and directly the mapping of the properties in the JSON file to the properties in the Swift class.
The following line is using my generic method to fetch the JSON value from the JSON Dictionary which was already prepared by the Libary of
David Owens.

Every JSON supported Datatype can be access directly over the generic Value class.

return Value.get(self, key: "firstName")

Then I have create a BaseWebservice class which is asynchronously loading my JSON file and return it in the callback. I am not gonna show
the code of this BaseWebservice class. But you can find it on my git account: https://github.com/prine

The call of the Webservice and actual converting into the Dataobject is done here (in the ViewController):

 
var baseWebservice:BaseWebservice = BaseWebservice();
var urlToJSON = "http://prine.ch/employees.json"

var callbackJSON = {(status:Int, jsonResponse:AnyObject!) -> () in
    var employeeContainer:EmployeeContainer = EmployeeContainer(jsonData: jsonResponse)
    
    println(employeeContainer.employees[0].firstname)
    println(employeeContainer.employees[1].firstname)
    
    for employee in employeeContainer.employees {
        println("Firstname: \(employee.firstname) Lastname: \(employee.lastname) age: \(employee.age)");
    }
}

baseWebservice.get(urlToJSON, callback:callbackJSON)

Which provides the following output:

John
Anna
Firstname: John Lastname: Doe age: 26
Firstname: Anna Lastname: Smith age: 30
Firstname: Peter Lastname: Jones age: 45

The whole code can be found on github under the following link:
https://github.com/prine/ROJSONParser

Wikipedia API Objective-C Library

With this very small library your able to load an article from wikipedia api. At the moment three different methods are provided:

// Fetches an wikipedia article from the wikipedia api  - (NSString *) getWikipediaArticle:(NSString *)name; 
// Returns the HTML page from an wikipedia article search by the name  - (NSString *) getWikipediaHTMLPage:(NSString *)name; 
// Return the Main image of an wikipedia article search by the name 
- (NSString *)getUrlOfMainImage:(NSString *)name;



Here is an example of how you should use the WikipediaHelper class:

WikipediaHelper *wikiHelper = [[WikipediaHelper alloc] init];  
NSString *searchWord = @"Elefant"; NSString *article = [wikiHelper getWikipediaHTMLPage:searchWord];  NSString *htmlSource = [wikiHelper getWikipediaHTMLPage:searchWord]; 
NSString *urlImage = [wikiHelper getUrlOfMainImage:searchWord];

You see the main image of the wikipedia article and in the bottom the loaded webview.

You will find the library and the example project in the following git project:

https://github.com/prine/WikiApiObjectiveC

Website einlesen und nach Suchbegriff durchsuchen

Wenn man eine Webseite nach einem gewissen Wort durchsuchen will, kann man dies wie folgt machen:

NSURL Objekt erzeugen, Content mittels stringWithContentsOfURL Methode auslesen und in die urlContents Variable speichern:
NSString *page = @"http://www.google.ch";
NSURL *url = [NSURL URLWithString:page];
NSString *urlContents=[NSString stringWithContentsOfURL:url];

Mit der Methode rangeOfString wird überprüft, ob der String vorkommt, oder nicht:
NSString *searchString = @"blabla";
NSRange range = [urlContents rangeOfString :searchString];
if (range.location != NSNotFound) {
// String found! Do Something...
}