Skip to content

Instantly share code, notes, and snippets.

View regularberry's full-sized avatar

Sean Berry regularberry

View GitHub Profile
override fun onBindViewHolder(holder: ViewHolder,
position: Int,
payloads: MutableList<Any>) {
if (!payloads.isEmpty()) {
// Because these updates can be batched,
// there can be multiple payloads for a single bind
when (payloads[0]) {
Payload.FAVORITE_CHANGE -> {
// Change only the "favorite" icon,
// leave background image alone:
@regularberry
regularberry / nil.swift
Last active April 28, 2018 21:53
Not the right nil
var dict: [String: Any?] = ["first": nil, "second": 2]
if let val = dict["first"] {
print(val)
}
// Output: nil
dict["first"] = nil
if let val = dict["first"] {
print(val)
dict["first"] = .some(nil)
if let val = dict["first"] {
print(val)
}
// prints ‘nil’
class MutableClass {
var val: Int?
init(val: Int = 0) {
self.val = val
}
func update(newVal: Int) {
val = nil
val = newVal
func testMutableClass() {
let group = DispatchGroup()
let obj = MutableClass()
for _ in 0...1000 {
group.enter()
DispatchQueue.global().async {
let sleepVal = arc4random() % 1000
usleep(sleepVal)
let poet = PoetFile(list: [], generatorInfo: nil)
print(poet.fileContents)
// OUTPUT
---------
//
//
// Generated by SwiftPoet on 6/29/18
//
let structBuilder = StructSpec.builder(for: "TestStruct")
let fieldBuilder = FieldSpec.builder(for: "myField", type: .StringType, construct: nil)
structBuilder.add(field: fieldBuilder.build())
let poet = PoetFile(list: [structBuilder.build()], generatorInfo: nil)
print(poet.fileContents)
/// OUTPUT
-----------
//
let structBuilder = StructSpec.builder(for: "TestStruct")
let fieldBuilder = FieldSpec.builder(for: "myField", type: .StringType, construct: nil)
structBuilder.add(field: fieldBuilder.build())
let methodBuilder = MethodSpec.builder(for: "doTheThing")
methodBuilder.add(modifier: .Public)
let code = """
print("I'm doing the thing!")
"""
let methodBuilder = MethodSpec.builder(for: "doTheThing")
methodBuilder.add(modifier: .Public)
let code = """
for _ in 0..<howManyTimes {
print("I'm doing the thing!")
}
"""
methodBuilder.add(codeBlock: code.toCodeBlock())
let url = URL(string: "http://0.0.0.0")!
let data = "Secret message".data(using: .utf8)
var request = URLRequest(url: url)
request.httpBody = data
let task = URLSession.shared.dataTask(with: request) { data, response, error in
print("We're all done here")
}
task.resume()