Skip to content

Instantly share code, notes, and snippets.

View RinniSwift's full-sized avatar
🔸
Swifting

Rinni Swift RinniSwift

🔸
Swifting
View GitHub Profile
// 1. empty array initialization
let numbers = [Int]()
let nums: [Int] = []
let newNumbers = Array<Int>()
let newNums: Array<Int> = Array()
// 2. preinitializing a fixed number of default values
let repeatSwift = Array<String>(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"]
// or
let repeatSwift = Array(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"]
let array = ["Hello", "people", "of", "the", "world"]
// looping using count property
for ind in 0..<array.count {
print(ind)
}
// 0, 1, 2, 3, 4
// looping using the endIndex property
for ind in 0..<array.endIndex {
// 1.
class CircularBuffer<T> {
var bufferMaxSize: Int
var size = 0
// 2.
var elements = Array<T?>(repeating: nil, count: 8)
// 3.
init(maxSize: Int) {
class CircularBuffer<T> {
// ...
// 1.
var front: T? {
if isEmpty() {
return nil
} else {
// 2.
class CircularBuffer<T> {
// ...
func enqueue(item: T) {
// 1.
if isEmpty() {
elements[bufferMaxSize-1] = item
} else {
func dequeue() -> T? {
// ...
// 1.
if !isEmpty() {
// 2.
let front = elements[bufferMaxSize - size]
elements[bufferMaxSize - size] = nil
size -= 1
class CircularBuffer<T> {
var bufferMaxSize: Int
var size = 0
var elements = Array<T?>(repeating: nil, count: 8)
var front: T? {
if isEmpty() {
return nil
} else {
// 1. empty set initialization
var numbers = Set<Int>()
var nums: Set<Int> = Set()
// 2. empty set initialization with array literal
var numbersSet: Set<Int> = []
// 3. creating set with predefined elements using array literals
var newNums: Set = [1, 2, 3, 4]
var numbers = Set<Int>()
numbers.insert(3) // (true, 3)
numbers.insert(22) // (true, 22)
numbers.insert(22) // (false, 22)
var numbers = Set<Int>(minimumCapacity: 30)
numbers.capacity // 48