This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict["first"] = .some(nil) | |
if let val = dict["first"] { | |
print(val) | |
} | |
// prints ‘nil’ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MutableClass { | |
var val: Int? | |
init(val: Int = 0) { | |
self.val = val | |
} | |
func update(newVal: Int) { | |
val = nil | |
val = newVal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testMutableClass() { | |
let group = DispatchGroup() | |
let obj = MutableClass() | |
for _ in 0...1000 { | |
group.enter() | |
DispatchQueue.global().async { | |
let sleepVal = arc4random() % 1000 | |
usleep(sleepVal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let poet = PoetFile(list: [], generatorInfo: nil) | |
print(poet.fileContents) | |
// OUTPUT | |
--------- | |
// | |
// | |
// Generated by SwiftPoet on 6/29/18 | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
----------- | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
OlderNewer