Théo Fleismaher Théo Fleismaher - 4 months ago 13
Swift Question

Get value from an IBOutlet from another class

I'm trying to make a call to my database where the city of my user will be equal to the location found thanks to Core Location. I can successfully make a call to the database and find the geolocation, I can also make a query where city = "nameOfACity" but I don't figure out how to call data where city = cityFromCoreLocation.

I can't call the variable I created for the result of the Geolocation as it's located in another class and displayed as an IBOutlet. I was wondering if I may :
- find a way to call that IBOutlet
- or call directly the placemark.locality from Geocoder function? (which I can't do for the moment as it is also in another class.

I'm new to swift so I still don't know how to call a variable from a different class... I read topics on this subject but they don't adapt to the current case.

Here is my code :

Call to the data base :

import UIKit

let sharedInstance = ModelBD()

class ModelBD: NSObject {

var database: FMDatabase? = nil

class func getInstance() -> ModelBD {
if (sharedInstance.database == nil) {
sharedInstance.database = FMDatabase(path: Utilities.getPath("bddFrance.sqlite"))
}

return sharedInstance
}


func getAllArticles() -> NSMutableArray {
sharedInstance.database!.open()

let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = \(VC0.myCity)", withArgumentsInArray: nil)
let arrDataArticles : NSMutableArray = NSMutableArray()

if (resultSet != nil) {
while resultSet.next() {
let articlesInfo : ArticlesData = ArticlesData()
articlesInfo.nameArticle = resultSet.stringForColumn("nom")
articlesInfo.cityArticle = resultSet.stringForColumn("ville")
articlesInfo.districtArticle = resultSet.stringForColumn("quartier")
articlesInfo.countryArticle = resultSet.stringForColumn("pays")

print("--")
print("nameArticle \(articlesInfo.nameArticle)")
print("cityArticle \(articlesInfo.cityArticle)")
print("districtArticle \(articlesInfo.districtArticle)")
print("countryArticle \(articlesInfo.countryArticle)")

arrDataArticles.addObject(articlesInfo)
}
}

sharedInstance.database!.close()
return arrDataArticles
}
}


Class where is located the variable that I need :

import UIKit
import CoreLocation

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {

let LocationManager = CLLocationManager()

var arrDataArticles: NSMutableArray!

@IBOutlet weak var myCity: UINavigationItem!

@IBOutlet weak var myTableView: UITableView!

var images = UIImage(named: "avatar")

override func viewDidLoad() {
super.viewDidLoad()

//CoreLocation
self.LocationManager.delegate = self
self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
self.LocationManager.requestWhenInUseAuthorization()
self.LocationManager.startUpdatingLocation()

//Data
self.getAllArticles()
}

//Location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
(placemarks, error) -> Void in

if error != nil {
print("Error")
}

if let pm = placemarks?.first {
self.displayLocationInfo(pm)
}
else {
print("Error : data error")
}

})
}

func displayLocationInfo (placemark: CLPlacemark) {

self.LocationManager.stopUpdatingLocation()

print(placemark.subThoroughfare)
print(placemark.thoroughfare)
print(placemark.locality)
print(placemark.postalCode)
print(placemark.subAdministrativeArea)
print(placemark.administrativeArea)
print(placemark.country)
myCity.title = placemark.locality

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error :" + error.localizedDescription)
}

//Data
func getAllArticles() {
arrDataArticles = NSMutableArray()
arrDataArticles = ModelBD.getInstance().getAllArticles()
myTableView.reloadData()
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let myCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell

let article: ArticlesData = arrDataArticles.objectAtIndex(indexPath.row) as! ArticlesData

myCell.rank.text = "\(indexPath.row + 1)"
myCell.photo.image = images
myCell.name.text = article.nameArticle
myCell.city.text = article.districtArticle

return myCell
}

}


Thanks in advance for your help.

Answer

Modify your getAllArticles method in ModelBD class to accept city (String) as an argument.

func getAllArticlesFromCity(city: String) -> NSMutableArray {
    sharedInstance.database!.open()

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = '\(city)'", withArgumentsInArray: nil)
    ...

Then in your VC0, you can just pass the city in that method:

func getAllArticles() {
    if let city = myCity.title {
      arrDataArticles = NSMutableArray()
      arrDataArticles = ModelBD.getInstance().getAllArticlesFromCity(city)
      myTableView.reloadData()
    } 
}

And you should only call self.getAllArticles after you get the location. Don't call it on viewDidLoad.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  //...
  if let pm = placemarks?.first {
    self.displayLocationInfo(pm)
    self.getAllArticles()
  }
  //...