This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Human { | |
static var name = "Mousa" | |
class var age : Int { | |
return 27 | |
} | |
} | |
// Type Method : we accessed both properties without even creating an instance of Human Class. | |
print(Human.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Human { | |
static var name = "Mousa" | |
class var age : Int { | |
return 27 | |
} | |
} | |
// Type Method : we accessed both properties without even creating an instance of Human Class. | |
print(Human.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
func toDateString() -> String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd" | |
return dateFormatter.string(from: self) | |
} | |
func toTimeString() -> String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "HH:mm:ss" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Double { | |
func rounded(toPlaces places:Int) -> Double { | |
let divisor = pow(10.0, Double(places)) | |
return (self * divisor).rounded() / divisor | |
} | |
} | |
let number = 34.33356 | |
print(number.rounded(toPlaces: 3)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func group<GroupingType: Hashable>(by key: (Iterator.Element) -> GroupingType) -> [[Iterator.Element]] { | |
var groups: [GroupingType: [Iterator.Element]] = [:] | |
var groupsOrder: [GroupingType] = [] | |
forEach { element in | |
let key = key(element) | |
if case nil = groups[key]?.append(element) { | |
groups[key] = [element] | |
groupsOrder.append(key) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
var shuffled: Array { | |
var elements = self | |
return elements.shuffle() | |
} | |
@discardableResult | |
mutating func shuffle() -> Array { | |
let count = self.count | |
indices.lazy.dropLast().forEach { | |
swapAt($0, Int(arc4random_uniform(UInt32(count - $0))) + $0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func toDouble() -> Double? { | |
return NumberFormatter().number(from: self)?.doubleValue | |
} | |
} | |
let number = "34.4" | |
print(number.toDouble()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func strikeThrough() -> NSAttributedString { | |
let attributeString = NSMutableAttributedString(string: self) | |
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length)) | |
return attributeString | |
} | |
} | |
// Usage | |
let hello = "Hello World".strikeThrough() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let firstSetOfNumbers: Set = [7, 5, 10, 6, 12] | |
let secondSetOfNumbers: Set = [5, 7, 9, 11, 13] | |
firstSetOfNumbers.sorted() // returns an ordered Set by value | |
// Output: [5, 6, 7, 10, 12] | |
firstSetOfNumbers.intersection(secondSetOfNumbers).sorted() // returns an intersected sorted set between two sets | |
// Output: [5, 7] | |
firstSetOfNumbers.subtracting(secondSetOfNumbers).sorted() // returns result of subtracted set from another set | |
// Output: [6, 10, 12] | |
secondSetOfNumbers.union(firstSetOfNumbers).sorted() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setOfNumbers.contains(8) // to check if set contains a specific value. |