Hi, I am new to Swift language.
In my C++ application, I able to convert 'void *' to 'int * 'as below. Here, 'request' type is 'void *' type.
I am trying to do same thing in Swift also. Here 'request' type is 'UnsafeMutableRawPointer' But I am facing with error
Any one please help to convert from UnsafeMutableRawPointer type to UnsafeMutablePointer<Int32>.
In my C++ application, I able to convert 'void *' to 'int * 'as below. Here, 'request' type is 'void *' type.
Code Block int *ret = request;
I am trying to do same thing in Swift also. Here 'request' type is 'UnsafeMutableRawPointer' But I am facing with error
"Cannot convert value of type 'UnsafeMutableRawPointer' to specified type 'UnsafeMutablePointer<Int32>'"
Code Block let ret: UnsafeMutablePointer<Int32> = request
Any one please help to convert from UnsafeMutableRawPointer type to UnsafeMutablePointer<Int32>.
You should have included the C++ code in your initial post.
The best methods to work with Swift pointers are different depending on the context.
You have made me spend some wasted time by hiding very important parts.
In your C++ code, the return type is int, NOT a pointer.
It returns an int value by dereferencing the pointer ret, as shown in return *ret;. You know what * means in C++.
The Swift equivalent to your C++ code would look like this:
I am very curious why test in this thread is different than the test in another post of yours.
Hope you would not waste my time any more.
The best methods to work with Swift pointers are different depending on the context.
You have made me spend some wasted time by hiding very important parts.
In your C++ code, the return type is int, NOT a pointer.
It returns an int value by dereferencing the pointer ret, as shown in return *ret;. You know what * means in C++.
The Swift equivalent to your C++ code would look like this:
Code Block func test(type: test_type, request: UnsafeMutableRawPointer, 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) if val == 0 { return request.load(as: Int32.self) } else if errno == EISDIR || errno == ENOENT { return 0 } return -1 }
I am very curious why test in this thread is different than the test in another post of yours.
Hope you would not waste my time any more.