This is a Git repository that I use to store some playgrounds where I practice Swift functionality.
Some exmaples ranging from basic view placement all the way to remaking constraints and updating them based on user interaction within the view. Click on the pictures to see their source code or here to see a more detailed Readme.
Some examples ranging from a basic Table View all the way to a Table View implementing a custom cell with Auto Layout and Async image loading. Click on the pictures to see their source code or here to see a more detailed Readme.
Some examples ranging from a basic Collection View all the way to a Collection View implementing a custom cell with Auto Layout and Async image loading and pagination. Click on the pictures to see their source code or here to see a more detailed Readme.
Practicing networking by starting with basic networking calls and slowly introducing more complex additions such as Result types and Generics
Expand for code block:
func fetchItunesData(completion: @escaping (FeedResponse?, Error?) -> ()) {
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, resp, error) in
if let err = error {
completion(nil, err)
return
}
do {
let albumData = try JSONDecoder().decode(FeedResponse.self, from: data!)
completion(albumData, nil)
} catch let jsonError {
completion(nil, jsonError)
}
}.resume()
}
Expand for code block:
func fetchItunesDataWithResults(completion: @escaping (Result<FeedResponse, Error>) -> ()) {
URLSession.shared.dataTask(with: url) { (data, resp, err) in
if let err = err {
completion(.failure(err))
return
}
...
fetchItunesDataWithResults { (result) in
switch result {
case .success(let data):
guard let resultData = data.feed.results else { return }
resultData.forEach({ (album) in
if let name = album.name {
print(name)
}
})
...
}
Expand for code block:
public func fetchGenericData<T: Decodable>(urlString: String, completion: @escaping ((Result<T, Error>) -> () )) {
...
do {
let dataResponse = try JSONDecoder().decode(T.self, from: data)
completion(.success(dataResponse))
} catch let jsonError {
completion(.failure(jsonError))
}
}
fetchGenericData(urlString: urlString) { (result: Result<FeedResponse, Error>) in
...
Some extensions and helpers I've found to be extremely helpful while developing. All credit given, where necessary, in the playground file.
Expand for code block:
let imageCache = NSCache<AnyObject, AnyObject>()
class CustomImageView: UIImageView {
var imageUrlString: String?
func loadImageUsingUrlString(_ urlString: String) {
let url = URL(string: urlString)
imageUrlString = urlString
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
}
}.resume()
}
}