我正在尝试将生 rust 的*const __CFData
转换为String
。
我正在尝试获取键盘语言并检查它是否是特定语言。
let current_source = ffi::TISCopyCurrentKeyboardLayoutInputSource();
let current_language = ffi::TISGetInputSourceProperty(current_source, ffi::kTISPropertyLocalizedName);
let language = CFDataGetBytePtr(current_language);
CStr::from_ptr(language).to_str().unwrap().to_string().contains("English")
我收到此运行时错误--[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x7ff85dfed1d8
TISGetInputSourceProperty
返回一个*const __CFData
,CFDataGetBytePtr
应该返回一个*const u8
,但是它在该函数上失败,并出现上述错误。
我如何得到的语言,并检查它是什么生 rust ?TIA.
1条答案
按热度按时间cbjzeqam1#
所以我稍微研究了一下,并得出了以下结论,* 似乎 * 可以工作。注意,就我所知,这个API肯定是不赞成的。
(the以下需要
libc
作为依赖项)注意
CFStringGetCStringPtr
对我不起作用,因为它返回了一个空指针。我猜这是因为(docs):如果字符串的内部存储不允许有效地返回[指针],则[返回] NULL。