How to return value from Swift closure? - ios

I'm new at swift closures.
I have this code and i can't extract the values from the forloop into the arrays outside the closure.
Sorry about this question but i have already searched stackflow but without success in this specific case.
Thanks in advance.
var namesArray: [String] = []
var imagesArray: [String] = []
var timesArray: [String] = []
var placesArray: [String] = []
func getData(){
//DATA SOURCE
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath: cmp.pageId, parameters: ["fields": "events"])
// Send request to Facebook
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil {
// Some error checking here
print(error.debugDescription)
} else
if let pageData = result["events"] {
print(result["events"]!!["data"]!![0]["name"])
if let eventDetails = pageData!["data"] {
// EVENT DETAILS FETCH
for var i = 0; i < eventDetails!.count; i++
{
let fetchedEventName = eventDetails![i]["name"] as? String!
let fetchedEventTime = eventDetails![i]["start_time"] as? String!
if eventDetails?[i]["place"]??["name"] != nil {
if let fetchedEventPlace = eventDetails?[i]["place"]??["name"] {
self.placesArray.append(fetchedEventPlace! as! String)
}
} else {
self.placesArray.append("Lisbon")
}
self.namesArray.append(fetchedEventName!)
self.timesArray.append(fetchedEventTime!)
}
print("Name of event: \(self.namesArray)")
}
} else {
print("Error.")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
getData()
}
EDIT: I want to show the fetched result into a tableview that's already set.
Heres the tableview code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("previewCell") as? PreviewCell {
var img: UIImage!
let url = NSURL(string: imagesArray[indexPath.row])!
if let data = NSData(contentsOfURL: url){
img = UIImage(data: data)
} else {
img = UIImage(named: "ant")
}
cell.configureCell(img, title: namesArray[indexPath.row], time: timesArray[indexPath.row], place: placesArray[indexPath.row])
return cell
} else {
return PreviewCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesArray.count
}
func addNewEvent(name: String, image: String, time: String, place: String)
{
namesArray.append(name)
imagesArray.append(image)
timesArray.append(time)
placesArray.append(place)
}

You are already getting the number in to the arrays so what you need to do is just reload the tableview after the for loop.
//end of for-loop
self.tableView.reloadData()
The reason for this is the asynchronous execution of the request.startWithCompletionHandler. The code inside that block will most likely execute after your tableView already loaded and it therefor needs a reload after the data has been fetched

In case if you need to return data from closure you can define completion on your getData() method like this
func getData(completion:([DataType]) -> Void) {
//process data
let dataArray:DataType = []
completion(dataArray)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
getData { (completionData) -> Void in
// process returned data
}
}
Hope this helps

Related

TableViewController with duplicate items of list

