gpt4 book ai didi

ios - 如何相应地操作解析数据?

转载 作者:行者123 更新时间:2023-11-28 06:52:56 26 4
gpt4 key购买 nike

所以基本上我试图从解析中获取数据并相应地显示数据。

如何查看信息并将信息添加到我的项目的 TableView 中。

我在解析中有一个 reservedList 类,它给出了确认列表的人的用户 ID 和用户名。

我不确定如何在 View Controller 上显示项目。每次我尝试将用户名打印到标签上时,我都会得到一个空白标签。我想知道如何实现 UITableView 来显示我的数据 –

获取错误

"Could not cast value of type '__NSArrayM' (0x103cbb8d8) to 'NSString' (0x10412fb20)." when i print out the items.

我在解析中有一个 reservedCust 类,其中包含 following

this it the format I would like to view the data in

var usernames = [String]()
var userids = [String]()
var times = [String]()
var items = [String]()

@IBOutlet var reserveTime: UILabel!
@IBOutlet var reserveUser: UILabel!
@IBOutlet var reservedItems: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

let reserveList = PFQuery(className: "reservedList")
reserveList.findObjectsInBackgroundWithBlock { (objects, error) -> Void in


if let objects = objects {

self.usernames.removeAll(keepCapacity: true)
self.times.removeAll(keepCapacity: true)
for object in objects {

if let username = object["username"] as? String {

self.usernames.append((object["username"] as? String)!)

}

var reservedUser = object["userid"] as? String

var query = PFQuery(className: "reservedCust")

query.whereKey("userid", equalTo: reservedUser!)

query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let objects = objects {

for object in objects {



if let time = object["time"] as? String {

self.times.append((object["time"] as? String)!)
}
if let item = object["items"] {
self.items.append(object["items"] as! (String))
}






}
print(self.usernames)
print(self.times)
print(self.items)
}




})
}
}



}


// Do any additional setup after loading the view.




}

最佳答案

首先声明两个类变量。

var countOfObjects : Int! // Holds count for query objects from Parse

var allObjects : NSArray! // All queried objects from Parse

比在你的 viewDidLoad()

override func viewDidLoad() {
super.viewDidLoad()

getDataFromParse() // A helper method to fetch data from Parse
// Initialize for countOfObjects to 0 on viewDidLoad
countOfObjects = 0
}

func getDataFromParse() {
let query = PFQuery(className:"YourClassName")

query.findObjectsInBackgroundWithBlock { (object : [PFObject]?, error : NSError?) -> Void in

print(object!.count)

//set count Of Objects
self.countOfObjects = object?.count

// Set allObjects
self.allObjects = object

//Reload TableView after query
self.YouRTableView.reloadData()
}

}

// MARK: - TableView DataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

return countOfObjects
}

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

let cell = tableView.dequeueReusableCellWithIdentifier(YourCellIdentifier, forIndexPath: indexPath) as! YourTableViewCell

if let object = allObjects {
let currentObject = object.objectAtIndex(indexPath.row)
cell.textLabel?.text = currentObject.valueForKey("somekey") // Assign label text from parse.
}

return cell

}

关于ios - 如何相应地操作解析数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34432925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com