从另一个班级

时间:2016-08-06 09:36:04

标签: ios swift class

我正在尝试拨打我的数据库,我的用户所在的城市将与Core Location位置相同。我可以成功调用数据库并找到地理位置,我也可以查询city =“nameOfACity”,但我不知道如何调用city = cityFromCoreLocation的数据。

我无法调用我为Geolocation的结果创建的变量,因为它位于另一个类中并显示为IBOutlet。我想知道我是否可以: - 找到一种方法来调用IBOutlet - 或直接从Geocoder函数调用placemark.locality? (目前我不能这样做,因为它也在另一个班级。

我是swift的新手,所以我仍然不知道如何从另一个类调用变量...我读了关于这个主题的主题,但他们不适应当前的情况。

这是我的代码:

调用数据库:

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
}
}

在哪里找到我需要的变量的类:

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
}

}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

修改getAllArticles类中的ModelBD方法,接受city(String)作为参数。

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

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

然后在VC0中,您可以通过该方法传递城市:

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

您只能在获得该位置后致电self.getAllArticles。请勿在{{1​​}}上拨打电话。

viewDidLoad