Last active
November 24, 2021 10:18
-
-
Save apple-avadhesh/501874de33954e7bb5efada4a7116933 to your computer and use it in GitHub Desktop.
Test JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BaseModel : Codable { | |
let photos : Photos? | |
let stat : String? | |
enum CodingKeys: String, CodingKey { | |
case photos = "photos" | |
case stat = "stat" | |
} | |
init(from decoder: Decoder) throws { | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
photos = try values.decodeIfPresent(Photos.self, forKey: .photos) | |
stat = try values.decodeIfPresent(String.self, forKey: .stat) | |
} | |
} | |
struct Photo : Codable { | |
let id : String? | |
let owner : String? | |
let secret : String? | |
let server : String? | |
let farm : Int? | |
let title : String? | |
let ispublic : Int? | |
let isfriend : Int? | |
let isfamily : Int? | |
let url_s : String? | |
let height_s : Int? | |
let width_s : Int? | |
enum CodingKeys: String, CodingKey { | |
case id = "id" | |
case owner = "owner" | |
case secret = "secret" | |
case server = "server" | |
case farm = "farm" | |
case title = "title" | |
case ispublic = "ispublic" | |
case isfriend = "isfriend" | |
case isfamily = "isfamily" | |
case url_s = "url_s" | |
case height_s = "height_s" | |
case width_s = "width_s" | |
} | |
init(from decoder: Decoder) throws { | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
id = try values.decodeIfPresent(String.self, forKey: .id) | |
owner = try values.decodeIfPresent(String.self, forKey: .owner) | |
secret = try values.decodeIfPresent(String.self, forKey: .secret) | |
server = try values.decodeIfPresent(String.self, forKey: .server) | |
farm = try values.decodeIfPresent(Int.self, forKey: .farm) | |
title = try values.decodeIfPresent(String.self, forKey: .title) | |
ispublic = try values.decodeIfPresent(Int.self, forKey: .ispublic) | |
isfriend = try values.decodeIfPresent(Int.self, forKey: .isfriend) | |
isfamily = try values.decodeIfPresent(Int.self, forKey: .isfamily) | |
url_s = try values.decodeIfPresent(String.self, forKey: .url_s) | |
height_s = try values.decodeIfPresent(Int.self, forKey: .height_s) | |
width_s = try values.decodeIfPresent(Int.self, forKey: .width_s) | |
} | |
} | |
struct Photos : Codable { | |
let page : Int? | |
let pages : Int? | |
let perpage : Int? | |
let total : Int? | |
let photo : [Photo]? | |
enum CodingKeys: String, CodingKey { | |
case page = "page" | |
case pages = "pages" | |
case perpage = "perpage" | |
case total = "total" | |
case photo = "photo" | |
} | |
init(from decoder: Decoder) throws { | |
let values = try decoder.container(keyedBy: CodingKeys.self) | |
page = try values.decodeIfPresent(Int.self, forKey: .page) | |
pages = try values.decodeIfPresent(Int.self, forKey: .pages) | |
perpage = try values.decodeIfPresent(Int.self, forKey: .perpage) | |
total = try values.decodeIfPresent(Int.self, forKey: .total) | |
photo = try values.decodeIfPresent([Photo].self, forKey: .photo) | |
} | |
} | |
func callAPI() { | |
if let url = URL(string: "https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&extras=url_s&api_key=aabca25d8cd75f676d3a74a72dcebf21&format=json&nojsoncallback=1") { | |
URLSession.shared.dataTask(with: url) { data, response, error in | |
if let data = data { | |
do { | |
let res = try JSONDecoder().decode(BaseModel.self, from: data) | |
print(res.photos?.photo) | |
} catch let error { | |
print(error) | |
} | |
} | |
}.resume() | |
} | |
} | |
callAPI() |
It has similar delegates, the rest should be the same. Or share the link of the repo and show what you've tried so far.
i just have button, textfield, and collectionView, i need to put all this response in the collectionView thats is, i dont have nothing to show because everything i tried is crap
Can u tell me what am i doing wrong?, like i said im trying to put the API response that gives me array, into a UICollectionView.
`
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var images = [AnyObject]()
var myCollectionView:UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 60, height: 60)
myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
myCollectionView!.dataSource = self
myCollectionView!.delegate = self
myCollectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView!.backgroundColor = UIColor.white
self.view.addSubview(myCollectionView!)
callAPI()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
myCell.backgroundColor = UIColor.black
let imageDictionary = self.images[indexPath.row] as! NSDictionary
let imageUrlString = imageDictionary.object(forKey: "thumb") as! String
let imageUrl:NSURL = NSURL(string: imageUrlString)!
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imageUrl as URL)!
let imageView = UIImageView(frame: CGRect(x:0, y:0, width:myCell.frame.size.width, height:myCell.frame.size.height))
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
imageView.image = image
imageView.contentMode = UIView.ContentMode.scaleAspectFit
myCell.addSubview(imageView)
}
}
return myCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
print("User tapped on item \(indexPath.row)")
let imageDictionary = self.images[indexPath.row] as! NSDictionary
let imageUrlString = imageDictionary.object(forKey: "thumb") as! String
print("Image url = \(imageUrlString)")
}
func callAPI() {
if let url = URL(string: "https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&extras=url_s&api_key=aabca25d8cd75f676d3a74a72dcebf21&format=json&nojsoncallback=1") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let res = try JSONDecoder().decode(ManagerForPhoto.self, from: data)
print(res.photos?.photo)
} catch let error {
print(error)
}
}
}.resume()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
`
share the repo link, if you want me to check it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i did go there but its talking about table view, and i want it all in collection view.