Pattern match if not nil - swift

# Let's say I've got the following typealias
public typealias DateRange = (from: NSDate?, to: NSDate?)
# And use it here
var selectedDateRange: DateRange = (nil, nil)
Can I then use pattern matching to both get the values and unwrap it using pattern matching?
# I can check vs. `nil` like so:
if case (nil, nil) = segmentControl.selectedCustomDateRange {
print("Damn, nil!")
}
# And I want something shorter than
if let from = segmentControl.selectedDateRange.from,
let to = segmentControl.selectedDateRange.to
{
model.setPeriod(from, to: to)
}
Any suggestions?

You can use if with pattern matching and the optional pattern
x?:
if case let (from?, to?) = selectedDateRange {
model.setPeriod(from, to: to)
}

Similar to the above, but different...
guard case let (from?, to?) = selectedDateRange else {
print("Damn, at least one nil!")
return
}
model.setPeriod(from, to: to)}

Related

if let with try? gives optional value [duplicate]

I am using an SQLite library in which queries return optional values as well as can throw errors. I would like to conditionally unwrap the value, or receive nil if it returns an error. I'm not totally sure how to word this, this code will explain, this is what it looks like:
func getSomething() throws -> Value? {
//example function from library, returns optional or throws errors
}
func myFunctionToGetSpecificDate() -> Date? {
if let specificValue = db!.getSomething() {
let returnedValue = specificValue!
// it says I need to force unwrap specificValue,
// shouldn't it be unwrapped already?
let specificDate = Date.init(timeIntervalSinceReferenceDate: TimeInterval(returnedValue))
return time
} else {
return nil
}
}
Is there a way to avoid having to force unwrap there? Prior to updating to Swift3, I wasn't forced to force unwrap here.
The following is the actual code. Just trying to get the latest timestamp from all entries:
func getLastDateWithData() -> Date? {
if let max = try? db!.scalar(eventTable.select(timestamp.max)){
let time = Date.init(timeIntervalSinceReferenceDate: TimeInterval(max!))
// will max ever be nil here? I don't want to force unwrap!
return time
} else {
return nil
}
}
Update: As of Swift 5, try? applied to an optional expression does not add another level of optionality, so that a “simple” optional binding is sufficient. It succeeds if the function did not throw an error and did not return nil. val is then bound to the unwrapped result:
if let val = try? getSomething() {
// ...
}
(Previous answer for Swift ≤ 4:) If a function throws and returns an optional
func getSomething() throws -> Value? { ... }
then try? getSomething() returns a "double optional" of the
type Value?? and you have to unwrap twice:
if let optval = try? getSomething(), let val = optval {
}
Here the first binding let optval = ... succeeds if the function did
not throw, and the second binding let val = optval succeeds
if the return value is not nil.
This can be shortened with case let pattern matching to
if case let val?? = try? getSomething() {
}
where val?? is a shortcut for .some(.some(val)).
I like Martin's answer but wanted to show another option:
if let value = (try? getSomething()) ?? nil {
}
This has the advantage of working outside of if, guard, or switch statements. The type specifier Any? isn't necessary but just included to show that it returns an optional:
let value: Any? = (try? getSomething()) ?? nil

How do I correct this Swift enum?

