FACEBOOK NEWSFEED PT 1: Set up UICollectionView Programmatically

[ad_1]
EPISODE DOWNLOAD:

In this episode, let’s build Facebook Newsfeed from scratch all programmatically without using the Storyboard.

You’ll learn:
+ How to set up your app’s view hierarchy
+ Set up UICollectionViewController
+ Create custom UICollectionViewCell
+ Implement UICollectionViewDataSource

EPISODE DOWNLOAD:

*********
ABOUT CODE MASTERY
*********
Code Mastery is hosted by Duc Tran, founder of Developers Academy.

This is his free-style no notes, no teleprompter presentation and live coding broadcast with you guys everyday.

To join Duc’s free courses, register for free at

*********
MEET DUC TRAN
*********

Duc Tran is founder of Developers Academy, one of the world’s leading iOS, Android and Web development trainers.

More than 2,000,000 developers have studied his video trainings; 100,000 developers see his posts each month. Each year, Duc has helped 20,000 plus developers graduate from his online courses or video series.

*********
FREE TRAININGS IN IOS DEVELOPMENT
*********
To subscribe and get free tutorials, courses and weekly content, visit me at:
Connect with Duc on facebook:
Tweet him:
Get daily inspiration:

===========
CODE IN THIS COURSE
===========

class FeedParser: NSObject, XMLParserDelegate
{
// 2
private var rssItems: [RSSItem] = []

private var currentElement = “”
private var currentTitle: String = “” {
didSet {
currentTitle = currentTitle.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
private var currentDescription: String = “” {
didSet {
currentDescription = currentDescription.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}

private var currentPubDate: String = “” {
didSet {
currentPubDate = currentPubDate.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}

private var parserCompletionHandler: (([RSSItem]) – Void)?

// 3
func parseFeed(url: String, completionHandler: (([RSSItem]) – Void)?) – Void
{
self.parserCompletionHandler = completionHandler

let request = URLRequest(url: URL(string: url)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request) { (data, response, error) in
guard let data = data else {
if let error = error {
print(error)
}
return
}

// parse xml data
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
}

task.resume()
}

// MARK: – XML Parser Delegate

// 4
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])
{
// we assign the name of the element to currentElement, if the item tag is found, we reset the temporary variables of title, description and pubdate for later use
currentElement = elementName
if currentElement == “item” {
currentTitle = “”
currentDescription = “”
currentPubDate = “”
}
}

// 5 – when the value of an element is found, this method gets called with a string representation of part of the characters of the current element

func parser(_ parser: XMLParser, foundCharacters string: String)
{
switch currentElement {
case “title”: currentTitle += string
case “description” : currentDescription += string
case “pubDate”: currentPubDate += string
default: break
}
}

// 6 – when we reach the closing tag /item is found, this method gets called
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if elementName == “item” {
let rssItem = RSSItem(title: currentTitle, description: currentDescription, pubDate: currentPubDate)
rssItems += [rssItem]
}
}

// 7 – call this when the parsing complete successfully. we call the parserCompletionHandler so the caller can perform any follow-up actions
func parserDidEndDocument(_ parser: XMLParser) {
parserCompletionHandler?(rssItems)
}

parse xml, xml swift, xml iOS, parse xml ios, swift parse xml, swift xml parsing, swift xml parser, iOS rss news reader, swift rss, swift xml parsing tutorial, duc tran, duc tran iOS, developers academy, mapkit direction, mapkit direction swift, mapkit direction iOS, mapview overlay, direction map swift

Comments

One response to “FACEBOOK NEWSFEED PT 1: Set up UICollectionView Programmatically”

  1. real life 1 Avatar

    after this tutorial can you please show us how to Set up Pinterest newsfeed UICollectionView Programmatically thanks for your work bro

Leave a Reply

Your email address will not be published. Required fields are marked *