Mark Mark - 1 month ago 11
Swift Question

Swift: 'Int32' is not convertible to 'Int32'

I'm in the middle of a project using

Swift 1.1
and
xCode 6.1.1


I receive the following error when trying to make seconds out of a Value and Timescale

PATH/ViewControllers/Camera/CaptureScreenViewController.swift:41:58: 'Int32' is not convertible to 'Int32'


on the line following line

var seconds = CMTimeMakeWithSeconds(Float64(value),timescale)


at
,timescale)


Below are a few lines for reference

var currentCellSegment = segmentForIndexPath(indexPath)
var value = currentCellSegment.duration.value.value
var timescale = currentCellSegment.duration.timescale.value
var seconds = CMTimeMakeWithSeconds(Float64(value),timescale)


Any suggestions on how to fix? Or answers to why this is happening?

Things I have tried

I have already uninstalled xCode, restarted, and re-installed.

I've already tried to cast
timescale
as Int32
like so

var timescale = currentCellSegment.duration.timescale.value as Int32


var timescale: Int32 = currentCellSegment.duration.timescale.value


var timescale: Int32 = currentCellSegment.duration.timescale.value as Int32


, but I receive the error on the line
var timescale...


as suggested by @martin-r

new reference code

var currentCellSegment = segmentForIndexPath(indexPath)
var value = currentCellSegment.duration.value
var timescale = currentCellSegment.duration.timescale
var seconds = CMTimeMakeWithSeconds(Float64(value),timescale)


Solved

Answer

The error message is indeed a bit strange, but the solution is probably to remove the unnecessary .value calls:

var value = currentCellSegment.duration.value
var timescale = currentCellSegment.duration.timescale

If currentCellSegment.duration is a struct CMTime then its timescale property is a CMTimeScale aka Int32, and that is what CMTimeMakeWithSeconds() expects.


The value property of Int32 returns a Builtin.Int32, and that causes the strange error message. Example:

func abc(x : Int32) {
    println(x)
}
let x = Int32(1)
abc(x)       // OK
abc(x.value) // error: 'Int32' is not convertible to 'Int32'