3

Being new to Xcode, I am trying to make use of an external C call that use pointers, and I’m having difficulty finding a way to reference them in Swift. The original call in C is defined as:

int32 FAR PASCAL swe_calc(double tjd, int ipl, int32 iflag, double *xx, char *serr)

where xx is a pointer to an array of 6 Doubles and serr is a pointer to any error message(s)

Swift sees it as:

int32 swe_calc(tjd: Double, ipl: Int32, iflag: int32, xx: UnsafeMutablePointer<Double>, serr: UnsafeMutablePointer<Int8>)

(from: https://github.com/dwlnetnl/SwissEphemeris/tree/master/SwissEphemeris)

The closest thing I’ve tried that even comes close is:

var serr : UnsafeMutablePointer<Int8> = nil; // returns any error messages from the call
var x = [Double](count: 6, repeatedValue: 0); // array of 6 doubles returned from the call
var xx : UnsafeMutablePointer<Double> ;
xx = &x
swe_calc(2436647.0003794227, 4, 258, xx, serr)
println("xx = \(x[0]), \(x[1]), \(x[2]), \(x[3]), \(x[4]), \(x[5]), errors (if any)=\(serr)")

The line xx=&x gives the error

Cannot assign a value of type input[(Double)] to a value of type ‘UnsafeMutablePointer’

I need a way to get/reference/use the values returned from xx into an array of 6 doubles, and serr should definitely not be an Int8, but a string instead. (I can get the other Java and C# versions to work, but Swift has me stumped.)

How can I make the swe_calc call to give it what it needs, and get out what I need?

nhgrif
62.1k25 gold badges136 silver badges174 bronze badges
asked May 4, 2015 at 20:45

1 Answer 1

2

You were close. Both UnsafeMutablePointer parameters need an array of the appropriate type passed as "inout argument" with &:

var x = [Double](count: 6, repeatedValue: 0);
var serr = [Int8](count: 1024, repeatedValue: 0)
let result = swe_calc(2436647.0003794227, 4, 258, &x, &serr)

Of course the arrays must be allocated large enough as expected by the C function.

If that function puts a NUL-terminated error string into the buffer pointed to by serr then you can convert it to a Swift string with:

let errorString = String.fromCString(&serr)
answered May 4, 2015 at 20:59

4 Comments

@nhgrif: I actually meant the en.wikipedia.org/wiki/Null_character which is abbreviated as NUL, and not NULL (as in null-pointer).
Oh, hm. I never knew the null character was NUL. Interesting. Sorry for the edit and thanks for the education.
@nhgrif: No problem. You find that e.g. on Unix manual pages: developer.apple.com/library/mac/documentation/Darwin/Reference/…: "the number of characters that precede the terminating NUL character"
FANTASTIC!!!! Worked like a charm! Thank-you. Thank-you Martin R, you're a life saver. Also, thank-you nhgrif for editing my question to make it more readable

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.