Skip to content

Instantly share code, notes, and snippets.

View akshitzaveri's full-sized avatar

Akshit Zaveri akshitzaveri

View GitHub Profile
enum Status {
case notStarted
case ongoing
case ended
}
enum Team {
case first
case second
}
struct Match {
var score: Int
init(score: Int) {
self.score = score
}
}
func finish(_ match: inout Match) { // Added "inout" keyword
match.score = 100 // Updating the score here changes the score on the original copy
class Match {
var score: Int
init(score: Int) {
self.score = score
}
}
func finish(_ match: Match) {
match.score = 100 // Updating the score here changes the score on the original copy
// Our Original class
class Example {
// Original function
static func start() {
print("Start")
}
}
// Subclassing
// Our Original class
class Example {
// Original function
class func start() {
print("Start")
}
}
// Subclassing
let url = URL(string: "http://jsonplaceholder.typicode.com/posts")!
AF.request(url)
.responseDecodable(of: [Post].self) { (response) in
guard let posts = response.value else { return completion([]) }
completion(posts)
}
let url = URL(string: "http://jsonplaceholder.typicode.com/posts")!
let task = URLSession.shared.dataTask(with: url, completionHandler: { data, response, error in
guard let data = data else { return completion([]) }
do {
let posts = try JSONDecoder().decode([Post].self, from: data)
completion(posts)
} catch {
print(error)
completion([])
}
struct Post {
let userID, id: Int
let title, body: String
}
extension Post: Decodable {
enum CodingKeys: String, CodingKey {
case userID = "userId"
case id, title, body
}
import UIKit
final class TableViewAdapter: NSObject, UITableViewDataSource {
let tableView: UITableView
init(tableView: UITableView) {
self.tableView = tableView
super.init()
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
private var adapter: TableViewAdapter!
override func viewDidLoad() {
super.viewDidLoad()