我想做的事:在我的静态单元格中检索我的 JSON 查询的返回标签。
我的问题是什么:我做不到。无法返回单个值来更改我的标签。
我做了什么:首先,我安装了 Alamofire 和 SwiftyJSON 库。其次,我做了我的 PHP 脚本,它产生了正确的 JSON 数据。我在我的故事板中创建了标签,并在我的 UITableViewController 中创建了@IBOutlet UILabel。第三,我做了我的 Alamofire 请求,我可以得到我的整个数据的输出。
我不能做什么:我无法让个人字段替换我的标签。我想从我的 JSON 中检索 [username] [gender] [location] [birthday] [mobilephone] [signature] 并用此返回替换我的标签。但是,例如,当我想检索 [手机] 时,我得到了 nil 结果。
我也认为我的 JSON 有问题,因为它似乎没有返回 Array 而只返回 Dictionary
我的表视图控制器
import UIKit
import Alamofire
import SwiftyJson
class PersonalDetails: UITableViewController {
required init(coder aDecoder: NSCoder) {
println("init PersonalDetails")
super.init(coder: aDecoder)
}
deinit {
println("deinit PersonalDetails")
}
var usersData = [PersonalDetailsData]()
@IBOutlet weak var dataUsername: UILabel!
@IBOutlet weak var dataGender: UILabel!
@IBOutlet weak var dataArea: UILabel!
@IBOutlet weak var dataBirthday: UILabel!
@IBOutlet weak var dataMobilePhone: UILabel!
@IBOutlet weak var dataSignature: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var username = prefs.valueForKey("USERNAME") as NSString
//load and parse the JSON into an array
Alamofire.request(.GET, "http://mywebsite/app/data/jsonpersodata.php", parameters: ["username": username]).responseJSON { (request, response, data, error) in
let swiftyJSONObject = JSON(data!)
if (error != nil)
{
// got an error in getting the data, need to handle it
println("error calling GET usersdata")
println(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if
let userdata = JSON(data)
if let mobilephone: String = userdata [0]["mobilephone"].stringValue {
self.dataMobilePhone.text = mobilephone
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func logoutTapped(sender : UIButton) {
let appDomain = NSBundle.mainBundle().bundleIdentifier
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain!)
self.performSegueWithIdentifier("logout", sender: self)
}
}
我的课
import Foundation
class PersonalDetailsData: NSObject {
var dataUsername:String?
var dataGender:String?
var dataArea:String?
var dataBirthday:String?
var dataMobilePhone:String?
var dataSignature:String?
}
我的 json
[
{
"username": "username1",
"gender": "?",
"location": "??",
"birthday": "1983/01\/16",
"mobilephone": "136777777",
"signature": null
}
]
我的PHP
<?php
header('Content-type: application/json');
/* include db.config.php */
include_once("config.php");
// Get user id
$id = isset($_GET['username']) ? mysql_real_escape_string($_GET['username']) : “”;
if(empty($id)){
$data = array ("result" => 0, "message" => 'Wrong user id');
} else {
// get user data
$sql = mysql_query("SELECT username, gender, location, birthday, mobilephone, signature FROM users WHERE username='$id'");
$data = array ();
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
$row_array['username'] = $row['username'];
$row_array['gender'] = $row['gender'];
$row_array['location'] = $row['location'];
$row_array['birthday'] = $row['birthday'];
$row_array['mobilephone'] = $row['mobilephone'];
$row_array['signature'] = $row['signature'];
//push the values in the array
array_push($data,$row_array);
}
echo json_encode($data);
mysql_close($conn);
/* JSON Response */
}
?>