1楼 @tinyfool 好吧,就那句String.fromCString(PQgetvalue(result, Int32(row), Int32(col)))!引发异常。PQgetvalue(result, Int32(row), Int32(col))函数从result结果中读取数据,返回char *
self.numberOfRows = Int(PQntuples(result))
for var row = 0; row < self.numberOfRows; row++ {
let rowrecord = NSMutableDictionary()
for var col = 0; col < self.numberOfColumns; col++ {
if PQgetisnull(result, Int32(row), Int32(col)) == 1 {
rowrecord.setObject("", forKey:self.fields[col] as! String)
}else {
let key = self.fields[col] as! String
let val = String.fromCString(PQgetvalue(result, Int32(row), Int32(col)))!
rowrecord.setObject(val, forKey:key)
}
}
tuples.addObject(rowrecord)
}
Xcode设断点提示这个:
Thread 14:EXCBADINSTRUCTION(code=EXCI386INVOP,subcode=0x0) 我也看不懂,下面的信息是线程14调用栈吧
0x100391253 <+35>: callq 0x1003de270 ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
-> 0x100391258 <+40>: ud2
0x10039125a <+42>: movq $0x0, -0x10(%rbp)
另外更正一下,如tiny叔所说,不是中文引起的,因为我查另外一个数据库的中文信息能正确显示,这种现象是不是因为编码不同或者这个数据库的数据本身就有问题——不过我用其他数据库客户端是可以查出数据,不会报错。
拖到下面又发现这样一些信息: 0x100391340 <+272>: leaq 0xdbc0d(%rip), %rdi ; "fatal error" 0x100391347 <+279>: leaq 0xdc892(%rip), %rcx ; "high- and low-surrogate code points are not valid Unicode scalar values"
0x1003913ea <+442>: leaq 0xdbb63(%rip), %rdi ; "fatal error" 0x1003913f1 <+449>: leaq 0xdbcf8(%rip), %rcx ; "shift amount is larger than type size in bits"
0x100391413 <+483>: leaq 0xdbb3a(%rip), %rdi ; "fatal error" 0x10039141a <+490>: leaq 0xdc80f(%rip), %rcx ; "value is outside of Unicode codespace" 0x100391421 <+497>: movl $0xb, %esi
猜测还是数据本身的编码问题