0

I am Doing this code. How to solve this problem after this code.

import Foundation
import SwiftyJSON
import Alamofire

class SymptomFieldsName {

var name: String?
var slug: String?
var msp_enabled: Bool?
var results: Array<String>?


required init(json: JSON, id: Int?) {

    self.name = json[RecordFields.Name.rawValue].stringValue
    self.slug = json[RecordFields.Slug.rawValue].stringValue
    self.msp_enabled = json[RecordFields.Mspenabled.rawValue].boolValue

    if let jsonArray = json[RecordFields.Results.rawValue].array
    {

        self.results = Array<String>()
        for entry in jsonArray {
            self.results?.append(entry.stringValue)
        }
    }
}

// MARK: Endpoints
func endpointForSpecies() -> String 
{
    return "http://52.74.163.60/api/symptoms_list/"
}

func getSpeciesAtPath( path: String, completionHandler: (SymptomName?, NSError?) -> Void)  {
    Alamofire.request(.GET, path)
        .responseSpeciesArray { (request, response, symptomName, error) in
            if let anError = error
            {
                completionHandler(nil, error)
                return
            }
            completionHandler(symptomName, nil)
    }
}
3
  • Where is your code for UITableViewCell? Commented Oct 13, 2015 at 11:55
  • @IBOutlet weak var tableview: UITableView! func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } Commented Oct 14, 2015 at 7:16
  • I want to fetch data in UITableView from Api in swift. so plg require code Commented Oct 14, 2015 at 7:19

2 Answers 2

1

Use NSJSONSerialization

let request = NSURLRequest(URL: <# JSON file URL #>)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(request) {

(data, response, error) -> Void in

if error == nil{
    jsonArray = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! [NSDictionary]

   // That way the entire JSON file is stored in an array of Dictionaries which can be accessed with key : value pairs

        }
    }
    task.resume()
Sign up to request clarification or add additional context in comments.

Comments

0

You can try the following detail explaination:

import Foundation
import SwiftyJSON
import Alamofire

class SymptomFieldsName {

 let path = "http://52.74.163.60/api/symptoms_list/"
  var ArrayOfString : [String] = []  // array of String data type.
  @IBOutlet weak var tableview: UITableView!      

  func getSpeciesAtPath( path: String) -> Void)  {

 //Step 2: Alamofire request call.
    Alamofire.Manager.sharedInstance.request(.GET, path, parameters: nil,  encoding: .JSON).responseJSON()
            {
                (request, responce, result) in

                if(result.isSuccess)
                {

                    //Step 3: We have used Swifty JSON to convert result into json array.
                    let json1 = JSON(result.value!) 

                   self.ArrayOfString = json1["stringvalue"].stringValue


                 }
          } //Alamofire ends here
     }//Function ends here


 //MARK: UITableViewDataSource Function

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return ArrayOfString.count
    }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

       print(indexPath.row) // Here you will get elements in `ArrayOfString` array
       return cell
    }

   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     print(indexPath) // This will return selected row index.
    }





 }//Class ends here

ArrayOfString contains your resultant string values. Hope this detail explanation will work for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.