Skip to content

Instantly share code, notes, and snippets.

class LocalizedLabel: UILabel {
var localizationKey: LocalizationKey?
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
}
@damijanracel
damijanracel / LocalizationKeyEnumParameters.swift
Last active October 15, 2018 10:22
LocalizationKey enum with parameters
enum LocalizationKey {
case hello
case bye(String)
case progress(Int, Int)
var string: String {
switch self {
case .hello:
return "hello".localized()
case .bye(let name):
@damijanracel
damijanracel / LocalizationKeyEnum.swift
Last active October 15, 2018 08:10
Localization key enum
enum LocalizationKey: String {
case hello = "hello"
case bye = "bye"
case progress = "progress"
var string: String {
return rawValue.localized()
}
}
@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)
}