Skip to content

Instantly share code, notes, and snippets.

View AppleCEO's full-sized avatar
๐Ÿ’ป

๋„๋ฏธ๋‹‰ AppleCEO

๐Ÿ’ป
View GitHub Profile
let numbers = [10, 83, 95, 100, 92]
// numbers์˜ ์š”์†Œ ์ค‘ ์ง์ˆ˜๋ฅผ ๊ฑธ๋Ÿฌ๋‚ด์–ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
let evenNumbers: [Int] = numbers.filter { (number: Int) -> Bool in
return number % 2 == 0
}
print(evenNumbers)
// [10, 100, 92]
let someNumbers = [10, 8, 7, 1, 9]
// ์ดˆ๊นƒ๊ฐ’์ด 0์ด๊ณ  someNumbers ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.
let sum: Int = someNumbers.reduce(0, { (first: Int, second: Int) -> Int in
//print("\(first) + \(second)") //์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”
return first + second
})
// 35
// ์ดˆ๊นƒ๊ฐ’์ด 5์ด๊ณ  someNumbers ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ฐ’์„ ๋บ๋‹ˆ๋‹ค.
@AppleCEO
AppleCEO / TV.swift
Created May 27, 2019 06:47
Default Initializers Example
struct TV {
var channel: Int = 1
}
var tv = TV()
@AppleCEO
AppleCEO / TV.swift
Created May 27, 2019 07:01
Memberwise Initializers Example
struct TV {
var channel: Int = 1
}
var tv = TV(channel: 15)
@AppleCEO
AppleCEO / TV.swift
Created May 27, 2019 07:07
์‚ฌ์šฉ์ž ์ง€์ • ์ด๋‹ˆ์…œ๋ผ์ด์ €
struct TV {
var channel: Int = 1
init(channel: Int) {
self.channel = channel
}
}
var tv = TV(channel: 15)
@AppleCEO
AppleCEO / TV.swift
Created May 27, 2019 07:11
์‚ฌ์šฉ์ž ์ง€์ • ์ด๋‹ˆ์…œ๋ผ์ด์ € ์˜ˆ์ œ
struct TV {
var channel: Int = 1
var brand: String
init(channel: Int) {
self.channel = channel
self.brand = "LG"
}
}
@AppleCEO
AppleCEO / Animal.swift
Last active May 27, 2019 08:47
์‹คํŒจ ๊ฐ€๋Šฅํ•œ ์ด๋‹ˆ์…œ๋ผ์ด์ € ์˜ˆ์ œ
struct Animal {
let species: String
init?(species: String) {
if species.isEmpty { return nil }
self.species = species
}
}
var animal = Animal(species: "")
@AppleCEO
AppleCEO / TV.swift
Created May 27, 2019 08:55
์ด๋‹ˆ์…œ๋ผ์ด์ € ์œ„์ž„ ์˜ˆ์ œ
struct TV {
var channel: Int = 1
var brand: String
init(channel: Int) {
self.channel = channel
self.brand = "LG"
}
init() {
@AppleCEO
AppleCEO / Radio.swift
Created May 27, 2019 09:02
์ง€์ • ์ด๋‹ˆ์…œ๋ผ์ด์ € ์˜ˆ์ œ
class Radio {
var channel: Double
init(channel: Double) {
self.channel = channel
}
}
let radio = Radio(channel: 89.1)
@AppleCEO
AppleCEO / Radio.swift
Created May 27, 2019 09:06
ํŽธ์˜ ์ด๋‹ˆ์…œ๋ผ์ด์ € ์˜ˆ์ œ
class Radio {
var channel: Double
init(channel: Double) {
self.channel = channel
}
convenience init(){
self.init(channel: 103.5)
}
}