How to remove multiple spaces in Strings with Swift 2

In Swift 2, join has become joinWithSeparator and you call it on the array. In filter, isEmpty should be called on the current iteration item $0. To replace whitespaces and newline characters with unique space characters as in your question: extension String { func condenseWhitespace() -> String { let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) return components.filter { … Read more

Swift property observer in protocol extension?

No, this is explicitly disallowed. See Extension: Computed Properties: Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties. Keep in mind that if this were legal, it would add some non-trivial confusion about order of execution. Imagine there were several extensions that added didSet, and … Read more

How to check if a String is an Int in Swift?

String Extension & Computed Property You can also add to String a computed property. The logic inside the computed property is the same described by OOPer extension String { var isInt: Bool { return Int(self) != nil } } Now you can “1”.isInt // true “Hello world”.isInt // false “”.isInt // false

tech