Swift 2: Expression Pattern of Type 'Bool' Cannot Match Values of Type 'Int'

Swift 2: expression pattern of type 'Bool' cannot match values of type 'Int'

You can use case let where and check if both match before checking them individually:

func fizzBuzz(n: Int) -> String {
let result: String
switch n {
case let n where n.isMultiple(of: 3) && n.isMultiple(of: 5):
result = "FizzBuzz"
case let n where n.isMultiple(of: 3):
result = "Fizz"
case let n where n.isMultiple(of: 5):
result = "Buzz"
default:
result = "none"
}
print("n:", n, "result:", result)
return result
}

Expression pattern of type Int cannot match values of type (int) - Int

Just as the error suggests, IRDComparisonTerm is a function that takes an Int parameter and returns an Int parameter ((Int) -> Int).

You forgot to call the function.

expression pattern of type 'Int' cannot match values of type 'uint?' (aka 'Optional UInt32 ')

Sounds like report.level is an optional value. You need to unwrap it or you need your switch to have optional patterns.

Unwrapping it would look like this:

class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
guard let level = report.level else {
return
}
switch level {
case 1:
// something
case 2:
// something
case 3:
// something
case 4:
// something
default:
return
}
}

Matching optionals would look like this:

class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
switch report.level {
case .some(1):
// something
case .some(2):
// something
case .some(3):
// something
case .some(4):
// something
default:
return
}
}

Pattern cannot match values of type 'SKError'

I’m inferring that your PurchaseResult is something like:

typealias PurchaseResult = CustomResult<Product, SKError>

(I don’t know what your Result type is, but all I know is that the standard Result type is .failure or .success, and yours has .error instead of .failure.)

I’m also assuming that you must have your own error type, e.g.:

enum MyAppError: Error {
case invalidProductId(String)
case noProductIdentifier
case paymentNotAllowed
}

So, I’d first change PurchaseResult to allow any relevant error types:

typealias PurchaseResult = CustomResult<Product, Error>

And then handle the various types of errors

func alertForPurchaseResult(result: PurchaseResult) -> UIAlertController {
switch result {
case .success(let product):
return alertWithTitle(title: "Thank You", message: "Purchase completed")

case .error(let error as SKError):
switch error.code {
case .unknown:
<#code#>
case .clientInvalid:
<#code#>
case .paymentCancelled:
<#code#>
case .paymentInvalid:
<#code#>
case .paymentNotAllowed:
<#code#>
case .storeProductNotAvailable:
<#code#>
case .cloudServicePermissionDenied:
<#code#>
case .cloudServiceNetworkConnectionFailed:
<#code#>
case .cloudServiceRevoked:
<#code#>
case .privacyAcknowledgementRequired:
<#code#>
case .unauthorizedRequestData:
<#code#>
case .invalidOfferIdentifier:
<#code#>
case .invalidSignature:
<#code#>
case .missingOfferParams:
<#code#>
case .invalidOfferPrice:
<#code#>
@unknown default:
<#code#>
}

case .error(let error as MyAppError):
switch error {
case .invalidProductId(let productID):
return alertWithTitle(title: "Purchase Failed", message: "\(productID) is not a valid product identifier")

case .noProductIdentifier:
return alertWithTitle(title: "Purchase Failed", message: "Product not found")

case .paymentNotAllowed:
return alertWithTitle(title: "Purchase Failed", message: "You are not allowed to make payments")
}

case .error:
return alertWithTitle(title: "Purchase Failed", message: "Unknown Error. Please Contact Support.")
}
}

The @unknown keyword in if you’re using Swift 5. You can omit that if using earlier Swift versions.

By the way, if you’re wondering about that SKError enumerations, when I did a switch for the .code of the SKError, it gave me a “fix” suggestion and it populated all those for me.

Expression pattern of type 'String' cannot match values of type 'AVMetadataKey'

Please ⌘-click on commonKey and you will see that the argument is of type AVMetadataKey rather than String.

You are encouraged to read the documentation. It's worth it and you can fix yourself an issue like this in seconds.

I added a guard statement to exit the method immediately if commonKey is nil.

private extension JukeboxItem.Meta {
func process(metaItem item: AVMetadataItem) {

guard let commonKey = item.commonKey else { return }
switch commonKey
{
case .commonKeyTitle :
title = item.value as? String
case .commonKeyAlbumName :
album = item.value as? String
case .commonKeyArtist :
artist = item.value as? String
case .commonKeyArtwork :
processArtwork(fromMetadataItem : item)
default :
break
}
}
}

Swift Enum: Expression pattern matching issue

Pattern matching uses Equatable internally, so you should change your Fruit class:

extension Fruit: Equatable {
static func == (lhs: Fruit, rhs: Fruit) -> Bool {
return lhs.name == rhs.name // or every field if you want
}
}

If you want to use the reference, simply change the == func to return true if both references are equal, but I don't think it's a good idea:

static func == (lhs: Fruit, rhs: Fruit) -> Bool {
return lhs === rhs
}

Swift tuple switch case : pattern of type cannot match values of type

Solution 1

let ABOUTTROVPROTECTIONCELL = (section: 1, row: 0)
let cellIdentifier = (section: indexPath.section, row: indexPath.row)

switch cellIdentifier {
case (ABOUTTROVPROTECTIONCELL.section, ABOUTTROVPROTECTIONCELL.row):
print("here")
default:
print("bleh")
}

Solution 2

Just use IndexPath struct and its initializer for creating ABOUTTROVPROTECTIONCELL

let ABOUTTROVPROTECTIONCELL = IndexPath(row: 0, section: 1)
let cellIdentifier = indexPath // Not necessary, you can just use indexPath instead

switch cellIdentifier {
case ABOUTTROVPROTECTIONCELL:
print("here")
default:
print("bleh")
}

Solution 3

Implement ~= func for your tuple:

typealias IndexPathTuple = (section: Int, row: Int)
func ~=(a: IndexPathTuple, b: IndexPathTuple) -> Bool {
return a.section ~= b.section && a.row ~= b.row
}

let ABOUTTROVPROTECTIONCELL = (section: 1, row: 0)
let cellIdentifier = (section: indexPath.section, row: indexPath.row)

switch cellIdentifier {
case ABOUTTROVPROTECTIONCELL:
print("here")
default:
print("bleh")
}

why `case self 0: return .positive` is not allowed in switch statement

This is a simple rule about switch statements.

In each case of a switch statement, the expression that follows immediately after case is evaluated and compared to the thing that you are switching on.

So in your code, self > 0 is evaluated and compared with self. Assuming self is 10,

self > 0 == self
true == 10 // WTF?

The second case let x where x > 0 is different, let x is not an expression. It basically saying:

In this case, I want a variable called x to be assigned the value of self. But please only do this if x > 0 after doing the assignment. If !(x > 0), go to another case.



Related Topics



Leave a reply



Submit