I m building iOS application using swift 4.2 and I have a TableViewController that have a strange behaviour.
This is the code:
import UIKit
class StoreByCategoryViewController: UITableViewController{
#IBOutlet var storeTableView: UITableView!
var categorySelected:CategoryModel?
var listaStore = [StoreModel]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Elenco Partner"
if((categorySelected) != nil && categorySelected?.id != "0"){
//posso chiamare il WS estraendo tutti i negozi
//per categoria
getStoreByCategory(category : self.categorySelected!);
storeTableView.reloadData()
}else{
getStore();
}
storeTableView.delegate = self
storeTableView.dataSource = self
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return listaStore.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return listaStore.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = storeTableView.dequeueReusableCell(withIdentifier:"customCell") as! StoreTableViewCell
let storeModel = listaStore[indexPath.row]
cell.storeAddress.text = storeModel.address
cell.storePhone.text = storeModel.phoneNumber
cell.storeDescription.text = storeModel.description
if(storeModel.imageUrl != ""){
print(storeModel.imageUrl)
let imageUrl:NSURL = NSURL(string: storeModel.imageUrl!)!
print(storeModel.imageUrl)
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imageUrl as URL)!
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
cell.storeImage.image = image
cell.storeImage.contentMode = .scaleToFill
}
}
}
// cell.storeImage.layer.cornerRadius = cell.storeImage.frame.height / 2
return cell
}
func getStoreByCategory(category : CategoryModel){
var params = [
"cat_id" : category.id
]
let postUrl = APIRequest(endPoint: "get_store_by_category")
postUrl.sendRequest(parameters: params as! [String : String]) {
responseObject, error in
guard let responseObject = responseObject, error == nil else {
print(error ?? "Unknown error")
return
}
do{
let messageData = try JSONDecoder().decode(ResponseStoreByCategoryModel.self, from: responseObject)
var array = messageData.result
for store in array {
var imageUrl = ""
if(store.image.count > 0){
imageUrl = store.image[0].image
}
let s = StoreModel(id: "",
description: store.firstName,
imageUrl: imageUrl,
address: store.address,
phoneNumber: store.phone)
self.listaStore.append(s)
}
self.storeTableView.reloadData()
}catch{
print("errore durante la decodifica dei dati")
}
}
}
func getStore(){
var params = [
"" : ""
]
let postUrl = APIRequest(endPoint: "get_store")
postUrl.sendRequest(parameters: params as! [String : String]) {
responseObject, error in
let user = CategoryModel(id: "0",
description: "Tutti",
imageUrl: "")
//self.listaCategorie.append(user)
guard let responseObject = responseObject, error == nil else {
print(error ?? "Unknown error")
return
}
do{
let messageData = try JSONDecoder().decode(ResponseStoreModel.self, from: responseObject)
var array = messageData.result
for store in array {
var imageUrl = ""
if(store.image.count > 0){
imageUrl = store.image[0].image
}
let s = StoreModel(id: "",
description: store.firstName,
imageUrl: imageUrl,
address: store.address,
phoneNumber: store.phone)
self.listaStore.append(s)
}
self.storeTableView.reloadData()
}catch{
print("errore durante la decodifica dei dati")
}
}
}
}
Method getStore, getStoreByCategory, return 9 items, but the method
numberOfSection and the method to populate customCell, are called 3 times. So i have a table with 27 items instead of 9 items.
Looks like you want only 1 section with 9 rows in it. If that's the case, you can return 1 from numberOfSections implementation.
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
The other thing I noticed is - you have a UITableViewController and an #IBOutlet var storeTableView: UITableView! as well. You don't need the outlet, You can use self.tableView for the same.

Swift 3, how to parse first and show it on next TableViewController

