[swift-users] Passing variadic C args through?
Jordan Rose
jordan_rose at apple.com
Tue Nov 14 19:47:22 CST 2017
Heh, it would be nice to catch this. You're collecting a bunch of arguments in an Array, and then passing that array straight on as a single argument itself, which means it gets passed as either an NSArray or a pointer (not sure which). Use 'init(format:arguments:)' instead.
Jordan
> On Nov 14, 2017, at 17:19, Rick Mann via swift-users <swift-users at swift.org> wrote:
>
> I've had a long-working `debugLog()` method that looks like this:
>
> ```
> func
> debugLog<T>(_ inMsg: T, file inFile : String = #file, line inLine : Int = #line)
> {
> let file = (inFile as NSString).lastPathComponent
> let s = "\(file):\(inLine) \(inMsg)"
> print(s)
> }
> ```
>
> I wanted to add a version that works like `String(format:)`:
>
> ```
> func
> debugLog(format inFormat: String, file inFile : String = #file, line inLine : Int = #line, _ inArgs: CVarArg...)
> {
> let s = String(format: inFormat, inArgs)
> debugLog(s, file: inFile, line: inLine)
> }
>
> ```
>
> While this compiles and executes, all of the values are zero for this example:
>
> ```
> let xc = CGFloat(1.0)
> let yc = CGFloat(0.0)
> let len = CGFloat(282.1364917907643)
>
> debugLog(format: "Pixel %f, %f length too far %f", xc, yc, len)
> ```
>
> Output:
>
> ```
> FisheyeImageRenderer.swift:108 Pixel 0.000000, 0.000000 length too far 0.000000
> ```
>
> Something is being misinterpreted in the passing of `inArgs: CVarArg...`
>
> TIA,
>
> --
> Rick Mann
> rmann at latencyzero.com
>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171114/94f3c241/attachment.html>
More information about the swift-users
mailing list