Swift find all occurrences of a substring

You just keep advancing the search range until you can’t find any more instances of the substring: extension String { func indicesOf(string: String) -> [Int] { var indices = [Int]() var searchStartIndex = self.startIndex while searchStartIndex < self.endIndex, let range = self.range(of: string, range: searchStartIndex..<self.endIndex), !range.isEmpty { let index = distance(from: self.startIndex, to: range.lowerBound) indices.append(index) … Read more

Hide the status bar in ios 9

Swift-3 override var prefersStatusBarHidden: Bool { return true } I got the Information From Here Change func to var Delete () Change -> to : This works because a computed variable has a getter function, so the function you were implementing before simply turns into the getter function 2016 onwards: simple Thing like On your … Read more

Create Directory in Swift 3.0

Please use this code: Swift 5.0, Swift 4.0 And Swift 3.0 let DocumentDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]) let DirPath = DocumentDirectory.appendingPathComponent(“FOLDER_NAME”) do { try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError { print(“Unable to create directory \(error.debugDescription)”) } print(“Dir Path = \(DirPath!)”)

How can I switch views programmatically in a view controller? (Xcode, iPhone)

If you’re in a Navigation Controller: ViewController *viewController = [[ViewController alloc] init]; [self.navigationController pushViewController:viewController animated:YES]; or if you just want to present a new view: ViewController *viewController = [[ViewController alloc] init]; [self presentViewController:viewController animated:YES completion:nil];

Quitting app causes error “Message from debugger: Terminated due to signal 9”

The “terminated due to signal 9” message just means your app was terminated by a SIGKILL signal. The OS sends that signal whenever your app is terminated involuntarily, whether it’s because of memory pressure (or several other reasons not relevant to this discussion), or the user explicitly killing your app by double tapping the Home … Read more

Swift 3 – dynamic vs @objc

A function/variable declared as @objc is accessible from Objective-C, but Swift will continue to access it directly via static or virtual dispatch. This means if the function/variable is swizzled via the Objective-C framework, like what happens when using Key-Value Observing or the various Objective-C APIs to modify classes, calling the method from Swift and Objective-C … Read more

How can I disambiguate a type and a module with the same name?

The type can be disambiguated using the little-known import (class|struct|func|protocol|enum) Module.Symbol syntax. import struct BTree.OrderedSet From this point on, OrderedSet unambiguously refers to the one in BTree. If this would still be ambiguous or sub-optimal in some files, you can create a Swift file to rename imports using typealiases: // a.swift import struct BTree.OrderedSet typealias … Read more