iOS Swift Error: 'T' is not convertible to 'MirrorDisposition'
You are getting an error because the compiler can't guarantee that the element stored in your array can be compared with ==
. You have to ensure that it the contained type is Equatable
. However, there is no way to add a method to a generic class that is more restrictive than the class itself. It is better to implement it as a function:
func removeItem<T: Equatable>(item: T, var fromArray array: [T]) -> [T] {
for i in reverse(0 ..< array.count) {
if array[i] == item {
array.removeAtIndex(i)
}
}
return array
}
Or you could add it as a more generic extension:
extension Array {
mutating func removeObjectsPassingTest(test: (object: T) -> Bool) {
for var i : Int = self.count - 1; i >= 0; --i {
if test(object: self[i]) {
self.removeAtIndex(i)
}
}
}
}
Then you can do this:
var list: [Int] = [1,2,3,2,1]
list.removeObjectsPassingTest({$0 == 2})
float is not convertible to 'MirrorDisposition' Swift What is mirrordisposition?
MirrorDisposition
is one of the types you can get from a Mirror
of a value (using reflect
function). They are made for the IDE to enable displaying the values.
/// How children of this value should be presented in the IDE.
enum MirrorDisposition {
case Struct
case Class
case Enum
case Tuple
case Aggregate
case IndexContainer
case KeyContainer
case MembershipContainer
case Container
case Optional
case ObjCObject
}
The error message means that the compiler didn't find an ==
operator to compare a Float
with an Int
. However, it probably found an ==
operator for MirrorDisposition
and Int
, so it is trying to convert Float
to MirrorDisposition
but it obviously can't, so you get an error message.
(by the way, the type error you get is random, depending on the operator the compiler tries to use. I am getting Float is not convertible to Selector
).
The error message is a bug, there should be a message saying Could not find == operator for Float and Int
.
The obvious fix to check value equality is using a cast:
if intValue == Int(floatValue) {
There is no reason to compare types this way because in Swift, types are checked by the compiler. There should never be a reason to check the type explicitly (speaking about value types, not object types, of course).
Can't perform an IF statement on a UITableViewCell in Swift? Error: is not convertible to MirrorDisposition
cell is not an Optional therefore it will never be nil. Try this instead, declaring cell as an NDTableViewCell? and then unwrapping it before the return.
var cell:NDTableViewCell? = tableView.dequeueReusableCellWithIdentifier(NDTableViewCell.identifier) as? NDTableViewCell
if (cell == nil) { // Error happens here!
cell = NDTableViewCell.loadFromNib()
}
return cell!
Also, see this answer for more details about the failed comparison: float is not convertible to 'MirrorDisposition' Swift What is mirrordisposition?
NSError' is not convertible to '@lvalue inout $T9' in Swift
You are reusing the error
variable passed in to the block - you simply have to define a local optional variable and pass its reference to JSONObjectWithData
var myError: NSError?
self.data = NSJSONSerialization.JSONObjectWithData(response, options:NSJSONReadingOptions.MutableLeaves, error: &myError)
That happens because JSONObjectWithData
needs a reference to a variable of NSError
type. The one passed to the block is immutable - it points to an instance of NSError
, but cannot be reassigned to point to another instance, or set to nil in case of no error.
Swift String is not convertible to 'MirorDisposition' error
You probably have to check whether array[0] doesn't contain the "1" string instead of an integer value
Error: Int is not convertible to NSRange
NSRange
is a struct
with members location
and length
, not an Int
use the initializer
var range: NSRange = NSRange(location:0, length:0)
or the convenience function
var range: NSRange = NSMakeRange(0, 0)
CGFloat is not convertible to Int when trying to calculate an expression
You have already explained and solved the matter perfectly. It's simply a matter of you accepting your own excellent explanation:
It works perfectly fine if ... I just give it a literal value ... But when I go with any variable or constant or expression containing one, it displays an error "CGFloat is not convertible to Int".
Correct. Numeric literals can be automatically coerced to another type, such as CGFloat. But variables cannot; you must coerce them, explicitly. To do so, initialize a new number, of the correct type, based on the old number.
So, this works automagically:
let f1 : CGFloat = 1.0
let f2 = f1 + 1
But here, you have to coerce:
let f1 : CGFloat = 1.0
let f2 = 1
let f3 = f1 + CGFloat(f2)
It's a pain, but it keeps you honest, I guess. Personally I agree with your frustration, as my discussion here will show: Swift numerics and CGFloat (CGPoint, CGRect, etc.) It is hard to believe that a modern language is expected to work with such clumsy numeric handling, especially when we are forced against our will to bang into the Core Graphics CGFloat type so frequently in real-life Cocoa programming. But that, it seems, is the deal. I keep hoping that Swift will change in this regard, but it hasn't happened so far. And there is a legitimate counterargument that implicit coercion, which is what we were doing in Objective-C, is dangerous (and indeed, things like the coming of 64-bit have already exposed the danger).
Enumerating with a for-in loop
while summaryValues[dayIndex]==nil{ // ERROR
summaryValues is an array and array[i] won't return nil. It will either return the element or it will crash if your index is out of range. First, the compiler is probably confused from comparing Int
and nil
. Second, trying to access summaryValues[dayIndex]
here will crash, because at the time you are checking it the array is empty and the index does not exist.
Related Topics
How to Cast a Metaclass Object to a Protocol Type in Swift
Building for Arm64E on Apple Silicon
Swift Alternative to Respondstoselector:
Realitykit - How to Edit or Add a Lighting
Firebase Sms Verification on iOS - 'Token Mismatch'
Calling Nsexception.Raise() in Swift
How to Replace Limited Number of Occurrences in String
Ios/Tvos Playground Fails with "Unable to Find Execution Service for Selected Run Destination"
Nsdateformatter Detect 24-Hour Clock in Os X and iOS
Swiftui MACos Scroll a List with Arrow Keys While a Textfield Is Active
What Is the Slice Compare Logic in Swift
From the Swift Repl, How to Get a List of Available Modules
Self Refrence Inside Swift Closure Return Nil Some Time