In the PluralSight course "Swift First Look" there is a section on Enums which appears to be out of date. The code provided is this:
//: Playground - noun: a place where people can play
import UIKit
enum CardType: String {
case AmericanExpress = "AmericanExpress"
case Visa = "Visa"
case Discover = "Discover"
case MasterCard = "MasterCard"
case None = "None"
private func regularExpression() -> NSRegularExpression {
switch self {
case .Visa:
return NSRegularExpression(pattern: "^4[0-9]{12}(?:[0-9]{3})?$", options: nil, error: nil)
case .AmericanExpress:
return NSRegularExpression(pattern: "^4[47][0-9]{13}$", options: nil, error: nil)
case .MasterCard:
return NSRegularExpression(pattern: "^5[1-5][0-9]{14}$", options: nil, error: nil)
case .Discover:
return NSRegularExpression(pattern: "^6(?:011|5[0-9]{2})[0-9]{12}$", options:nil, error:nil)
default:
return NSRegularExpression(pattern: ".*", options:nil, error:nil)
}
}
func isValidFor(cardNumber: String) -> Bool {
let re = self.regularExpression()
let range = NSRange(0..<cardNumber.lengthOfBytes(using: String.Encoding.utf8))
let matches = re.numberOfMatches(in: cardNumber, options: .anchored, range: range)
return matches > 0
}
static let allValues = [Visa, AmericanExpress, Discover, MasterCard]
static func from(cardNumber: String) -> CardType? {
for type in self.allValues {
if type.isValidFor(cardNumber: cardNumber) {
return type
}
}
}
}
CardType.Visa.isValidFor(cardNumber: "4242424242424242")
CardType.Visa.isValidFor(cardNumber: "1234")
CardType.from(cardNumber: "4242424242424242").rawValue
The problem with this is that the return statements in the switch block all have this error reported by the latest version of XCode:
Type of expression is ambiguous without more context
Also, the last line reports this error:
Value of optional type 'CardType?' not unwrapped; did you mean to use '!' or '?'?
How do I correct this and what is the current version of Swift requiring here?
Note: Yes, I'm just starting with Swift.
The NSRegularExpression() initializer has changed. You have to pass [] instead of nil for options (but since that is the default value, you can leave off options altogether), and you have to deal with the fact that it can throw an error. Since you are dealing with static patterns that won't fail, you can call them with try!. In general, that is dangerous because it will crash if an error is returned. With these static patterns, that won't happen (or you'll fix it when it does).
Change:
return NSRegularExpression(pattern: "^4[0-9]{12}(?:[0-9]{3})?$", options: nil, error: nil)
to:
return try! NSRegularExpression(pattern: "^4[0-9]{12}(?:[0-9]{3})?$")
and repeat that for all of the NSRegularExpressions.
For the last line, you just need a ?:
CardType.from(cardNumber: "4242424242424242")?.rawValue
Finally, this function needs a return nil to account for the case when a CardType is not found in the for loop:
static func from(cardNumber: String) -> CardType? {
for type in self.allValues {
if type.isValidFor(cardNumber: cardNumber) {
return type
}
}
return nil
}

Function throws AND returns optional.. possible to conditionally unwrap in one line?

I am using an SQLite library in which queries return optional values as well as can throw errors. I would like to conditionally unwrap the value, or receive nil if it returns an error. I'm not totally sure how to word this, this code will explain, this is what it looks like:
func getSomething() throws -> Value? {
//example function from library, returns optional or throws errors
}
func myFunctionToGetSpecificDate() -> Date? {
if let specificValue = db!.getSomething() {
let returnedValue = specificValue!
// it says I need to force unwrap specificValue,
// shouldn't it be unwrapped already?
let specificDate = Date.init(timeIntervalSinceReferenceDate: TimeInterval(returnedValue))
return time
} else {
return nil
}
}
Is there a way to avoid having to force unwrap there? Prior to updating to Swift3, I wasn't forced to force unwrap here.
The following is the actual code. Just trying to get the latest timestamp from all entries:
func getLastDateWithData() -> Date? {
if let max = try? db!.scalar(eventTable.select(timestamp.max)){
let time = Date.init(timeIntervalSinceReferenceDate: TimeInterval(max!))
// will max ever be nil here? I don't want to force unwrap!
return time
} else {
return nil
}
}
Update: As of Swift 5, try? applied to an optional expression does not add another level of optionality, so that a “simple” optional binding is sufficient. It succeeds if the function did not throw an error and did not return nil. val is then bound to the unwrapped result:
if let val = try? getSomething() {
// ...
}
(Previous answer for Swift ≤ 4:) If a function throws and returns an optional
func getSomething() throws -> Value? { ... }
then try? getSomething() returns a "double optional" of the
type Value?? and you have to unwrap twice:
if let optval = try? getSomething(), let val = optval {
}
Here the first binding let optval = ... succeeds if the function did
not throw, and the second binding let val = optval succeeds
if the return value is not nil.
This can be shortened with case let pattern matching to
if case let val?? = try? getSomething() {
}
where val?? is a shortcut for .some(.some(val)).
I like Martin's answer but wanted to show another option:
if let value = (try? getSomething()) ?? nil {
}
This has the advantage of working outside of if, guard, or switch statements. The type specifier Any? isn't necessary but just included to show that it returns an optional:
let value: Any? = (try? getSomething()) ?? nil

A good way to do optional chaining

I currently do this in my code to cope with optionals...
I do a
fetchedResultController.performFetch(nil)
let results = fetchedResultController.fetchedObjects as [doesnotmatter]
// add all items to server that have no uid
for result in results {
let uid = result.valueForKey("uid") as String?
if uid == nil
{
let name = result.valueForKey("name") as String?
let trainingday = result.valueForKey("trainingdayRel") as Trainingdays?
if let trainingday = trainingday
{
let trainingUID = trainingday.valueForKey("uid") as String?
if let trainingUID = trainingUID
{
let urlstring = "http://XXXX/myGym/addTrainingday.php?apikey=XXXXXX&date=\(date)&appid=47334&exerciseUID=\(exerciseUID)"
let sUrl = urlstring.stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding)
let url = NSURL(string: sUrl!)
// save the received uid in our database
if let dictionary = Dictionary<String, AnyObject>.loadJSONFromWeb(url!)
{
trainingday.setValue(uid, forKey: "uid")
}
self.managedObjectContext!.save(nil)
}
}
}
}
Actually I would also need an "else"-clause for each and every "if let" statement. That seems totally terrible code to me! Is there no better way to do this?
Yes, with switch-case and pattern matching you can achieve this:
var x : SomeOptional?
var y : SomeOptional?
switch (x, y)
{
case (.Some(let x), .Some(let y)): doSomething() // x and y present
case (.None(let x), .Some(let y)): doSomethingElse() // x not present, but y
// And so on for the other combinations
default: break
}
Have a look at this blog post: Swift: Unwrapping Multiple Optionals
Edit (slightly off-topic and opinion-based): this is one of my favorite features in Swift. It also lets you implement FSMs with only few code, which is great.

