Skip to content
DeveloperMemos

'scanLocation' was deprecated in iOS 13.0

iOS, Swift, Deprecation, iOS 13.02 min read

With each new release, Apple introduces changes and improvements to its iOS platform, including the deprecation of certain features or properties. One such deprecation occurred in iOS 13.0, where the scanLocation property in the NSScanner class was deprecated. This property was widely used for scanning strings and extracting specific information. In this article, we will explore the deprecation of scanLocation and discuss alternative approaches to achieve similar functionality in Swift.

The Deprecation of scanLocation

Prior to iOS 13.0, the scanLocation property in the NSScanner class allowed developers to access the current scan location within a string. It was frequently used in conjunction with the scanUpToCharacters(from: into:) method to extract desired substrings or perform custom parsing operations. However, starting from iOS 13.0, scanLocation was marked as deprecated.

Why Was scanLocation Deprecated?

Apple deprecates certain features or properties to improve the overall developer experience, enhance performance, or introduce more reliable alternatives. Although Apple doesn't provide explicit reasons for deprecations, it is likely that the deprecation of scanLocation was motivated by providing more streamlined and efficient APIs for string parsing in Swift.

Alternative Approaches

If you were relying on scanLocation in your code, you need to find an alternative approach to achieve the same functionality. Thankfully, Swift provides several options to scan strings and extract desired information. Let's explore a couple of alternative approaches.

Approach 1: String Slicing

One straightforward alternative to using scanLocation is to utilize Swift's powerful string slicing capabilities. You can extract substrings based on specific start and end indices using the prefix(_:) and suffix(_:) methods or by creating a Range with startIndex and endIndex. Here's an example:

1let inputString = "Hello, World!"
2let startIndex = inputString.index(inputString.startIndex, offsetBy: 7)
3let endIndex = inputString.index(inputString.endIndex, offsetBy: -1)
4let extractedString = inputString[startIndex..<endIndex]
5print(extractedString) // Output: "World"

In this example, we use the index(_:offsetBy:) method to obtain the desired start and end indices and then create a substring using the slicing syntax.

Approach 2: Regular Expressions

Another powerful technique for string scanning and pattern matching is using regular expressions. Swift provides a convenient API for regular expressions through the NSRegularExpression class. You can define patterns and extract matches using the firstMatch(in:options:range:) method. Here's an example:

1import Foundation
2
3let inputString = "Hello, World!"
4let pattern = "W[a-z]+"
5do {
6 let regex = try NSRegularExpression(pattern: pattern)
7 let range = NSRange(location: 0, length: inputString.utf16.count)
8 if let match = regex.firstMatch(in: inputString, options: [], range: range) {
9 let extractedString = (input String as NSString).substring(with: match.range)
10 print(extractedString) // Output: "World"
11 }
12} catch {
13 print("Error creating regular expression: \(error)")
14}

In this example, we define a pattern that matches a word starting with 'W' followed by lowercase letters. We then use the firstMatch(in:options:range:) method to obtain the first match within the specified range.

In Summary

The deprecation of the scanLocation property in iOS 13.0 removed a commonly used feature for string scanning and extraction. However, Swift provides alternative approaches such as string slicing and regular expressions to achieve similar functionality. By adapting your code to use these alternatives, you can ensure compatibility with the latest iOS versions and take advantage of the enhanced capabilities offered.