-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAEStoriesWorried.swift
87 lines (74 loc) · 3.6 KB
/
AEStoriesWorried.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// AEStoriesWorried.swift
// Æffect
//
// Created by Hang Cui on 5/5/15.
// Copyright (c) 2015 Josh O'Steen. All rights reserved.
//
import UIKit
class AEStoriesWorried: NSObject {
var storyWorried = [AEStory]()
func loadHappy(fromURLString: String, completionHandler: (AEStoriesWorried, String?) -> Void) {
storyWorried = [AEStory]()
if let url = NSURL(string: fromURLString) {
let urlRequest = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) -> Void in
if error != nil {
dispatch_async(dispatch_get_main_queue(), {
completionHandler(self, error.localizedDescription)
})
} else {
self.parse(data, completionHandler: completionHandler)
}
})
task.resume()
} else {
dispatch_async(dispatch_get_main_queue(), {
completionHandler(self, "Invalid URL")
})
}
}
func parse(jsonData: NSData, completionHandler: (AEStoriesWorried, String?) -> Void) {
var jsonError: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary {
if (jsonResult.count > 0)
{
if let storyData = jsonResult["response"] as? NSDictionary {
if let docs = storyData["docs"] as? NSArray{
println(docs.count)
if(docs.count == 0){
println("no articles")
}
else{
println("abcde")
for specificStory in docs{
//println(specificStory["anger"])
let article_key = specificStory["article_id"] as? NSString
let title = specificStory["title"] as? NSString
let author = specificStory["author"] as? NSString
let storyURL = specificStory["article_url"] as? NSString
//let emotion = specificStory["emotion"] as? NSString
let content_without_tags = specificStory["content_without_tags"] as? NSString
let pubdate = specificStory["pubdate"] as? NSString
let picture_url = specificStory["picture_url"] as? NSString
let newStory = AEStory(title: title! as String, author: author! as String, pubdate: pubdate! as String, content_without_tags: content_without_tags! as String, picture_url: picture_url! as String, article_url: storyURL! as String);
storyWorried.append(newStory)
}
}
}
}
dispatch_async(dispatch_get_main_queue(), {
completionHandler(self, nil)
})
}
} else {
if let unwrappedError = jsonError {
dispatch_async(dispatch_get_main_queue(), {
completionHandler(self, "\(unwrappedError)")
})
}
}
}
}