Facing type conversion issues in swift language

Hi, I am new to Swift language. I am trying to convert below C++ function to Swift function as below.

C++ function:
Code Block
int test(test_type type, request_info_t *request, size_t request_len)
{
  size_t size = request_len;
  int mib[] = {0,0,0,0,0};
   
  int ret = sysctl(mib, 5, request, &size, NULL, 0)
  return ret;
}

Swift function:
Code Block
func test( type: test_type, request: request_info_t, request_len: size_t) -> Int64
{
  let size: Int = request_len
  let mib: [Int]= [0, 0, 0, 0, 0]
   
  let val: Int32 = sysctl(mib, 5, request, size, nil, 0)
  return val
}

But I am facing below issues with arguments at sysctl(). Could any one please to convert this.
  • Error due to first argument mib: Cannot convert value of type '[Int]' to expected argument type 'UnsafeMutablePointer<Int32>?'

  • Error due to third argument request: Cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>?'

  • Error due to fourth argument size: Cannot convert value of type 'requestinfot' to expected argument type 'UnsafeMutableRawPointer?'

Accepted Reply

  • First, the type of the first parameter of sysctl in Swift is UnsafeMutablePointer<Int32>!.

You may know that int in C++ (of Apple's platform) is equivalent to Int32 in Swift, not Int.
You should declare mib as an Array of Int32 and mutable.
  • Second, the type of the third parameter of sysctl in Swift is UnsafeMutableRawPointer!.

You can pass arbitrary mutable pointer to UnsafeMutableRawPointer!, but the second parameter of test in Swift is declared as request_info_t, not a pointer. It does not match the type of second parameter in C++, it's request_info_t * -- pointer to request_info_t.
You should declare the second parameter of test in Swift as pointer to request_info_t.
  • Third, the type of the fourth parameter of sysctlin Swift is UnsafeMutablePointer<Int>!.

You can pass an inout parameter (&) of Int to UnsafeMutablePointer<Int>!, but your code lacks &.
  • Fourth, the return type of test in C++ is int, as already said, its equivalent in Swift is Int32.

All such things included, your code should look like this:
Code Block
func test(type: test_type, request: UnsafeMutablePointer<request_info_t>, request_len: size_t) -> Int32 {
var size: Int = request_len
var mib: [Int32] = [0, 0, 0, 0, 0]
let val: Int32 = sysctl(&mib, 5, request, &size, nil, 0)
return val
}


Replies

val is an Int32 and your func returns Int64. That's not correct.

Have a look here for details and examples of using sysctl in Swift:
https :// www.cocoawithlove. com/blog/2016/03/08/swift-wrapper-for-sysctl.html
  • First, the type of the first parameter of sysctl in Swift is UnsafeMutablePointer<Int32>!.

You may know that int in C++ (of Apple's platform) is equivalent to Int32 in Swift, not Int.
You should declare mib as an Array of Int32 and mutable.
  • Second, the type of the third parameter of sysctl in Swift is UnsafeMutableRawPointer!.

You can pass arbitrary mutable pointer to UnsafeMutableRawPointer!, but the second parameter of test in Swift is declared as request_info_t, not a pointer. It does not match the type of second parameter in C++, it's request_info_t * -- pointer to request_info_t.
You should declare the second parameter of test in Swift as pointer to request_info_t.
  • Third, the type of the fourth parameter of sysctlin Swift is UnsafeMutablePointer<Int>!.

You can pass an inout parameter (&) of Int to UnsafeMutablePointer<Int>!, but your code lacks &.
  • Fourth, the return type of test in C++ is int, as already said, its equivalent in Swift is Int32.

All such things included, your code should look like this:
Code Block
func test(type: test_type, request: UnsafeMutablePointer<request_info_t>, request_len: size_t) -> Int32 {
var size: Int = request_len
var mib: [Int32] = [0, 0, 0, 0, 0]
let val: Int32 = sysctl(&mib, 5, request, &size, nil, 0)
return val
}