#IUO Swift declares result of NSRegularExpression firstMatchInString:options:range: to be non-Optional
| Originator: | kristopherdjohnson | ||
| Number: | rdar://18057640 | Date Originated: | 18-Aug-2014 09:24 PM |
| Status: | Duplicate/18058760 | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6 Beta 6 |
| Classification: | Crash/Hang/Data Loss | Reproducible: | Always |
Summary:
In Xcode 6 beta 6, the Swift interface for NSRegularExpression firstMatchInString:options:range: is declared like this:
func firstMatchInString(string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult
However, it is possible for this method to return nil if no match is found. Therefore, the result should be NSTextCheckingResult? or NSTextCheckingResult!, so that the caller can check for nil.
Steps to Reproduce:
Paste this into a playground:
import Foundation
var error: NSError?
let re = NSRegularExpression.regularExpressionWithPattern("hello", options: NSRegularExpressionOptions(0), error: &error)!
let str: NSString = "hello, world"
let range = NSMakeRange(0, str.length)
let firstMatch = re.firstMatchInString(str, options: NSMatchingOptions(0), range: range)
let str2: NSString = "foo"
let range2 = NSMakeRange(0, str2.length)
let secondMatch = re.firstMatchInString(str2, options: NSMatchingOptions(0), range: range)
Expected Results:
firstMatch should return a valid match, and secondMatch should return nil (because there is no match)
Actual Results:
firstMatch is evaluated properly, but the evaluation of secondMatch causes SIGABRT, presumably due to the attempt to use the nil result
Version:
Xcode 6 beta 6 (6A280e)
Notes:
This worked in Xcode 6 beta 5, where the result type was declared to be Optional
Configuration:
OS X 10.9.4
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!