Skip to content

Instantly share code, notes, and snippets.

@damijanracel
damijanracel / Localizable.string
Created October 11, 2018 14:07
Localizable strings
"hello" = "hello";
"bye" = "bye %@";
"progress" = "Finished %d of %d";
@damijanracel
damijanracel / UILabelLocalizationExtension.swift
Created October 9, 2018 11:27
UILabel extension for localization
extension UILabel {
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
private struct AssociatedKeys {
static var localizationKey = "nsh_localizationKey"
}
class LocalizedLabel: UILabel {
var localizationKey: LocalizationKey?
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
}
@damijanracel
damijanracel / StringExtensionForLocalization.swift
Last active October 9, 2018 10:42
String extension for localization
func localized() -> String {
let localizedString = NSLocalizedString(self, comment: "")
return localizedString
}
public enum HttpMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
protocol HttpRequestProtocol {
init(method: HttpMethod, url: String, headers: [String: String], parameters: [String: Any], encoding: Encoding)
func run(completion: @escaping ((HttpResponse) -> Void))
}
struct PetReusableCellDataProvider: ReusableTableViewCellDataProvider {
let pet: Pet
var leftTitle: String? {
return pet.name
}
var leftSubtitle: String? {
return "Owner: " + pet.owner
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableTableViewCell") as? ReusableTableViewCell, indexPath.row < users.count else {
return UITableViewCell()
}
let user = users[indexPath.row]
let dataProvider = UserReusableCellDataProvider(user: user)
cell.set(with: dataProvider)
return cell
}
private func set(image: UIImage?) {
guard let image = image else {
hideImage()
return
}
showImage()
iconImageView.image = image
}
func set(with dataProvider: ReusableTableViewCellDataProvider) {
leftTitleLabel.text = dataProvider.leftTitle
leftSubtitleLabel.text = dataProvider.leftSubtitle
rightTitleLabel.text = dataProvider.rightTitle
rightSubtitleLabel.text = dataProvider.rightSubtitle
set(image: dataProvider.image)
}