Skip to content

Instantly share code, notes, and snippets.

@jshawl
Created May 6, 2015 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshawl/36e7b88c784c45113889 to your computer and use it in GitHub Desktop.
Save jshawl/36e7b88c784c45113889 to your computer and use it in GitHub Desktop.
//
// Story.swift
// MashItUp
//
// Created by Jesse Shawl on 5/5/15.
// Copyright (c) 2015 Jesse Shawl. All rights reserved.
//
import UIKit
class Story: NSObject {
let identifier: String?
let title: String?
let author: String?
let postDate: NSDate?
let link: NSURL?
let excerpt: String?
let channel: String?
init(dictionary:[String:AnyObject]){
identifier = dictionary["_id"] as? String
title = dictionary["title"] as? String
author = dictionary["author"] as? String
postDate = NSDate()
link = NSURL(string: dictionary["link"] as String)
excerpt = dictionary["excerpt"] as? NSString
channel = dictionary["channel"] as? NSString
}
}
class Provider {
func getAllStories(){
let session = NSURLSession.sharedSession()
let url = NSURL(string:"http://mashable.com/stories.json")!
let task = session.dataTaskWithURL(url) { (data, response, error) in
var error:NSError? = nil
let object = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error) as [String:AnyObject]
self.createStories( object )
}
task.resume()
}
func createStories( dictionary:[String:AnyObject] ) {
if let newStories = dictionary["new"] as? [[String:AnyObject]]{
for story in newStories{
let story = Story( dictionary: story )
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment