Skip to content

Instantly share code, notes, and snippets.

__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("PokeApi")))
@interface AppPokeApi : KotlinBase
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
- (void)getPokemonListSuccess:(AppKotlinUnit *(^)(NSArray<AppPokemonEntry *> *))success failure:(AppKotlinUnit *(^)(AppKotlinThrowable * _Nullable))failure __attribute__((swift_name("getPokemonList(success:failure:)")));
@end;
@hlung
hlung / PokeListViewController.swift
Last active April 29, 2019 10:34
A UIViewController invoking PokeApi
import UIKit
import app
class PokeListViewController: UIViewController {
@IBOutlet var pokemonSprite: UIImageView!
@IBOutlet var pokemonInfo: UILabel!
@IBOutlet var pokeListTableView: UITableView!
internal var pokeList: [PokemonEntry] = []
internal var api = PokeApi()
@hlung
hlung / PokeApi.kt
Last active April 29, 2019 09:26
Pokemon API client in Kotlin
package com.raywenderlich.pokelist
import com.raywenderlich.pokelist.shared.ApplicationDispatcher
import com.raywenderlich.pokelist.shared.Image
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
@hlung
hlung / Pokemon.kt
Created April 29, 2019 09:24
Pokemon model classes in Kotlin
package com.raywenderlich.pokelist
import kotlinx.serialization.Serializable
@Serializable
data class Pokemon(
val name: String,
val url: String
)
@Serializable
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // UIApplicationDelegate expects an optional UIWindow
private var appCoordinator: AppCoordinator!
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
@hlung
hlung / PlaceholderTextView.swift
Created November 27, 2018 10:41
A UITextView subclass with placeholder text support. Swift 4.2
import UIKit
/// A UITextView subclass with placeholder text support.
/// It uses another UILabel to show the placeholder, shown when text is empty.
class PlaceholderTextView: UITextView {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(white: 0.5, alpha: 0.85)
label.backgroundColor = .clear
@hlung
hlung / keyboardHeightObservable.swift
Last active June 11, 2023 11:41 — forked from laurilehmijoki/keyboardHeightObservable.swift
RxSwift Observable on iOS keyboard height. Updated from original. Changelog below.
import RxSwift // Version 3.2.0
import RxCocoa // Version 3.2.0
typealias KeyboardHeightInfo = (CGFloat, TimeInterval)
func keyboardHeight() -> Driver<KeyboardHeightInfo> {
return Observable
.from([
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillShow)
.map { notification -> KeyboardHeightInfo in

haha

  • 123
  • 456
@hlung
hlung / MapValueExample.swift
Last active May 29, 2017 08:36
A Swift 3 example of mapValue(), a map() variant that only converts value of dictionary receiver and returns as a new dictionary instead of array.
//
// MapValueExample.swift
// ViKi
//
// Created by Thongchai Kolyutsakul on 29/5/17.
//
//
import Foundation
import SwiftyJSON
@hlung
hlung / CachedFibonacci.swift
Last active January 11, 2016 15:57
A Swift 2 function that calculates a fibonacci number, then cached it in a dictionary for later use. Max value of `Double` is 1.79769313486232e+308. Note: there is a faster way to calculate this if you need to jump later numbers. It's called "fast doubling", see https://vinayakgarg.wordpress.com/2012/11/07/fastest-way-to-compute-fibonacci-number/
typealias NumType = Double
var calculatedFibs = [NumType: NumType]()
func fib(num: NumType) -> NumType {
guard num > 1 else { // when num is 0, 1, just return 0, 1.
return num
}
if let cached = calculatedFibs[num] { // read cache
return cached