Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Alamofire
- import SwiftyJSON
- class MasterViewController: UITableViewController {
- var tableTitle = [String]()
- var tableBody = [String]()
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- getJSON()
- }
- func getJSON(){
- Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
- // checking if result has value
- if let value = Response.result.value {
- let json = JSON(value)
- for anItem in json.array! {
- let title: String? = anItem["Title"].stringValue
- let body: String? = anItem["Body"].stringValue
- self.tableTitle.append(title!)
- self.tableBody.append(body!)
- }
- }
- }
- self.tableView.reloadData()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // Table View Stuff
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.tableTitle.count
- }
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
- // cell config
- cell.title!.text = tableTitle[indexPath.row]
- cell.body!.text = tableBody[indexPath.row]
- return cell
- }
- }
- func getJSON(){
- Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
- // checking if result has value
- if let value = Response.result.value {
- let json = JSON(value)
- for anItem in json.array! {
- let title: String? = anItem["Title"].stringValue
- let body: String? = anItem["Body"].stringValue
- self.tableTitle.append(title!)
- self.tableBody.append(body!)
- }
- dispatch_async(dispatch_get_main_queue()) {
- self.tableView.reloadData()
- }
- }
- }
- }
- import AlamofireImage
- import SwiftyJSON
- class NetworkHandler {
- /// The shared instance to define the singleton.
- static let sharedInstance = RequestManager()
- /**
- Private initializer to create the singleton instance.
- */
- private init() { }
- func getJSON(completionHandler: (json: JSON?, error: NSError?) -> Void) {
- Alamofire.request(.GET, http://announcement.vassy.net/api/AnnouncementAPI/Get/).responseJSON { response in
- switch(response.result) {
- case .Success(let value):
- let json = JSON(value)
- completionHandler(json: json, error: nil)
- case .Failure(let error):
- completionHandler(json: nil, error: error)
- }
- }
- }
- }
- class MasterViewController: UITableViewController {
- var tableTitle = [String]()
- var tableBody = [String]()
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- NetworkHandler.sharedInstance.getJSON { [weak self] (json, error) -> Void in
- // request was made successful
- if error == nil {
- for anItem in json.array! {
- let title: String? = anItem["Title"].stringValue
- let body: String? = anItem["Body"].stringValue
- self.tableTitle.append(title!)
- self.tableBody.append(body!)
- }
- dispatch_async(dispatch_get_main_queue()) {
- self.tableView.reloadData()
- }
- }
- }
- }
- // rest of your code
- }
- make func getJSON() to func getJSON(_ whatUwant2execute : @escaping () -> () )
- getJSON { ... codes ... }
- getJSON(_ whatUwant2execute : @escaping () -> () ) {
- Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
- // checking if result has value
- if let value = Response.result.value {
- let json = JSON(value)
- whatUwant2execute()
- .
- .
- .
- getJSON ({ ... codes ... })
Add Comment
Please, Sign In to add comment