I am really new to developing iOS apps. So I also apologize in advance if my coding is not close to being optimal or good written...
I am trying to create fairly simple app only for showing retrieving and showing data, but I hit a bump which I am trying to solve it for past few days, but I need help...
So the whole functionality of the app is like this: when opened(displying viewcontroller with textfield and button), user should enter username and push button. After button is pushed app should then do 2 things in next order:
combine certain URL address with entered username, retrieve data
(what kind of data, depends on given username - I gave few examples
on the bottom of that post) and pass them to next
tableviewcontroller
display tableviewcontroller and show parsed data.
But, this does not happen, what I noticed is, that my app opens new tableviewconotrller first, and after tableviewcontroller is open, it parses data, which causes that my table has no data (but I can see that data has been parsed, using print())
I am using Swift 3.
ViewController with textfield and button for "login":
import UIKit
class ViewController: UIViewController {
var zavodi = [[String]]()
#IBOutlet weak var uporabnik: UITextField!
#IBAction func vstop(_ sender: Any) {
self.parse { (completed) in
if (completed){
let zavodiView = self.storyboard?.instantiateViewController(withIdentifier: "zavodiController") as! ZavodiController
zavodiView.niz = self.uporabnik.text!
zavodiView.zavodi = self.zavodi
self.navigationController?.pushViewController(zavodiView, animated: true)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.title="Vstop"
}
func parse( completion: #escaping (_ completed: Bool)-> () ){
let uporabnikIme = uporabnik.text!
//parsing
let shramba = UserDefaults.standard
let zavodiUrl = "https://oaza.rrc.si/eZaporiMobi/kapStSta/read?pUporabniskoIme="+uporabnikIme;
var zavodiRequest = URLRequest(url: URL(string: zavodiUrl)!)
zavodiRequest.httpMethod = "GET"
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: zavodiRequest) { (data, response, error) in
if (error != nil) {
print("Error")
}
else {
var zavodiTemp = [Zavod]()
do {
let fetchedData = try JSONSerialization.jsonObject(with: data!) as! NSArray
//print(fetchedData)
zavodiTemp.removeAll()
for najdenZavod in fetchedData {
let vsakZavod = najdenZavod as! [String : Any]
let zavodId = vsakZavod["zaiId"] as! Int
let naziv = vsakZavod["kratekNaziv"] as! String
let ureditev = vsakZavod["ureditev"] as! Int
let zasedenost = vsakZavod["zasedenost"] as! String
let kapaciteta = vsakZavod["kapaciteta"] as! Int
let stStanje = vsakZavod["stStanje"] as! Int
let naBegu = vsakZavod["naBegu"] as! Int
let prekinitev = vsakZavod["prekinitev"] as! Int
zavodiTemp.append(Zavod(zavodId: zavodId, naziv: naziv, ureditev: ureditev, zasedenost: zasedenost,kapaciteta: kapaciteta, stStanje: stStanje, naBegu: naBegu, prekinitev: prekinitev))
}
zavodiTemp = zavodiTemp.sorted(by: {$0.ureditev < $1.ureditev})
self.zavodi.removeAll()
for e in zavodiTemp {
var temp = [String]()
temp.append(String(e.zavodId)) //0
temp.append(e.naziv) //1
temp.append(String(e.ureditev)) //2
temp.append(e.zasedenost) //3
temp.append(String(e.kapaciteta)) //4
temp.append(String(e.stStanje)) //5
temp.append(String(e.naBegu)) //6
temp.append(String(e.prekinitev)) //7
self.zavodi.append(temp)
}
let steviloZavodov = self.zavodi.count
shramba.set(self.zavodi, forKey:"zavodi")
shramba.set(steviloZavodov, forKey:"steviloZavodov")
var s = [[String]]()
s = shramba.array(forKey: "zavodi") as! [[String]]
for e in s{
print(e[2]+" "+e[1])
}
}
catch {
print()
}
}
}
task.resume()
completion(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
class Zavod {
var zavodId : Int
var naziv : String
var ureditev : Int
var zasedenost : String
var kapaciteta : Int
var stStanje : Int
var naBegu : Int
var prekinitev : Int
init(zavodId : Int, naziv : String, ureditev : Int, zasedenost : String, kapaciteta : Int, stStanje : Int, naBegu : Int, prekinitev : Int) {
self.zavodId = zavodId
self.naziv = naziv
self.ureditev = ureditev
self.zasedenost = zasedenost
self.kapaciteta = kapaciteta
self.stStanje = stStanje
self.naBegu = naBegu
self.prekinitev = prekinitev
}
}
}
TableViewController where should parsed data be displayed:
import UIKit
class ZavodiController: UITableViewController {
var niz = ""
var zavodi = [[String]]()
override func viewDidLoad() {
super.viewDidLoad()
print(niz)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("število zavodov"+String(self.zavodi.count))
return self.zavodi.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "zavodCelica", for: indexPath) as! ZavodCelica
cell.nazivZavoda.text = self.zavodi[indexPath.row][1]
cell.kapaciteta.text = self.zavodi[indexPath.row][4]
cell.zasedenost.text = self.zavodi[indexPath.row][3]
cell.stStanje.text = self.zavodi[indexPath.row][5]
cell.naBegu.text = self.zavodi[indexPath.row][6]
cell.prekinitev.text = self.zavodi[indexPath.row][7]
return cell
}
}
I also tried to use UserDefaults, but it just seems that tableviewcontroller just simply gets loaded before actual parsing is done.
For usernames, you can use:
-"manj", returns 1 record
-"mref", returns 3 records
-"mmli", returns 14 records
I would really appreciate any help.
There can be a lot of ways to achieve this. Reloading the table is also a fix. But to be very accurate with the data your should wait for the function completion before moving to the next screen. Look at the code
func parse()( completion: #escaping (_ completed: Bool)-> () ){
// parse logic goes in here
// after the processing finishes return true like following
completion(true) // you can also have logic to return failures.
}
This will be called like
self.parse { (completed) in
if (completed){
let zavodiView = storyboard?.instantiateViewController(withIdentifier: "zavodiController") as! ZavodiController
zavodiView.niz = uporabnik.text!
zavodiView.zavodi = self.zavodi
navigationController?.pushViewController(zavodiView, animated: true)
}
}
Afetr appending data to the table array add this
DispatchQueue.main.async {
self.tableview.reloadData()
}

TableView is not loading any JSON data Swift 4

I have spent about three weeks trying to figure this out. I can get the section title to view, but none of the JSON data is showing. When I do a standard "array" contained in the file, it displays.
I have followed every tip and trick out there and I am stuck.
I think this may have something to do with AnyObject and String, but I am missing something. Please see my code below:
import UIKit
import Alamofire
import SwiftyJSON
class UserTableViewCell: UITableViewCell {
#IBOutlet weak var userFirstname: UILabel!
#IBOutlet weak var userLastname: UILabel!
}
class Profile2VC: UITableViewController {
#IBOutlet var userTable: UITableView!
var usertitles = ["First Name", "Last Name", "Email", "Mobile Number"]
var userinfo = [[String:AnyObject]]() //Array of dictionary
override func viewDidLoad() {
super.viewDidLoad()
let defaultValues = UserDefaults.standard
let URL_USER_LOGIN = "https://www.myapp.com/myphp.php"
let userid = "13"
let parameters: Parameters=["id":coolid]
Alamofire.request(URL_USER_LOGIN, method: .get, parameters:
parameters).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
if let userData = swiftyJsonVar["user"].arrayObject {
self.userinfo = userData as! [[String:AnyObject]]
//debugPrint(userData)
}
if self.userinfo.count > 0 {
self.userTable.reloadData()
}
}
}
self.userTable.reloadData()
// Uncomment the following line to preserve selection between
presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the
navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return userinfo.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell",
for: indexPath) as! UserTableViewCell
//let userTitles = usertitles[indexPath.row]
let userInfo = userinfo[indexPath.row]
cell.userFirstname?.text = userInfo["first_name"] as? String
cell.userLastname?.text = userInfo["last_name"] as? String
//cell.imageView?.image = UIImage(named: fruitName)
//cell.textLabel?.text = usertitles[indexPath.row]
return cell
}
}
First of all you need to reload your table view in main queue. Check below code:
DispatchQueue.main.async {
self.userTable.reloadData()
}
And you are reloading it multiple times which is not good so Remove unwanted reload code and you final code will be:
Alamofire.request(URL_USER_LOGIN, method: .get, parameters: parameters).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
if let userData = swiftyJsonVar["user"].arrayObject {
self.userinfo = userData as! [[String:AnyObject]]
//debugPrint(userData)
}
if self.userinfo.count > 0 {
DispatchQueue.main.async {
self.userTable.reloadData()
}
}
}
}
//self.userTable.reloadData() //Remove this code
And once your API call done, Make sure debugPrint(userData) is printing some data and then when you are reloading userTable put a breakpoint in cellForRowAt and confirm that it's calling.
Then if its calling and data is there from server, You are good to go.
But if cellForRowAt method didn't called then you need to check your userTable DataSource and Delegate if it's correctly connected or not.
Try this code :
let API = URL(string:"http://www.myapp.com/myphp.php")
let request = URLRequest(url:API!)
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if let data = data {
if String(data: data, encoding: String.Encoding.utf8) != nil {
let data = data
let json = try? JSONSerialization.jsonObject(with: data, options: [])
let jsonData = json as! [[String:Any]]
DispatchQueue.main.sync {
let user = jsonData.flatMap { $0["user"] as? String }
print(user)
self.annocumentTableView.reloadData()
}
}
}
})
task.resume()

