-
-
Save apple-avadhesh/501874de33954e7bb5efada4a7116933 to your computer and use it in GitHub Desktop.
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() |
I don't understand what's the problem here, it does work on Swift Playground. Put that in a func
if you are trying this on the Xcode project.
yep i tested it with the playground as well something is wrong with my crap ill see what it is, im new to swift and took a big project as starter so i learn alot
Try with the updated source now. It should print the photos
array onto the console.
yep working great just need to implement to UIColletion and good to go :) do u recomment collection? or tableView?
glad it worked, depends on the design though. TableView will do if it's just a list. Use collection if it's a grid layout.
okay cool thank you for all the help
bro can u help me with this? im trying to put it in UIViewColletion, and im newbie to swift, im trying to figure it out but couldn't.
what im trying to do is:
The first screen the user is presented with should host a continuous scrolling list of flickr thumbnails recently loaded to flickr.
When a photo thumbnail is clicked the app should open a new screen displaying the specific flickr photo
check this link out if you're trying to pass data to detail page: https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift
i did go there but its talking about table view, and i want it all in collection view.
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.
its not the problem, its the statments cannot be top level, thats what it shows, i think its file name or something.