Swift error: Could not find an overload for '==' that accepts the supplied arguments?
You can doif (status == .NotReachable)
or if (status == NetworkStatus.NotReachable)
.
Could not find an overload init' that accept the supplied argument
Array subscript takes an Int
, not a UInt32
. You need to convert back:
let x = jokes[Int(arc4random_uniform(UInt32(jokes.count)))]
If this gets a bit noisy for you, you may want to create a function to handle it:
func randomValueLessThan(x: Int) -> Int {
return Int(arc4random_uniform(UInt32(x)))
}
Or you could extend Array to help out:
extension Array {
func uniformSelection() -> T {
return self[Int(arc4random_uniform(UInt32(self.count)))]
}
}
EDIT: It is worth digging a little deeper into the Int(arc4random()%jokes.count)
case because you might be tempted to fix it incorrectly, and it demonstrates why Swift works the way it does.
Let's start with your version
let n = Int(arc4random() % jokes.count)
// => Could not find an overload for 'init' that accepts the supplied arguments
That's a little confusing. Let's simplify to see the problem
let n = arc4random() % jokes.count
// => Cannot invoke '%' with an argument list of type '(UInt32, Int)'
That should be clearer. arc4random
returns a UInt32
and jokes.count()
returns an Int
. You can't modulus different types. You need to get them to the same place. Well, we want an Int
, right? Seems easy:
let n = Int(arc4random()) % jokes.count // WARNING!!!! Never ever do this!!!!
Why is Apple so pedantic and forces us to do that by hand? Couldn't the compiler just cast it automatically? Well, the above code will work fine on a 64-bit processor and crash about half the time on a 32-bit processor. That's because by calling Int()
, you're promising that the value will always be in the Int
range. And on a 32-bit processor, that's the 32-bit signed range. But arc4random
returns a value in the whole 32-bit unsigned range, which includes lots of numbers that won't fit in an Int
. Blam! (Or if you turn off bounds checking, then it just trashes your numbers like it does in C, which is no better.)
That's why Swift is picky about integer conversions. You need to be absolutely certain when you convert that it's a safe conversion. You shouldn't just sprinkle them around until it compiles.
That said, of course you should never use modulus on arc4random
. But that's a different question.
As one more side note, you'll notice that the numeric casting in randomValueLessThan()
creates many possible invalid situations. If you pass a number less than 0 you'll crash (that shouldn't be surprising, though). If you pass a number greater than UInt32.Max
, you'll also crash, which is slightly more surprising, but pretty unlikely in most code. The point of this is that by adding these casts, we've made randomValueLessThan
a partial function. It's not defined over all of its range of inputs (it's "domain"). In "real life" programming, we do that all the time and we just hope it never bites us. But Swift is trying to help us get bitten even less by making it more obvious when you're breaking type safety.
uniformSelection
has a similar problem. It's only defined for arrays with fewer than UInt32.Max
elements. These sometimes feel like meaningless corner cases, and they are, until suddenly they aren't and your program crashes. (Array.subscript
is also a partial function, since it is undefined for values outside the array's range. I actually suggested to Apple that Array.subscript
return an optional to account for that. They were probably wise to ignore me.)
Could not find an overload for '*' that accepts the supplied argument
Swift seems to be fairly picky about implied type casting, so in your example you're multiplying str (an Integer) by 0.01 (a Double) so to resolve the error, you'll need to cast it like this:
var str: Int = 0
var pennyCount = 0.00
str = pennyTextField.text.toInt()!
pennyCount = Double(str) * 0.01
Could not find an overload for '-' that accepts the supplied arguments
Use Int
, not Integer
.
Integer
is a protocol, not the type.
error - could not find an overload for '-' that accepts the supplied arguments
Change it to this:
var lastContentOffset = scrollView.contentOffset.y
if (lastContentOffset * -1) > 64 {
//do something
}
Swift error: Could not find an overload for '&&' that accepts the supplied arguments
If you look at the full compiler output in the Report Navigator then you will see the
message
note: expression was too complex to be solved in reasonable time;
consider breaking up the expression into distinct sub-expressions
which tells you how to solve the problem.
Could not find an overload for 'init' that accepts the supplied arguments
resultLabel.text = "(\area)"
Create a string representation instead.
Could not find an overload for 'logInWithPermissions' that accepts the supplied arguments
The problem is with the second argument, the block. The two arguments are typed as PFUser
and NSError
, but they should be optional types (because those values are coming from Objective-C and can be nil). You can fix it by excluding the types from your closure and letting the compiler infer them:
PFFacebookUtils.logInWithPermissions(permissions, block: { user, error in ... })
Swift: Could not find an overload for '|' that accepts the supplied arguments
Use the .value
then use the result to create a PFLogInFields
instance:
logInViewController.fields = PFLogInFields(PFLogInFieldsUsernameAndPassword.value
| PFLogInFieldsLogInButton.value)
Related Topics
Swiftyjson - Call Can Throw, But It Is Marked with 'Try' and the Error Is Not Handled
Spacer Not Working with Form Inside a VStack
How to Create an Array of Functions
Implicit Return in a Closure Causing an Error
Why Can the Keyword "Weak" Only Be Applied to Class and Class-Bound Protocol Types
Swift Package Manager - Uikit Dependency
Present Uiviewcontroller as a Modal with Transparent Background
Unwrapping Swift Optional Without Variable Reassignment
Swift - Seeding Arc4Random_Uniform? or Alternative
Swift Closures Causing Strong Retain Cycle with Self
Cannot Assign to Property: Function Call Returns Immutable Value
Declaration Is Only Valid at File Scope (Extension)
How to Create Several Cached Uicolor
Ambiguous Use of Subscript (Swift 3)
How to Avoid Nested Navigation Bars in Swiftui
Get the Last Character of a String Without Using Array