gpt4 book ai didi

ios - swift ios http请求拉取数据并仅显示数据选定的数据值

转载 作者:行者123 更新时间:2023-11-30 11:36:28 24 4
gpt4 key购买 nike

我的代码如下,它使用 alamofire http 请求从 api 中提取数据。使用的API https://jsonplaceholder.typicode.com/posts 。我可以获取所有数据,但我想知道的是,我想获取所有数据,但我只显示集合中的内容,例如标题等于此“sunt aut facere repellat Provident Occaecati excepturi optio reprehenderit”显示只有标题的值等于我上面所说的。例如,有很多名称值,但我只想显示谁的名字:“Dun”。谢谢。

API 示例

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},


var getAllDetail: [[String:Any]] = [[String:Any]]()

@IBOutlet var collectionView: UICollectionView!
@IBAction func signOutButtonIsPressed(_ sender: Any) {
let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.showLoginScreen()
}
@IBOutlet var signoutButton: UIButton!
var items = [Item]()

override func viewDidLoad() {
super.viewDidLoad()

self.signoutButton.layer.cornerRadius = 3.0
demoApi()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.navigationController?.navigationBar.isHidden = true
self.navigationItem.hidesBackButton = true

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return getAllDetail.count
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
cell.nameLabel.text = getTempDetails["title"] as? String ?? "" //titleArray[indexPath.row]
}
return cell
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

// handle tap events
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
print("You selected ID #\( getTempDetails["userId"] as? String ?? "" )!")
}

}

func demoApi() {
Alamofire.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
guard let json = response.result.value as! [[String:Any]]? else{ return}
print("Response \(json)")
for item in json {

self.getAllDetail.append(item)

// if let title = item["title"] as? String {
// self.titleArray.append(title)
// }

}
if !self.getAllDetail.isEmpty{
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
break


case .failure(_):
print("Error")
break

}
}

}

最佳答案

引用您的示例响应:

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

你可以这样使用:-

var dict = response as! NSDictionary

let title = dict.value(forKey: "title")
print(title)


if title == "your text"{
print("Matched")
}else{
print("Not Matched")

}

在您的网络响应中编辑实现:-

示例响应:

    { 
"id": 51,
"name": "uuuuuuuu",
"desc": "ahahah",
"reward": "1.00",
"sched": "2015-01-01T08:00:00+08:00",
"parent": "",
"child": "",
"occurrence": {
"name": "once"
},
"status": "expired",
"date_created": "2018-04-06T11:46:46.131836+08:00",
"date_modified": "2018-04-09T13:16:44.088716+08:00"
}

像这样使用:

    let dictOccurence = dict["occurrence"] as! NSDictionary
let strname = dictOccurence["name"]
print(strname!)


if strname == "your text"{
print("Matched")
}else{
print("Not Matched")

}

关于ios - swift ios http请求拉取数据并仅显示数据选定的数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49716697/

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