Swift字符串转wchar_t问题:LibXL写入单元格报错
问题根源:错误的UTF编码转换导致LibXL密钥无效
我一眼就看出你的String转wchar_t的实现有核心问题——这正是LibXL仍判定你在使用试用版的原因。
为什么你的转换代码不对?
在macOS和iOS平台上,wchar_t是32位的UTF-32编码,但你的代码用了.utf16来生成C字符串:
.cString(using: .utf16)返回的是16位的UTF-16字节序列- 你直接通过
memcpy把这些16位字节复制到4字节的wchar_t指针里,相当于把两个UTF-16字符打包成一个wchar_t,完全打乱了字符串的实际内容 - 结果就是你传给
xlBookSetKeyW的密钥是乱码,LibXL无法识别,依然保持试用版限制,所以才会抛出“can't write row 0 in trial version”的错误
正确的String转wchar_t实现
下面的扩展会正确处理UTF-32到wchar_t的转换,匹配LibXL对宽字符的要求:
extension String { /// Converts String to a null-terminated wchar_t pointer (matches macOS/iOS UTF-32 wchar_t) /// Important: Remember to deallocate the pointer with `deallocate()` when finished! var wideChar: UnsafeMutablePointer<wchar_t>? { let utf32Codes = Array(self.utf32) let capacity = utf32Codes.count + 1 // +1 for null terminator let ptr = UnsafeMutablePointer<wchar_t>.allocate(capacity: capacity) defer { // Initialize all memory to avoid garbage values ptr.initialize(repeating: 0, count: capacity) } for (index, code) in utf32Codes.enumerated() { ptr[index] = wchar_t(code) } // Ensure null terminator is set (already handled by initialize, but explicit here) ptr[capacity - 1] = 0 return ptr } }
关键使用注意:内存释放
宽字符指针需要手动释放,否则会造成内存泄漏。建议用defer语句确保指针在代码块结束时被释放:
if let name = "John Doe".wideChar, let passKey = "mac-f.....lots of characters...3".wideChar { defer { name.deallocate() passKey.deallocate() } xlBookSetKeyW(book, name, passKey) print(">: " + String.init(cString: xlBookErrorMessageW(book))) } if let sheetName = "Output".wideChar, let path = savePath.wideChar, let test = "Hello".wideChar { defer { sheetName.deallocate() path.deallocate() test.deallocate() } let sheet: SheetHandle = xlBookAddSheetW(book, sheetName, nil) xlSheetWriteStrW(sheet, 0, 0, test, sectionTitleFormat) print(">: " + String.init(cString: xlBookErrorMessageW(book))) let success = xlBookSaveW(book, path) dump(success) print(">: " + String.init(cString: xlBookErrorMessageW(book))) }
修复后的效果
修正编码转换后,LibXL能正确识别你的密钥,退出试用版模式,就不会再限制你写入第0行了,你的单元格写入操作应该能正常执行。
内容的提问来源于stack exchange,提问作者Todd
相关产品推荐
相关产品推荐

