Swift3 Alamofire SwiftyJSON在founcion之外获取数据

时间:2016-09-28 11:16:33

标签: ios alamofire xcode8 swifty-json

我试图让swfityjson和almo在Xcode 8中工作 就像在本页的帖子中一样:

Showing JSON data on TableView using SwiftyJSON and Alamofire

但是我无法从自己那里得到数据。 这对我有点帮助,但我对计数感到困惑。有人请帮我这个,因为我是swift的新手,并花了一天时间试图了解如何从almo函数中获取行数和数据。

 import UIKit
 import Alamofire
 import SwiftyJSON

class TableViewController: UITableViewController {

var names = [String]()
var rowCount = [Int]()
var tableTitle = [String]()
typealias JSONStandard = [String : AnyObject]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}


func callAlamo(){
    let url = "http://xxxxxx/xxxx/xxxxxx.php"
    //let id = UserDefaults.standard.string(forKey: "UserDefaults")
    Alamofire.request(url).responseJSON{(respones)->Void in

        if let value = respones.result.value{
            let json = JSON(value)
            let rows = json["items"].arrayValue
            for anItem in rows {
                let title: String? = anItem["SupplierName"].stringValue
                self.tableTitle.append(title!)
                 //print(self.tableTitle.count)
            }

        }


    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
    // let  isUserLoggedin = UserDefaults.standard.boll(forKey: "userLogIn")
    let ststus = UserDefaults.standard.string(forKey: "userLogIn")
    if  ststus == "false" {
        self.performSegue(withIdentifier: "loginView", sender: self)
    }else{

        callAlamo()
    }
}



@IBAction func logout(_ sender: AnyObject) {
    UserDefaults.standard.set(false, forKey: "userLogIn")
    UserDefaults.standard.synchronize()
    self.performSegue(withIdentifier: "loginView", sender: self)
}


  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print (tableTitle)
    return self.tableTitle.count
}

}

我无法获取tableTitle.count。谢谢!

2 个答案:

答案 0 :(得分:0)

在viewDidAppear之前调用UITableView Delegate方法。所以你需要在从服务器

获得响应后重新加载UITableView
 DispatchQueue.main.async {
            self.tableView.reloadData()             
        }

在for循环之后。

答案 1 :(得分:-1)

- (UITextInputMode *)textInputMode {
    for (UITextInputMode *inputMode in [UITextInputMode activeInputModes]) {
        if ([[self langFromLocale:[self localeKey]] isEqualToString:[self langFromLocale:inputMode.primaryLanguage]]) {
            return inputMode;
        }
    }

    return [super textInputMode];
}


- (NSString*)localeKey {
    NSArray* lang = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"];
    NSString* currentLang = lang[0];

    return currentLang;
}

- (NSString *)langFromLocale:(NSString *)locale {
    NSRange r = [locale rangeOfString:@"_"];
    if (r.length == 0) r.location = locale.length;

    NSRange r2 = [locale rangeOfString:@"-"];
    if (r2.length == 0) r2.location = locale.length;

    return [[locale substringToIndex:MIN(r.location, r2.location)] lowercaseString];
}