Getting data from pointer column in Parse (iOS)

I have seen the following questions.
Get data from a pointer's row in Parse (ios)
and various other questions but still unable to figure out.
I have a class in Parse called 'Plumber'
As you can see the 'practiceArea' is a pointer to this class called 'PracticeArea' (mind the uppercase P in the class)
So from here I want to extract the corresponding 'title' column value for the corresponding pointer. How can I do this?
This is my code so far
//
// Directory.swift
// plumber_main
//
// Created by James on 13/4/16.
// Copyright © 2016 James. All rights reserved.
//
import UIKit
import Parse
class Directory: UITableViewController {
#IBOutlet var plumbersDirectory: UITableView!
var profImages = [PFFile]()
var plumberName = [String]()
var plumberRate = [NSNumber]()
var plumberPracArea = [PFObject]()
var plumberExp = [String]()
var refresher: UIRefreshControl!
func refresh()
{
let query_one = PFQuery(className: "PracticeArea")
query_one.includeKey("title")
let query = PFQuery(className: "plumber")
query.includeKey("practiceArea")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock(
{
(listll: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(listll!.count) names of the plumbers.")
// Do something with the found objects
if let objects = listll {
for object in objects {
print(object)
self.profImages.append(object["photo"] as! PFFile)
self.plumberName.append(object["name"] as! String)
self.plumberExp.append(object["expLevel"] as! String)
self.plumberPracArea.append(object["practiceArea"] as! PFObject)
print(object ["practiceArea"].objectId)
self.plumberRate.append(object["ratePerHr"] as! NSNumber)
// print(object["plumber_Name"] as! String )
// self.plumbersname.append(object["plumber_Name"] as! String)
//self.lblName.text = object["plumber_Name"] as? String
}
self.plumbersDirectory.reloadData()
}
print(self.plumberName.count)
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
self.tableView.reloadData()
self.refresher.endRefreshing()
})
}
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refrehsh")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
refresh()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return plumberName.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let plumbercell: plumber_Directory_Cell = tableView.dequeueReusableCellWithIdentifier("plumberlistproto") as! plumber_Directory_Cell
plumbercell.name.text = plumberName[indexPath.row]
plumbercell.exp.text = plumberExp[indexPath.row]
plumbercell.pracArea.text = String(plumberPracArea[indexPath.row])
plumbercell.price.text = String (plumberRate[indexPath.row])
profImages[indexPath.row].getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
if imageData != nil {
let image = UIImage(data: imageData!)
plumbercell.mini_image.image = image
}
else
{
print(error)
} }
//cell.textLabel?.text = plumbersname[indexPath.row]
return plumbercell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print(indexPath.row)
}
}
Try this
func refresh()
{
let query = PFQuery(className: "PracticeArea")
query.includeKey("practiceArea")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock( {
(listll: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(listll!.count) names of the plumbers.")
// Do something with the found objects
if let objects = listll {
self.plumberName = objects
} else {
self.plumberName.removeAllObjects()
}
self.plumbersDirectory.reloadData()
print(self.plumberName.count)
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
})
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let plumbercell: plumber_Directory_Cell = tableView.dequeueReusableCellWithIdentifier("plumberlistproto") as! plumber_Directory_Cell
let object = plumberName[indexPath.row]
plumbercell.name.text = object["name"]
plumbercell.exp.text = object["expLevel"]
let practiceArea = object["practiceArea"]
plumbercell.pracArea.text = practiceArea["title"]
plumbercell.pracArea.text = String(plumberPracArea[indexPath.row])
plumbercell.price.text = String (plumberRate[indexPath.row])
profImages[indexPath.row].getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?) -> Void in
if imageData != nil {
let image = UIImage(data: imageData!)
plumbercell.mini_image.image = image
}
else
{
print(error)
} }
//cell.textLabel?.text = plumbersname[indexPath.row]
return plumbercell
}
You were really close and definitely not "doing it all wrong". Since you've already included the key in the query request, when you want the information from that object too, then you just need this
let practiceArea = object["projectArea"] as! PFObject
let title = practiceArea["title"] as? String
you should query the different class if you want to use the pointer with the "includeKey"...
let query: PFQuery = PFQuery(className: "PracticeArea")
query.orderByDescending("createdAt")
query.includeKey("practiceArea")
query.findObjectsInBackgroundWithBlock{
(objects:[PFObject]?, error:NSError?)->Void in
if error == nil{
for object in objects! {
//do stuff with each object
}
}
}