unwrapping multiple optionals in if statement

I want to unwrap two optionals in one if statement, but the compiler complaints about an expected expression after operator at the password constant.
What could be the reason?
if let email = self.emailField?.text && let password = self.passwordField?.text
{
//do smthg
}
Done in Swift.
Great news. Unwrapping multiple optionals in a single line is now supported in Swift 1.2 (XCode 6.3 beta, released 2/9/15).
No more tuple/switch pattern matching needed. It's actually very close to your original suggested syntax (thanks for listening, Apple!)
if let email = emailField?.text, password = passwordField?.text {
}
Another nice thing is you can also add where for a "guarding condition":
var email: String? = "baz#bar.com"
var name: String? = "foo"
if let n = name, e = email where contains(e, "#") {
println("name and email exist, email has #")
}
Reference: XCode 6.3 Beta Release Notes
Update for Swift 3:
if let email = emailField?.text, let password = passwordField?.text {
}
each variable must now be preceded by a let keyword
How about wrapping the optionals in a tuple and using switch to pattern match?
switch (self.emailField?.text, self.passwordField?.text) {
case let (.Some(email), .Some(password)):
// unwrapped 'email' and 'password' strings available here
default:
break
}
It's definitely a bit noisier, but at least it could also be combined with a where clause as well.
The usage
if let x = y {
}
is not equivalent to
if (let x = y) { // this is actually not allowed
}
"if let" is effectively a two-word keyword, which is equivalent to
if y != nil {
let x = y!
// rest of if let block
}
Before Swift 1.2
Like #James, I've also created an unwrap function, but this one uses the existing if let for control flow, instead of using a closure:
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
default:
return nil
}
}
This can be used like so:
if let (email, password) = unwrap(self.emailField?.text, self.passwordField?.text)
{
// do something
}
From: https://gist.github.com/tomlokhorst/f9a826bf24d16cb5f6a3
Note that if you want to handle more cases (like when one of the two fields is nil), you're better off with a switch statement.
Swift 4
if let suggestions = suggestions, let suggestions1 = suggestions1 {
XCTAssert((suggestions.count > suggestions1.count), "TEST CASE FAILED: suggestion is nil. delete sucessful");
}
I can't explain why the above code doesn't work, but this would be good a replacement:
if let email = self.emailField?.text
{
if let password = self.passwordField?.text
{
//do smthg
}
}
Based on #Joel's answer, I've created a helper method.
func unwrap<T, U>(a:T?, b:U?, handler:((T, U) -> ())?) -> Bool {
switch (a, b) {
case let (.Some(a), .Some(b)):
if handler != nil {
handler!(a, b)
}
return true
default:
return false
}
}
// Usage
unwrap(a, b) {
println("\($0), \($1)")
}