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?'
First, the type of the first parameter of sysctl in Swift is UnsafeMutablePointer<Int32>!.
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 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>!.
Fourth, the return type of test in C++ is int, as already said, its equivalent in Swift is Int32.
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 }