Array doesn't save

Getting a JSON object from a rest web service I get the data from the object and I want to show it in a tableview.
class TableViewController1: UITableViewController {
var nomProduit = ["ok"]
var prixProduit = [""]
var vt1 : String?
var vt2 : String?
var i : Int!
var compteur1:Int!
var resultat1:NSArray?
var x : AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
// \(detectionString)
let str:String = "http://vps43623.ovh.net/yamoinscher/api/products/6194005492077"
let url = NSURL(string: str)!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
self.resultat1 = jsonResult["meme_categorie"] as? NSArray
self.compteur1 = self.resultat1!.count
print(self.compteur1!)
//self.value = (compteur1 as? Int)!
for self.i=0 ; self.i < self.compteur1! ; self.i = self.i+1 {
if let aStatus = self.resultat1![self.i] as? NSDictionary{
self.vt1 = aStatus["libelle_prod"]! as? String
self.nomProduit.append(self.vt1!)
self.vt2 = aStatus["prix"]! as? String
self.prixProduit.append(self.vt2!)
//print(self.nomProduit[self.i])
}
}
} catch {
print("JSON serialization failed")
}
}
}
task.resume()
}
Then My problem is that this array stays nil:
self.prixProduit.append(self.vt2!)
here is the rest of my code
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 17
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! customCell1
// cell.PrixSim.text = nomProduit[indexPath.row]
print(self.nomProduit[0])
return cell
}
First of all use a custom struct for the category objects, it makes things so much easier.
At the beginning of TableViewController1
class TableViewController1: UITableViewController {
declare this struct
struct Produit {
var id : String
var prix : String
var title : String
}
and a data source array (forget all your other properties / variables)
var produits = [Produit]()
In viewDidLoad get the data, populate the data source array and reload the table view on the main thread.
This code uses Swift native collection types
override func viewDidLoad() {
super.viewDidLoad()
// \(detectionString)
let str = "http://vps43623.ovh.net/yamoinscher/api/products/6194005492077"
let url = NSURL(string: str)!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(urlContent, options: [])
if let jsonResult = jsonObject as? [String:AnyObject] {
if let memeCategorie = jsonResult["meme_categorie"] as? [[String:String]] {
for categorie in memeCategorie {
if let prix = categorie["prix"], title = categorie["libelle_prod"], id = categorie["id"] {
self.produits.append(Produit(id: id, prix: prix, title: title))
}
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
} catch {
print("JSON serialization failed", error)
}
} else if let connectionError = error {
print("connection error", connectionError)
}
}
task.resume()
}
In numberOfRowsInSection return the actual number of items rather than a hard-coded number.
You can omit numberOfSectionsInTableView since the default value is 1.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return produits.count
}
In cellForRowAtIndexPath get the item by index path and assign the values to your labels (or whatever). For now the values are just printed out.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! customCell1
let produit = produits[indexPath.row]
print(produit.id, produit.title, produit.prix)
return cell
}
}

Resources