swift3.0 无法将类型“[UnsafeMutablePointer<Int8>]”的值转换为预期的参数类型 'UnsafeMutablePointer<Int8>?'
swift3.0 Cannot convert value of type '[UnsafeMutablePointer<Int8>]' to expected argument type 'UnsafeMutablePointer<Int8>?'
此代码在 Swift2.3 中运行良好,现在我正在将其转换为 Swift3。所以我收到这个错误。任何人都有想法,如何解决这个问题?
var cmdLnConf: OpaquePointer?
fileprivate var cArgs: [UnsafeMutablePointer<Int8>]
public init?(args: (String,String)...) {
// Create [UnsafeMutablePointer<Int8>].
cArgs = args.flatMap { (name, value) -> [UnsafeMutablePointer<Int8>] in
//strdup move the strings to the heap and return a UnsageMutablePointer<Int8>
return [strdup(name),strdup(value)]
}
cmdLnConf = cmd_ln_parse_r(nil, ps_args(), CInt(cArgs.count), &cArgs, STrue)
if cmdLnConf == nil {
return nil
}
}
enter image description here
根据我们的讨论,您的 C 函数中的参数似乎应该是 char *p[]
我做了个小测试
//
// f.h
// test001
//
#ifndef f_h
#define f_h
#include <stdio.h>
void f(char *p[], int len);
#endif /* f_h */
我定义了具有一些基本功能的函数
//
// f.c
// test001
#include "f.h"
void f(char *p[], int len) {
for(int i = 0; i<len; i++) {
printf("%s\n", p[i]);
};
};
具有所需的桥接头
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#include "f.h"
和swift'command line'申请
//
// main.swift
// test001
//
import Darwin
var s0 = strdup("alfa")
var s1 = strdup("beta")
var s2 = strdup("gama")
var s3 = strdup("delta")
var arr = [s0,s1,s2,s3]
let ac = Int32(arr.count)
arr.withUnsafeMutableBytes { (p) -> () in
let pp = p.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Int8>?.self)
f(pp, ac)
}
它终于打印出来了
alfa
beta
gama
delta
Program ended with exit code: 0
根据结果,你必须使用
let count = CInt(cArgs.count)
cArgs.withUnsafeMutableBytes { (p) -> () in
let pp = p.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Int8>?.self)
cmdLnConf = cmd_ln_parse_r(nil, ps_args(), count, pp, STrue)
}
警告!!!
不要在定义指针的闭包内部调用 cArgs.count
!
此代码在 Swift2.3 中运行良好,现在我正在将其转换为 Swift3。所以我收到这个错误。任何人都有想法,如何解决这个问题?
var cmdLnConf: OpaquePointer?
fileprivate var cArgs: [UnsafeMutablePointer<Int8>]
public init?(args: (String,String)...) {
// Create [UnsafeMutablePointer<Int8>].
cArgs = args.flatMap { (name, value) -> [UnsafeMutablePointer<Int8>] in
//strdup move the strings to the heap and return a UnsageMutablePointer<Int8>
return [strdup(name),strdup(value)]
}
cmdLnConf = cmd_ln_parse_r(nil, ps_args(), CInt(cArgs.count), &cArgs, STrue)
if cmdLnConf == nil {
return nil
}
}
enter image description here
根据我们的讨论,您的 C 函数中的参数似乎应该是 char *p[]
我做了个小测试
//
// f.h
// test001
//
#ifndef f_h
#define f_h
#include <stdio.h>
void f(char *p[], int len);
#endif /* f_h */
我定义了具有一些基本功能的函数
//
// f.c
// test001
#include "f.h"
void f(char *p[], int len) {
for(int i = 0; i<len; i++) {
printf("%s\n", p[i]);
};
};
具有所需的桥接头
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#include "f.h"
和swift'command line'申请
//
// main.swift
// test001
//
import Darwin
var s0 = strdup("alfa")
var s1 = strdup("beta")
var s2 = strdup("gama")
var s3 = strdup("delta")
var arr = [s0,s1,s2,s3]
let ac = Int32(arr.count)
arr.withUnsafeMutableBytes { (p) -> () in
let pp = p.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Int8>?.self)
f(pp, ac)
}
它终于打印出来了
alfa
beta
gama
delta
Program ended with exit code: 0
根据结果,你必须使用
let count = CInt(cArgs.count)
cArgs.withUnsafeMutableBytes { (p) -> () in
let pp = p.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Int8>?.self)
cmdLnConf = cmd_ln_parse_r(nil, ps_args(), count, pp, STrue)
}
警告!!!
不要在定义指针的闭包内部调用 cArgs.count
!