Cannot invoke initializer for type 'UnsafeMutablePointer<Int32>' with an argument list of type '(Int32?)' - ios

I am trying to implement some methods and pointers of ffmpeg to Swift but converting it to Swift is a little complex. What does this error mean?
Cannot invoke initializer for type 'UnsafeMutablePointer' with an argument list of type '(Int32?)'?
Code is below
let pictureFrameData = av_malloc(Int(numBytes))
var test = frame?.pointee.linesize.0.
av_image_fill_arrays(UnsafeMutablePointer(frameRGB?.pointee.data.0),
UnsafeMutablePointer<Int32>(frame?.pointee.linesize.0)!,
pictureFrameData,
frameRGB?.pointee.format,
frameRGB?.pointee.width,
frameRGB?.pointee.height,
1)
the error is at this line UnsafeMutablePointer<Int32>(frame?.pointee.linesize.0)

If the function expects an 'UnsafeMutablePointer<Int32> argument
then you'll have to pass an Int32 value as “inout parameter” with &:
var linesize: Int32 = ...
av_image_fill_arrays(..., &lineSize, ...)

Related

Constant 'spacesLeft' inferred to have type '()', which may be unexpected Swift

I am building a Tic Tac Toe game with an AI using Xcode 8 and Swift. Here are the relevant variables I am using that are contributing to the error:
var allSpaces: Set<Int> = [1,2,3,4,5,6,7,8,9]
var playerOneMoves = Set<Int>()
var playerTwoMoves = Set<Int>()
var nextMove: Int? = nil
Inside a function defining how the AI will play there are these variables:
var count = 0
let spacesLeft = allSpaces.subtract(PlayerOneMoves.union(playerTwoMoves))
The latter results in the compiler warning:
Constant 'spacesLeft" inferred to have type '()', which may be unexpected
There is an if statement just below that says:
if allSpaces.subtract(playerOneMoves.union(playerTwoMoves)).count > 0 {
nextMove = spacesLeft[spacesLeft.startIndex.advancedBy(Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
The condition gives the following error:
Value of tuple type '()' has no member 'count'
The statement gives the following error:
Type '()' has no subscript members
I am struggling to find a solution.
subtract modifies Set in place and doesn't return a value, you want to use subtracting
For the first warning, subtract returns Void, so use subtracting:
let spacesLeft = allSpaces.subtracting(playerOneMoves.union(playerTwoMoves))
For the second error, advancedBy is deprecated, you may change like this:
if spacesLeft.count > 0 {
nextMove = spacesLeft[spacesLeft.index(spacesLeft.startIndex, offsetBy: Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}
Set.subtract is a mutating function, so it modifies the Set in place and its return value is Void, which is just a type alias for an empty tuple, (), hence the warning.
You should call Set.substracting, which is the non-mutating version of subtract and returns Set<Set.Element>.
The subtract(_:) function is a mutating function so it will mutate the Set your using to call the function.
From Apple Docs:
subtract(_:)
Removes the elements of the given set from this set.
The reason you're getting the errors is because this function returns Void which in Swift is a typealias for an empty tuple(from Swift's source code). Since Void has no subscripts nor count property/variable you get those errors.
Maybe you should take a look at the subtracting(_:) function, which returns a different Set.
From Apple Docs:
subtracting(_:)
Returns a new set containing the elements of this set that do not occur in the given set.

Swift3 error 'cannot be applied to operands of type

Getting the following error:
Binary operator '==' cannot be applied to operands of type 'UInt16' and '() -> UInt16'
in this section of code:
let array: [UInt16] = [3,4, 7]
let x = NSNumber(value: 4)
let option = array.filter { (test) -> Bool in
return test == x.uint16Value // compiler complains here
}
print(option) // "[4]"
Normally this type of error means two values of separate class are being compared. Here the compiler thinks I'm comparing a uint16 value with a function that returns a uint16 value.
What I mean to do is call NSNumber's uint16 property getter.
// defined in NSNumber
open var uint16Value: UInt16 { get }
I'm told to instead make a function call by adding parenthesis (e.g .uint16Value()). But this leads to the follow up error:
Cannot call value of non-function type 'UInt16'
I have dropped this exact code into a playground and it runs fantastically.
Has anyone else run into this? Or seen this type of behavior? Have you beaten it?
Update:
When setting to a local variable, no difference is made. I have recently run the migration tool from Swift 2.2 or 2.3 up to swift 3 in Xcode 8.0. It doesn't seem like there is anything wrong with syntax or migration.
let y = x.uint16Value
return test == y // compiler complains here

Cannot convert value of type 'String!' to expected argument type error

I have a public function:
public func lastActivityFor(userName: String) -> String { ... }
and later I want to call it as:
OneLastActivity.lastActivityFor("username")
but the last line get error:
Cannot convert value of type 'String!' to expected argument type 'OneLastActivity'
Why it appears if I send to those function String as suggested?(userName: String)
Any ideas how can I fix it? If you want more code, just ask me about.
Just create an instance of your class this way:
let temp = OneLastActivity()
Now you can use it's method:
temp.lastActivityFor("username")
Or you can directly use it this way:
OneLastActivity().lastActivityFor("username")
Hope it helps.

Calling C function from Swift

I am trying to call a C function from Swift , but I do not know exactly how to define variables to pass parameters.
The function c is:
DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals );
The main problem is pszFieldName, pnWidth and pnDecimals inout parameters. I tried made ​​:
var dbf:DBFHandle = DBFOpen(pszPath, "rb")
var fName:[CChar] = []
var fieldWidth:Int32 = 0
let fieldDecimals:Int32 = 0
let fieldInfo:DBFFieldType = DBFGetFieldInfo(dbf, i, fName, &fieldWidth, &fieldDecimals)
but it gives me an error
Cannot invoke 'DBFGetFieldInfo' with an argument list of type '(DBFHandle, Int32, [CChar], inout Int32, inout Int32)'
Expected an argument list of type '(DBFHandle, Int32, UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>)'
Any ideas?
UnsafeMutablePointer<Int8>, UnsafeMutablePointer<Int32>, UnsafeMutablePointer<Int32>
You need to convert your variables to the appropriate types required by the method signature.
C Syntax:
const Type *
Type *
Swift Syntax:
UnsafePointer
UnsafeMutablePointer
This is covered by Apple in their Using Swift with Cocoa and Objective-C reference located here.
C Syntax -----> Swift Syntax
const Type * -----> UnsafePointer
Type * -----> UnsafeMutablePointer
The number of input and the types should be the same
To create an UnsafeMutablePointer<Int8> from a string use:
String(count: 10, repeatedValue: Character("\0")).withCString( { cString in
println()
// Call your function here with cString
})

Can´t get the SecKey UnsafeMutablePointer in iOS Swift

I am working in a project in swift, involving RSA Encryption, and I am stuck with a pointer problem as follows:
I have a global var publicKey: SecKey? optional value, and I need to get the UnsafeMutablePointer<Unmanaged<SecKey>?> pointer to it, that is the data type required in the SecKeyGeneratePair function.
I am trying to define the pointer as:
var keyPointer = UnsafeMutablePointer<Unmanaged<SecKey>?>(publicKey!)
But the compiler complains with a Cannot invoke 'init' with an argument of type #lvalue SecKey error
According to the Using Swift with Cocoa and Objective-C book, In the Core Foundation and Unmanaged Objects section, it states that the Unmanaged<T> structure provides 2 methods takeUnretainedValue() and takeRetainedValue(). but trying to implement them, gives the following error
var keyPointer = UnsafeMutablePointer<Unmanaged<SecKey>?>(publicKey!.takeRetainedValue())
'SecKey' does not have a member named 'takeRetainedValue'
Any help to work this out will be appreciated
Try this:
var publicKey: SecKey?
var privateKey: SecKey?
var publicKeyUnManaged:Unmanaged<SecKey>?
var privateKeyUnManaged:Unmanaged<SecKey>?
let dic:[String:String] = [kSecAttrKeyType:kSecAttrKeyTypeRSA, kSecAttrKeySizeInBits:"2048"]
SecKeyGeneratePair(dic, &publicKeyUnManaged, &privateKeyUnManaged)
publicKey = publicKeyUnManaged?.takeRetainedValue()
privateKey = privateKeyUnManaged?.takeRetainedValue()
You don't have to create UnsafeMutablePointer<Unmanaged<SecKey>?> manually. As mentioned in this document,
When a function is declared as taking an UnsafeMutablePointer argument, it can accept any of the following:
nil, which is passed as a null pointer
An UnsafeMutablePointer value
An in-out expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call
In this case, we can use 3rd "in-out" expression.
for Swift 2.1 it isn't working with Unmanaged, the right version is:
var publicKey: SecKey?
var privateKey: SecKey?
let dic:[String:String] = [kSecAttrKeyType:kSecAttrKeyTypeRSA, kSecAttrKeySizeInBits:"2048"]
SecKeyGeneratePair(dic, &publicKey, &privateKey)

Resources