0
我想知道CoreFoundation中的 +[NSString stringWithContentsOfURL:usedEncoding:error:]
的等價物是什麼?相當於+ [NSString stringWithContentsOfURL:usedEncoding:error:] for CFStringRef
我知道我可以用(__bridge NSString *)
東西這個時候,我相當複雜我的生活了一下,瞭解了一下這怎麼做可可出來之前...... :)
我的口味是醜陋的有夫婦在Objective-C線,使事情
...
CFErrorRef error = NULL;
CFURLRef fileURL = CFBundleCopyResourceURL(bundle,
CFSTR("file"),
CFSTR("txt"),
NULL);
// Ugly piece of objc code in my whole C source file :(
NSError *nsError = (__bridge NSError *)(error);
NSString *nsString = [NSString stringWithContentsOfURL:(__bridge NSURL *)fileURL
usedEncoding:NULL
error:&nsError];
CFStringRef fileContents = (__brigde CFStringRef)nsString;
...
這幾乎是我想要的東西......雖然'-stringWithContentsOfURL:usedEncoding:錯誤:'會嘗試猜測文件的編碼,而我必須在不知道編碼的情況下讀取文件時將編碼傳遞給CFStringCreateFromExternalRepresentation()#: – nacho4d
可以嘗試迭代多個具有不同編碼集的'CFStringCreateExternalRepresentation'並將'lossByte'指定爲0所以如果函數不能使用編碼,函數返回NULL。如果這有幫助,我會編輯我的答案! :) –