Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
//
var name = "Suyeol Jeon"
//
let birthyear = 1995
//
name = " "
//
birthyear = 2000
! Cannot assign to value: 'birthyear' is a 'let' constant
var name: String = "Suyeol Jeon"
let birthyear: Int = 1995
var height: Float = 180.1
var name: String = "Suyeol Jeon"
let birthyear: Int = 1995
var height: Float = 180.1
"ABC" String
123 Int
[String] [String: String]
let languages = [
"Swift",
"Objective-C",
"Python",
]
var capitals = [
" ": " ",
" ": " ",
" ": ...
if for switch
for _ in 0..<10 {
print("Hello!")
}
• for i in 0..<10 

i
•
switch age {
case 8..<14:
student = " "
case 14..<17:
student = " "
case 17..<20:
student = " "
default:
student = " "
}
" "
" "
"" // ?
" "
"" // ?
" "
nil //
123
0 //
nil //
var name: String = " "
name = nil
! Nil cannot be assigned to type 'String'
String nil
var name: String? = " "
name = nil
? String
nil
var email: String?
print(email) // nil
email = "devxoul@gmail.com"
print(email) // Optional("dev...com")
String? String
let optional: String? = "Hello"
let required: String = optional
! Value of optional type 'String?' not unwr...
String? String

 

String String?
"A" 10
nil
let optionalName: String? = " "
if let name = optionalName {
print(name)
}
let optionalName: String? = " "
if let name = optionalName {
print(name)
}
let optionalName: String? = " "
if let name = optionalName {
print(name)
}
let optionalName: String? = " "
if let name = optionalName {
print(name)
}
" "
let optionalName: String? = " "
if let name = optionalName {
print(name)
}
let name: String = " "
let optionalName: String? = " "
if let name = optionalName {
print(name) //
}
let optionalName: String? = " "
if let name = optionalName {
print(name) //
}
let optionalName: String?
if let name = optionalName {
print(name)
}
let optionalName: String?
if let name = optionalName {
print(name)
}
let optionalName: String?
if let name = optionalName {
print(name)
}
nil
let optionalName: String?
if let name = optionalName {
print(name)
}
if let name = optionalName,
let age = optionalAge {
print(name, age)
}
if let age = optionalAge
where age >= 20 {
print(age)
}
where


if let arr = optionalArray {
print(arr.isEmpty)
} else {
print(false)
}
print(optionalArray?.isEmpty == true)
print(optionalArray?.isEmpty == true)
print(optionalArray?.isEmpty == true)
print(optionalArray?.isEmpty == true)
print(optionalArray?.isEmpty == true)
print(optionalArray?.isEmpty == true)
let optionalName: String? = " "
print(optionalName) // Optional(" ")
print(optionalName!) //
var email: String! = " "
print(email) //
nil
func hello(name: String, time: Int) -> String {
var string = ""
for _ in 0..<time {
string += "(name) !n"
}
return string
}
func hello(name: String, time: Int) -> String {
var string = ""
for _ in 0..<time {
string += "(name) !n"
}
return string
...
func hello(name: String, time: Int) -> String {
var string = ""
for _ in 0..<time {
string += "(name) !n"
}
return string
...
func hello(name: String, numberOfTimes time: Int) -> Void {
print(time)
}
hello(" ", numberOfTimes: 3)
func hello(name: String, numberOfTimes time: Int) -> Void {
print(time)
}
hello(" ", numberOfTimes: 3)
func hello(withName name: String,
numberOfTimes time: Int) -> Void {
print(time)
}
hello(withName: " ", numberOfTimes: 3)
func hello(withName name: String,
numberOfTimes time: Int) -> Void {
print(time)
}
hello(withName: " ", numberOfTimes: 3)
func hello(name: String, _ time: Int) -> Void {
print(time)
}
hello(" ", 3)
func hello(name: String, time: Int = 1) -> Void {
print(time)
}
hello(" ")
func sum(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sum(1, 2)
sum(3, 4, 5)
func hello(name: String, time: Int) {
func message(name: String) {
return "(name) !"
}
for _ in 0..<time {
print message(n...
func helloGenerator(message: String) -> String -> String {
func hello(name: String) -> String {
return name + message
}
re...
func helloGenerator(message: String) -> String -> String {
func hello(name: String) -> String {
return name + message
}
re...
func helloGenerator(message: String) -> (String, String) -> String {
func hello(firstName: String, lastName: String) -> St...
func helloGenerator(message: String) -> (String, String) -> String {
func hello(firstName: String, lastName: String) -> St...
func helloGenerator(message: String) -> (String, String) -> String {
return { (firstName: String, lastName: String) -> Str...
func helloGenerator(message: String) -> (String, String) -> String {
return { (firstName: String, lastName: String) -> Str...
func helloGenerator(message: String) -> (String, String) -> String {
return { (firstName: String, lastName: String) -> Str...
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName , lastName in
return lastName + fi...
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName, lastName in
return lastName + fir...
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName, lastName in
return lastName + fir...
func helloGenerator(message: String) -> (String, String) -> String {
return {
return $1 + $0 + message
}
}
func helloGenerator(message: String) -> (String, String) -> String {
return {
return $1 + $0 + message
}
}
func helloGenerator(message: String) -> (String, String) -> String {
return { $1 + $0 + message }
}
func helloGenerator(me...
let hello: (String, String) -> String = { $1 + $0 + " !" }
hello(" ", " ")
let hello: ((String, String) -> String)? = nil
hello?(" ", " ")
func manipulateNumber(number: Int,
usingBlock block: Int -> Int) -> Int {
return block(number)
}
manipulateNumber(10, usin...
manipulateNumber(10, usingBlock: {
$0 * 2
})
manipulateNumber(10, usingBlock: { (number: Int) -> Int in
return number * 2
...
let numbers = [1, 3, 2, 6, 7, 5, 8, 4]
let sortedNumbers = numbers.sort { $0 < $1 }
print(sortedNumbers) // [1, 2, 3, 4, 5...
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
let arr2 = arr1.map { $0 * 2 }
// [2, 6, 12, 4, 14, 18]
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
0 1+
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
0 1+ = 1
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
1 3
0 1+ = 1
+
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
1 3
0 1+ = 1
+ = 4
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
1 3
0 1+ = 1
+ = 4
4 6+
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
1 3
0 1+ = 1
+ = 4
4 6+ = 10
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: { $0 + $1 }) // 28
1 3
0 1+ = 1
+ = 4
4 6+ = 10
let arr1 = [1, 3, 6, 2, 7, 9]
arr1.reduce(0, combine: +) // 28
class Dog {
var name: String?
var age: Int?
func simpleDescription() -> String {
return "" (self.name)"
}
}
struct Coffee ...
class Dog {
var name: String?
var age: Int?
init() {
self.age = 0 //
}
}
class Dog {
var name: String //
var age: Int?
init() {
self.age = 0
}
} ! Return from initializer without initializing all...
class Dog {
var name: String = " "
var age: Int?
init() {
self.age = 0
}
}
class Dog: Animal {
var name: String?
var age: Int
override init() {
self.age = 0 //
super.init() //
// `self`
print(self....
struct Hex {
var decimal: Int?
}
struct Hex {
var hexString: String? {
get {
if let decimal = self.decimal {
return String(decimal, radix: 16)
} else {
ret...
struct Hex {
var hexCode: String? {
if let hex = self.hexString {
return "0x" + hex
}
return nil
}
}
struct Hex {
var decimal: Int? {
willSet {
print("(self.decimal) (newValue) .")
}
didSet {
print("(oldValue) (self.decimal...
var coffeeInfo = (" ", 5100)
coffeeInfo // (String, Int)
coffeeInfo.0 //
coffeeInfo.1 // 5100
coffeeInfo.1 = 5100
var namedCoffeeInfo = (coffee: " ",
price: 5100)
namedCoffeeInfo.coffee //
namedCoffeeInfo.price // 5100
namedCoffeeInfo.p...
let (coffee, price) = (" ", 5100)
coffee //
price // 5100
let (_, latteSize, lattePrice) = (" ",
"Venti", 5600)
latteSize ...
func coffeeInfoForName(name: String) -> (name: String, price: Int)? {
let coffeeInfoList: [(name: String, price: Int)] = [...
enum Month: Int {
case January = 1
case February
case March
case April
case May
case June
case July
case August
case Septe...
enum Month: Int {
case ...
func simpleDescription() -> String {
switch self {
case .January:
return "1 "
case .February:
r...
let december = Month.December
print(december.simpleDescription()) // 12
print(december.rawValue) // 12
let october = Month(rawValue: 10)
print(october) // Optional(Month.October)
enum IssueState: String {
case Open = "open"
case Closed = "closed"
}
let state = IssueState(rawValue: "open")
print(state...
enum Spoon {
case Dirt
case Bronze
case Silver
case Gold
}
enum Spoon {
case Dirt
case Bronze
case Silver
case Gold
}
let spoon: Spoon = .Gold
func doSomething(spoon: Spoon) {
// .....
enum Error {
case InvalidParameter(String, String)
case Timeout
}
let error = Error.InvalidParameter(
"email",
" ."
)
if case .InvalidParameter(let field, let message) = error {
print(field) // email
print(message) // .
}
switch error {
case .InvalidParameter(let field, let message):
print(field) // email
print(message) // .
default:
break
}
public enum Optional<Wrapped> {
case None
case Some(Wrapped)
}
let age: Int? = 20
switch age {
case .None: // `nil`
print(" .")
case .Some(let x) where x < 20:
print(" ")
case .Some(let...
/// .
protocol Sendable {
var from: String? { get }
var to: String { get }
func send()
}
struct Mail: Sendable {
var from: String?
var to: String
func send() {
print("Send a mail from (self.from) to (self.to)")
...
func sendAnything<T: Sendable>(sendable: T) {
sendable.send()
}
protocol Messagable {
var message: String? { get }
}
protocol Sendable: Messagable {
// ...
}
let anyNumber: Any = 10
let anyString: Any = "Hi"
let anyInstance: AnyObject = Dog()
extension String {
var length: Int {
return self.characters.count
}
func reverse() -> String {
return self.characters.reve...




protocol Sendable {
var from: String? { get }
var to: String { get }
func send()
}
extension Sendable {
func debug() {
pri...




Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Upcoming SlideShare
Loading in …5
×

Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기

885 views

Published on

소프트웨어 마에스트로 7기 1단계 1차 기초기술 교육 강의 자료

Published in: Software
  • Be the first to comment

Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기

  1. 1. // var name = "Suyeol Jeon" // let birthyear = 1995
  2. 2. // name = " " // birthyear = 2000 ! Cannot assign to value: 'birthyear' is a 'let' constant
  3. 3. var name: String = "Suyeol Jeon" let birthyear: Int = 1995 var height: Float = 180.1
  4. 4. var name: String = "Suyeol Jeon" let birthyear: Int = 1995 var height: Float = 180.1 "ABC" String 123 Int
  5. 5. [String] [String: String] let languages = [ "Swift", "Objective-C", "Python", ] var capitals = [ " ": " ", " ": " ", " ": " ", ]
  6. 6. if for switch
  7. 7. for _ in 0..<10 { print("Hello!") } • for i in 0..<10 
 i •
  8. 8. switch age { case 8..<14: student = " " case 14..<17: student = " " case 17..<20: student = " " default: student = " " }
  9. 9. " "
  10. 10. " " "" // ?
  11. 11. " " "" // ?
  12. 12. " " nil //
  13. 13. 123 0 // nil //
  14. 14. var name: String = " " name = nil ! Nil cannot be assigned to type 'String' String nil
  15. 15. var name: String? = " " name = nil ? String
  16. 16. nil var email: String? print(email) // nil email = "devxoul@gmail.com" print(email) // Optional("dev...com")
  17. 17. String? String let optional: String? = "Hello" let required: String = optional ! Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
  18. 18. String? String 
 
 String String?
  19. 19. "A" 10 nil
  20. 20. let optionalName: String? = " " if let name = optionalName { print(name) }
  21. 21. let optionalName: String? = " " if let name = optionalName { print(name) }
  22. 22. let optionalName: String? = " " if let name = optionalName { print(name) }
  23. 23. let optionalName: String? = " " if let name = optionalName { print(name) } " "
  24. 24. let optionalName: String? = " " if let name = optionalName { print(name) } let name: String = " "
  25. 25. let optionalName: String? = " " if let name = optionalName { print(name) // }
  26. 26. let optionalName: String? = " " if let name = optionalName { print(name) // }
  27. 27. let optionalName: String? if let name = optionalName { print(name) }
  28. 28. let optionalName: String? if let name = optionalName { print(name) }
  29. 29. let optionalName: String? if let name = optionalName { print(name) } nil
  30. 30. let optionalName: String? if let name = optionalName { print(name) }
  31. 31. if let name = optionalName, let age = optionalAge { print(name, age) }
  32. 32. if let age = optionalAge where age >= 20 { print(age) } where
  33. 33.
  34. 34. if let arr = optionalArray { print(arr.isEmpty) } else { print(false) }
  35. 35. print(optionalArray?.isEmpty == true)
  36. 36. print(optionalArray?.isEmpty == true)
  37. 37. print(optionalArray?.isEmpty == true)
  38. 38. print(optionalArray?.isEmpty == true)
  39. 39. print(optionalArray?.isEmpty == true)
  40. 40. print(optionalArray?.isEmpty == true)
  41. 41. let optionalName: String? = " " print(optionalName) // Optional(" ") print(optionalName!) //
  42. 42. var email: String! = " " print(email) // nil
  43. 43. func hello(name: String, time: Int) -> String { var string = "" for _ in 0..<time { string += "(name) !n" } return string }
  44. 44. func hello(name: String, time: Int) -> String { var string = "" for _ in 0..<time { string += "(name) !n" } return string } hello(" ", time: 3)
  45. 45. func hello(name: String, time: Int) -> String { var string = "" for _ in 0..<time { string += "(name) !n" } return string } hello(" ", time: 3)
  46. 46. func hello(name: String, numberOfTimes time: Int) -> Void { print(time) } hello(" ", numberOfTimes: 3)
  47. 47. func hello(name: String, numberOfTimes time: Int) -> Void { print(time) } hello(" ", numberOfTimes: 3)
  48. 48. func hello(withName name: String, numberOfTimes time: Int) -> Void { print(time) } hello(withName: " ", numberOfTimes: 3)
  49. 49. func hello(withName name: String, numberOfTimes time: Int) -> Void { print(time) } hello(withName: " ", numberOfTimes: 3)
  50. 50. func hello(name: String, _ time: Int) -> Void { print(time) } hello(" ", 3)
  51. 51. func hello(name: String, time: Int = 1) -> Void { print(time) } hello(" ")
  52. 52. func sum(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } sum(1, 2) sum(3, 4, 5)
  53. 53. func hello(name: String, time: Int) { func message(name: String) { return "(name) !" } for _ in 0..<time { print message(name) } }
  54. 54. func helloGenerator(message: String) -> String -> String { func hello(name: String) -> String { return name + message } return hello } let hello = helloGenerator(" !") hello(" ")
  55. 55. func helloGenerator(message: String) -> String -> String { func hello(name: String) -> String { return name + message } return hello } let hello = helloGenerator(" !") hello(" ")
  56. 56. func helloGenerator(message: String) -> (String, String) -> String { func hello(firstName: String, lastName: String) -> String { return lastName + firstName + message } return hello } let hello = helloGenerator(" !") hello(" ", " ")
  57. 57. func helloGenerator(message: String) -> (String, String) -> String { func hello(firstName: String, lastName: String) -> String { return lastName + firstName + message } return hello } let hello = helloGenerator(" !") hello(" ", " ")
  58. 58. func helloGenerator(message: String) -> (String, String) -> String { return { (firstName: String, lastName: String) -> String in return lastName + firstName + message } } func helloGenerator(message: String) -> (String, String) -> String { func hello(firstName: String, lastName: String) -> String { return lastName + firstName + message } return hello }
  59. 59. func helloGenerator(message: String) -> (String, String) -> String { return { (firstName: String, lastName: String) -> String in return lastName + firstName + message } }
  60. 60. func helloGenerator(message: String) -> (String, String) -> String { return { (firstName: String, lastName: String) -> String in return lastName + firstName + message } }
  61. 61. func helloGenerator(message: String) -> (String, String) -> String { return { firstName , lastName in return lastName + firstName + message } }
  62. 62. func helloGenerator(message: String) -> (String, String) -> String { return { firstName, lastName in return lastName + firstName + message } }
  63. 63. func helloGenerator(message: String) -> (String, String) -> String { return { firstName, lastName in return lastName + firstName + message } }
  64. 64. func helloGenerator(message: String) -> (String, String) -> String { return { return $1 + $0 + message } }
  65. 65. func helloGenerator(message: String) -> (String, String) -> String { return { return $1 + $0 + message } }
  66. 66. func helloGenerator(message: String) -> (String, String) -> String { return { $1 + $0 + message } } func helloGenerator(message: String) -> (String, String) -> String { return { return $1 + $0 + message } }
  67. 67. let hello: (String, String) -> String = { $1 + $0 + " !" } hello(" ", " ")
  68. 68. let hello: ((String, String) -> String)? = nil hello?(" ", " ")
  69. 69. func manipulateNumber(number: Int, usingBlock block: Int -> Int) -> Int { return block(number) } manipulateNumber(10, usingBlock: { (number: Int) -> Int in return number * 2 })
  70. 70. manipulateNumber(10, usingBlock: { $0 * 2 }) manipulateNumber(10, usingBlock: { (number: Int) -> Int in return number * 2 }) manipulateNumber(10) { $0 * 2 }
  71. 71. let numbers = [1, 3, 2, 6, 7, 5, 8, 4] let sortedNumbers = numbers.sort { $0 < $1 } print(sortedNumbers) // [1, 2, 3, 4, 5, 6, 7, 8] let evens = numbers.filter { $0 % 2 == 0 } print(evens) // [2, 6, 8, 4]
  72. 72. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  73. 73. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  74. 74. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  75. 75. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  76. 76. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  77. 77. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  78. 78. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  79. 79. let arr1 = [1, 3, 6, 2, 7, 9] let arr2 = arr1.map { $0 * 2 } // [2, 6, 12, 4, 14, 18]
  80. 80. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28
  81. 81. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 0 1+
  82. 82. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 0 1+ = 1
  83. 83. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 1 3 0 1+ = 1 +
  84. 84. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 1 3 0 1+ = 1 + = 4
  85. 85. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 1 3 0 1+ = 1 + = 4 4 6+
  86. 86. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 1 3 0 1+ = 1 + = 4 4 6+ = 10
  87. 87. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: { $0 + $1 }) // 28 1 3 0 1+ = 1 + = 4 4 6+ = 10
  88. 88. let arr1 = [1, 3, 6, 2, 7, 9] arr1.reduce(0, combine: +) // 28
  89. 89. class Dog { var name: String? var age: Int? func simpleDescription() -> String { return "" (self.name)" } } struct Coffee { var name: String? var size: String? func simpleDescription() -> String { return "☕ (self.name)" } }
  90. 90. class Dog { var name: String? var age: Int? init() { self.age = 0 // } }
  91. 91. class Dog { var name: String // var age: Int? init() { self.age = 0 } } ! Return from initializer without initializing all stored properties
  92. 92. class Dog { var name: String = " " var age: Int? init() { self.age = 0 } }
  93. 93. class Dog: Animal { var name: String? var age: Int override init() { self.age = 0 // super.init() // // `self` print(self.simpleDescription()) } func simpleDescription() -> String { return "" (self.name)" } }
  94. 94. struct Hex { var decimal: Int? }
  95. 95. struct Hex { var hexString: String? { get { if let decimal = self.decimal { return String(decimal, radix: 16) } else { return nil } } set { if let newValue = newValue { self.decimal = Int(newValue, radix: 16) } else { self.decimal = nil } } } }
  96. 96. struct Hex { var hexCode: String? { if let hex = self.hexString { return "0x" + hex } return nil } }
  97. 97. struct Hex { var decimal: Int? { willSet { print("(self.decimal) (newValue) .") } didSet { print("(oldValue) (self.decimal) .") } } }
  98. 98. var coffeeInfo = (" ", 5100) coffeeInfo // (String, Int) coffeeInfo.0 // coffeeInfo.1 // 5100 coffeeInfo.1 = 5100
  99. 99. var namedCoffeeInfo = (coffee: " ", price: 5100) namedCoffeeInfo.coffee // namedCoffeeInfo.price // 5100 namedCoffeeInfo.price = 5100
  100. 100. let (coffee, price) = (" ", 5100) coffee // price // 5100 let (_, latteSize, lattePrice) = (" ", "Venti", 5600) latteSize // Venti lattePrice // 5600
  101. 101. func coffeeInfoForName(name: String) -> (name: String, price: Int)? { let coffeeInfoList: [(name: String, price: Int)] = [ (" ", 5100), (" ", 5600), ] for coffeeInfo in coffeeInfoList { if coffeeInfo.name == name { return coffeeInfo } } return nil } coffeeInfoForName(" ")?.price // 5100 coffeeInfoForName(" ")?.price // nil
  102. 102. enum Month: Int { case January = 1 case February case March case April case May case June case July case August case September case October case November case December }
  103. 103. enum Month: Int { case ... func simpleDescription() -> String { switch self { case .January: return "1 " case .February: return "2 " case ... return "..." case .December: return "12 " } } }
  104. 104. let december = Month.December print(december.simpleDescription()) // 12 print(december.rawValue) // 12
  105. 105. let october = Month(rawValue: 10) print(october) // Optional(Month.October)
  106. 106. enum IssueState: String { case Open = "open" case Closed = "closed" } let state = IssueState(rawValue: "open") print(state) // Optional(IssueState.Open)
  107. 107. enum Spoon { case Dirt case Bronze case Silver case Gold }
  108. 108. enum Spoon { case Dirt case Bronze case Silver case Gold } let spoon: Spoon = .Gold func doSomething(spoon: Spoon) { // ... } doSomething(.Silver)
  109. 109. enum Error { case InvalidParameter(String, String) case Timeout } let error = Error.InvalidParameter( "email", " ." )
  110. 110. if case .InvalidParameter(let field, let message) = error { print(field) // email print(message) // . }
  111. 111. switch error { case .InvalidParameter(let field, let message): print(field) // email print(message) // . default: break }
  112. 112. public enum Optional<Wrapped> { case None case Some(Wrapped) }
  113. 113. let age: Int? = 20 switch age { case .None: // `nil` print(" .") case .Some(let x) where x < 20: print(" ") case .Some(let x) where x < 65: print(" ") default: print(" ") }
  114. 114. /// . protocol Sendable { var from: String? { get } var to: String { get } func send() }
  115. 115. struct Mail: Sendable { var from: String? var to: String func send() { print("Send a mail from (self.from) to (self.to)") } }
  116. 116. func sendAnything<T: Sendable>(sendable: T) { sendable.send() }
  117. 117. protocol Messagable { var message: String? { get } } protocol Sendable: Messagable { // ... }
  118. 118. let anyNumber: Any = 10 let anyString: Any = "Hi" let anyInstance: AnyObject = Dog()
  119. 119. extension String { var length: Int { return self.characters.count } func reverse() -> String { return self.characters.reverse() .map { String($0) } .joinWithSeparator("") } }
  120. 120. 
 

  121. 121. protocol Sendable { var from: String? { get } var to: String { get } func send() } extension Sendable { func debug() { print("(self.from) -> (self.to)") } }
  122. 122. 
 


×