Guest User

Untitled

a guest
Nov 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. import UIKit
  2. import Alamofire
  3. import SwiftyJSON
  4.  
  5. class MasterViewController: UITableViewController {
  6.  
  7. var tableTitle = [String]()
  8. var tableBody = [String]()
  9.  
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. // Do any additional setup after loading the view, typically from a nib.
  13.  
  14. getJSON()
  15.  
  16. }
  17.  
  18. func getJSON(){
  19.  
  20. Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
  21.  
  22. // checking if result has value
  23. if let value = Response.result.value {
  24.  
  25. let json = JSON(value)
  26.  
  27. for anItem in json.array! {
  28.  
  29. let title: String? = anItem["Title"].stringValue
  30. let body: String? = anItem["Body"].stringValue
  31. self.tableTitle.append(title!)
  32. self.tableBody.append(body!)
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. self.tableView.reloadData()
  41.  
  42. }
  43.  
  44. override func didReceiveMemoryWarning() {
  45. super.didReceiveMemoryWarning()
  46. // Dispose of any resources that can be recreated.
  47. }
  48.  
  49. // Table View Stuff
  50.  
  51. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  52. return 1
  53. }
  54.  
  55. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  56. return self.tableTitle.count
  57. }
  58.  
  59.  
  60. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  61. let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
  62.  
  63. // cell config
  64. cell.title!.text = tableTitle[indexPath.row]
  65. cell.body!.text = tableBody[indexPath.row]
  66. return cell
  67.  
  68. }
  69.  
  70. }
  71.  
  72. func getJSON(){
  73.  
  74. Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
  75.  
  76. // checking if result has value
  77. if let value = Response.result.value {
  78.  
  79. let json = JSON(value)
  80.  
  81. for anItem in json.array! {
  82.  
  83. let title: String? = anItem["Title"].stringValue
  84. let body: String? = anItem["Body"].stringValue
  85. self.tableTitle.append(title!)
  86. self.tableBody.append(body!)
  87.  
  88. }
  89.  
  90. dispatch_async(dispatch_get_main_queue()) {
  91. self.tableView.reloadData()
  92. }
  93.  
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. import AlamofireImage
  101. import SwiftyJSON
  102.  
  103. class NetworkHandler {
  104.  
  105. /// The shared instance to define the singleton.
  106. static let sharedInstance = RequestManager()
  107.  
  108.  
  109. /**
  110. Private initializer to create the singleton instance.
  111. */
  112. private init() { }
  113.  
  114. func getJSON(completionHandler: (json: JSON?, error: NSError?) -> Void) {
  115.  
  116. Alamofire.request(.GET, http://announcement.vassy.net/api/AnnouncementAPI/Get/).responseJSON { response in
  117.  
  118. switch(response.result) {
  119. case .Success(let value):
  120.  
  121. let json = JSON(value)
  122. completionHandler(json: json, error: nil)
  123.  
  124. case .Failure(let error):
  125. completionHandler(json: nil, error: error)
  126. }
  127. }
  128. }
  129. }
  130.  
  131. class MasterViewController: UITableViewController {
  132.  
  133. var tableTitle = [String]()
  134. var tableBody = [String]()
  135.  
  136. override func viewDidLoad() {
  137. super.viewDidLoad()
  138. // Do any additional setup after loading the view, typically from a nib.
  139.  
  140. NetworkHandler.sharedInstance.getJSON { [weak self] (json, error) -> Void in
  141.  
  142. // request was made successful
  143. if error == nil {
  144. for anItem in json.array! {
  145. let title: String? = anItem["Title"].stringValue
  146. let body: String? = anItem["Body"].stringValue
  147. self.tableTitle.append(title!)
  148. self.tableBody.append(body!)
  149. }
  150.  
  151. dispatch_async(dispatch_get_main_queue()) {
  152. self.tableView.reloadData()
  153. }
  154. }
  155. }
  156. }
  157.  
  158. // rest of your code
  159. }
  160.  
  161. make func getJSON() to func getJSON(_ whatUwant2execute : @escaping () -> () )
  162.  
  163. getJSON { ... codes ... }
  164.  
  165. getJSON(_ whatUwant2execute : @escaping () -> () ) {
  166.  
  167. Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
  168.  
  169. // checking if result has value
  170. if let value = Response.result.value {
  171.  
  172. let json = JSON(value)
  173. whatUwant2execute()
  174. .
  175. .
  176. .
  177.  
  178. getJSON ({ ... codes ... })
Add Comment
Please, Sign In to add comment