Binary operator ' =' cannot be applied to operands of type 'String.IndexDistance?' (aka 'Optional Int ') and 'Int'
The reason is that Equatable
works with optionals and Comparable
does not. You have to unwrap the optional.
A suitable and safe solution is to optional bind the text
property:
if let password = textFieldPassword.text, password.count >= 8 { ... }
Binary operator '~=' cannot be applied to operands of type 'String' and 'String?'
Because of Optional Chaining, bubble?.name
has type String?
. You have a few options:
- Use
"largeBubble"?
in yourcase
expression (Swift 2+ only). - Check for nil before doing your
switch
, so the switch argument will be aString
instead ofString?
. - Use
bubble!.name
(orbubble.name
if it's aSKPhysicsBody!
) if you are absolutely sure that it won't be nil
Binary operator cannot be applied to operands of type Int and String - Swift 2.3 - Swift 3.2 conversion error
Error itself says it's different types Int
and String
.
You can need to typecast one or another in same form and them compare.
if (String(error?.code)!) == "-112"){
print("hello")
}
Cannot invoke initializer for type 'Double' with an argument list of type '(String?)'
amount?.count <= 0
here amount is optional. You have to make sure it not nil
.
let amount:String? = amountTF.text
if let amountValue = amount, amountValue.count <= 0 {
}
amountValue.count <= 0
will only be called if amount
is not nil.
Same issue for this let am = Double(amount)
. amount
is optional.
if let amountValue = amount, let am = Double(amountValue) {
// am
}
How to get an Int from a string.index?
You can use distance(from:to:)
to compute the (integer) distance
from a String.Index
to the string's start position:
let str = "abab"
for char in str {
if let idx = str.index(of: char) {
let distance = str.distance(from: str.startIndex, to: idx)
print(char, distance)
}
}
But note that index(of:)
returns the first index of the character
in the string, so the above code will print
a 0
b 1
a 0
b 1
If your intention is to get the running offset together with each character in the string then use enumerated()
for (distance, char) in str.enumerated() {
print(char, distance)
}
This will print
a 0
b 1
a 2
b 3
Swift3 optionals chaining in IF conditions bug?
Optional comparison operators are removed from Swift 3.
SE-0121
You need to write something like this:
if test?.optionalInt ?? 0 > 4
{
}
Swift Error Cannot convert value of type '[String.Element]' (aka 'Array Character ') to expected argument type '[String]'
The problem is that Array(allWords[0])
produces [Character]
and not the [String]
that you need.
You can call map
on a String
(which is a collection of Character
s and use String.init
on each character to convert it to a String
). The result of the map
will be [String]
:
var arrayOfLetters = allWords[0].map(String.init)
Notes:
- When I tried this in a Playground, I was getting the mysterious message
Fatal error: Only BidirectionalCollections can be advanced by a negative amount
. This seems to be a Playground issue, because it works correctly in an app. - Just the word
"Leopards"
produces109,536
permutations.
Another Approach
Another approach to the problem is to realize that permute
doesn't have to work on [String]
. It could use [Character]
instead. Also, since you are always starting with a String
, why not pass that string to the outer permute
and let it create the [Character]
for you.
Finally, since it is logical to think that you might just want anagrams of the original word, make minStringLen
an optional with a value of nil
and just use word.count
if the value is not specified.
func permute(word: String, minStringLen: Int? = nil) -> Set<String> {
func permute(fromList: [Character], toList: [Character], minStringLen: Int, set: inout Set<String>) {
if toList.count >= minStringLen {
set.insert(String(toList))
}
if !fromList.isEmpty {
for (index, item) in fromList.enumerated() {
var newFrom = fromList
newFrom.remove(at: index)
permute(fromList: newFrom, toList: toList + [item], minStringLen: minStringLen, set: &set)
}
}
}
var set = Set<String>()
permute(fromList: Array(word), toList:[], minStringLen: minStringLen ?? word.count, set: &set)
return set
}
Examples:
print(permute(word: "foo", minStringLen: 1))
["of", "foo", "f", "fo", "o", "oof", "oo", "ofo"]
print(permute(word: "foo"))
["foo", "oof", "ofo"]
Related Topics
Create View Based Nstableview Programmatically Using Bindings in Swift
Swiftui: Is There Exist Modifier to Highlight Substring of Text() View
Avcapturevideodataoutput Captureoutput Not Being Called
Use of Undeclared Type 'Attributedstring'
Calculate the Angle Between a Line and X-Axis
Why Is Calendar.Date(From: Datecomponents) Adding Time
Google API - Invalid Credentials
Why Swift Throws Error When Using Optional Param in Closure Func
Lazy Property Initialization in Swift
What Is the Use of the Validate() Method in Alamofire.Request
Using Applescript with Apple Events in MACos - Script Not Working
For-In Loop and Type Casting Only for Objects Which Match Type
Wkwebview Auto Fill Login Form Swift 2