//
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 unwrapped; did you
mean to use '!' or '?'?
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
}
hello(" ", time: 3)
func hello(name: String, time: Int) -> String {
var string = ""
for _ in 0..<time {
string += "(name) !n"
}
return string
}
hello(" ", time: 3)
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(name)
}
}
func helloGenerator(message: String) -> String -> String {
func hello(name: String) -> String {
return name + message
}
return hello
}
let hello = helloGenerator(" !")
hello(" ")
func helloGenerator(message: String) -> String -> String {
func hello(name: String) -> String {
return name + message
}
return hello
}
let hello = helloGenerator(" !")
hello(" ")
func helloGenerator(message: String) -> (String, String) -> String {
func hello(firstName: String, lastName: String) -> String {
return lastName + firstName + message
}
return hello
}
let hello = helloGenerator(" !")
hello(" ", " ")
func helloGenerator(message: String) -> (String, String) -> String {
func hello(firstName: String, lastName: String) -> String {
return lastName + firstName + message
}
return hello
}
let hello = helloGenerator(" !")
hello(" ", " ")
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
}
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 {
return { (firstName: String, lastName: String) -> String in
return lastName + firstName + message
}
}
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName , lastName in
return lastName + firstName + message
}
}
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName, lastName in
return lastName + firstName + message
}
}
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName, lastName in
return lastName + firstName + message
}
}
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(message: String) -> (String, String) -> String {
return {
return $1 + $0 + message
}
}
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, usingBlock: { (number: Int) -> Int in
return number * 2
})
manipulateNumber(10, usingBlock: {
$0 * 2
})
manipulateNumber(10, usingBlock: { (number: Int) -> Int in
return number * 2
})
manipulateNumber(10) {
$0 * 2
}
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]
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 {
var name: String?
var size: String?
func simpleDescription() -> String {
return "☕ (self.name)"
}
}
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 stored properties
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.simpleDescription())
}
func simpleDescription() -> String {
return "" (self.name)"
}
}
struct Hex {
var decimal: Int?
}
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
}
}
}
}
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.price = 5100
let (coffee, price) = (" ", 5100)
coffee //
price // 5100
let (_, latteSize, lattePrice) = (" ",
"Venti", 5600)
latteSize // Venti
lattePrice // 5600
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
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
}
enum Month: Int {
case ...
func simpleDescription() -> String {
switch self {
case .January:
return "1 "
case .February:
return "2 "
case ...
return "..."
case .December:
return "12 "
}
}
}
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) // Optional(IssueState.Open)
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) {
// ...
}
doSomething(.Silver)
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 x) where x < 65:
print(" ")
default:
print(" ")
}
/// .
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.reverse()
.map { String($0) }
.joinWithSeparator("")
}
}




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





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

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