Skip to content

Instantly share code, notes, and snippets.

@Kuniwak Kuniwak/SE0176Tests.swift
Last active Jan 25, 2018

Embed
What would you like to do?
SE-0176 の理解を確認するための例題集です
// 問題: コンパイルエラーもしくは実行時警告になる testX 関数をすべて答えてください。
import XCTest
class SE0176PlaygroundTests: XCTestCase {
func test1() {
struct ExampleStruct {
mutating func assignedBy(_ block: () -> ExampleStruct) {
self = block()
}
}
var x = ExampleStruct()
x.assignedBy { ExampleStruct() }
}
func test2() {
struct ExampleStruct {
mutating func assignedBy(_ block: () -> ExampleStruct) {
self = block()
}
}
var x = ExampleStruct()
x.assignedBy { x }
}
func test3() {
struct ExampleStruct {
mutating func assignedBy(_ block: (inout ExampleStruct) -> Void) {
block(&self)
}
}
var x = ExampleStruct()
x.assignedBy { y in y = x }
}
func test4() {
struct ExampleStruct {
var property: String?
mutating func assignedBy(_ block: () -> String?) {
self.property = block()
}
}
var x = ExampleStruct()
x.assignedBy { "Example" }
}
func test5() {
struct ExampleStruct {
var property: String?
mutating func assignedBy(_ block: () -> String?) {
self.property = block()
}
}
var x = ExampleStruct()
x.assignedBy { x.property }
}
func test6() {
var x = 0
x = { x + 1 }()
}
func test7() {
class ExampleClass {
var property: ExampleClass?
func assignedBy(_ block: () -> ExampleClass?) {
self.property = block()
}
}
let x = ExampleClass()
x.assignedBy { x }
}
func test8() {
class ExampleClass {
var property: ExampleClass?
func assignedBy(_ block: (inout ExampleClass?) -> Void) {
block(&self.property)
}
}
let x = ExampleClass()
x.assignedBy { y in y = x }
}
func test9() {
var global = 0
func swapGlobal(with another: Int) {
global = another
}
swapGlobal(with: global)
}
func test10() {
var global = 0
func assignGlobal(to another: inout Int) {
another = global
}
assignGlobal(to: &global)
}
func test11() {
var global = 0
func swapGlobal(with another: inout Int) {
global = another
}
swapGlobal(with: &global)
}
func test12() {
class ExampleClass {
var property: Int?
func doSomething(_ x: UnsafeMutableRawPointer) {
print(x)
}
}
var x = ExampleClass()
x.doSomething(&x)
}
func test13() {
class ExampleClass {
var property: Int?
func doSomething(_ x: UnsafeMutableRawPointer) {
print(x == &self.property)
}
}
var x = ExampleClass()
x.doSomething(&x.property)
}
}
// 答えは https://gist.github.com/Kuniwak/a32883a62a37bf72f351fa3dc9c5aac7 で確認!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.