- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 CFDictionaryGetValue
从 CFDictionaryRef
中获取 CFStringRef
。
我一直在尝试使用 CFStringGetCString
或 CFStringGetCStringPtr
将 CFStringRef
转换为 char*
,他们要么返回 NULL,要么崩溃。
有没有办法做到这一点?怎么办?
谢谢。
编辑:示例代码:
SecStaticCodeRef staticCode;
CFDictionaryRef information;
SecCSFlags flags = kSecCSInternalInformation
| kSecCSSigningInformation
| kSecCSRequirementInformation
| kSecCSInternalInformation;
CFURLRef pathURL = NULL;
CFStringRef pathStr = NULL;
CFStringRef uniqueid;
char* str = NULL;
CFIndex length;
pathStr = CFStringCreateWithCString(kCFAllocatorDefault,
filename, kCFStringEncodingUTF8);
pathURL = CFURLCreateWithString(kCFAllocatorDefault, pathStr, NULL);
SecStaticCodeCreateWithPath(pathURL, kSecCSDefaultFlags, &staticCode);
SecCodeCopySigningInformation(staticCode, flags, &information);
uniqueid = (CFStringRef) CFDictionaryGetValue(information, kSecCodeInfoUnique);
// how do I convert it here to char *?
length = CFStringGetLength(uniqueid);
str = (char *)malloc( length + 1 );
CFStringGetCString(uniqueid, str, length, kCFStringEncodingUTF8);
printf("hash of signature is %s\n", str);
CFRelease(information);
CFRelease(staticCode);
最佳答案
来自第 17 章 example code在 iOS:PTL .
char * MYCFStringCopyUTF8String(CFStringRef aString) {
if (aString == NULL) {
return NULL;
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize =
CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(aString, buffer, maxSize,
kCFStringEncodingUTF8)) {
return buffer;
}
free(buffer); // If we failed
return NULL;
}
生成的缓冲区必须始终被释放(这就是名称中包含 Copy
的原因)。链接的示例代码还有一个速度稍快的版本,它使用您提供的缓冲区。
关于将 CFStringRef 转换为 char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9166291/
我想了解从 ARC 中的 CFStringRef 获取 NSString 的正确方法?反方向也一样,ARC中的CFStringRef到NSString? 在不造成内存泄漏的情况下,正确的方法是什么?
我有一个 CFStringRef 变量,我执行检查以确保它不是 1 特定值。如果是,那么我想将其设置为 @""的 NSString 等效项 这是代码 CFStringRef data = = CFDi
我有一个应用程序可以获取用户地址簿中的名字和姓氏。我已经在模拟器和我自己的运行 iOS 7 和 XCode 5 的 iPhone 4S 上进行了测试,它运行良好。 最近,一些国际用户一直在提示我的应用
我想定义一些常量并考虑使用 #define 结构,如下所示: #define kUpdateTeamNotification CFSTR("kUpdateTeamNotification") 我的问题
我只是尝试播放苹果的 HLS 样本流。我的代码很简单: - (IBAction)playHLS:(id)sender { NSString* str = @"https://devima
我正在尝试跟踪程序中的内存使用情况,我需要知道用于字符串数据数组的总内存。 我的字符串表示为 CFStringRef,我的数组是 CFMutableArrayRef。 在字符串上,我可以调用 CFSt
能否请您告诉我在非 ARC 世界中哪种方法是正确的以及为什么。 + (NSString *)getUUID { CFUUIDRef theUUID = CFUUIDCreate(NULL); CFSt
我正在使用 CFDictionaryGetValue 从 CFDictionaryRef 中获取 CFStringRef。 我一直在尝试使用 CFStringGetCString 或 CFString
我有一个 wchar_t *,我需要在需要 CFStringref 的函数上使用它 我尝试使用 CFStringCreateWithCharacters 但我没有得到任何结果。 所以,如果我有: wc
NSString *aNSString; CFStringRef aCFString; aCFString = CFStringCreateWithCString(NULL, [aNSString U
我有一个 DTrace 探针捕获对函数的调用,该函数的参数之一是 CFStringRef。这是保存指向 unicode 字符串的指针的私有(private)结构。但 CFStringRef 本身并不是
我有以下代码片段: -(CFStringRef)setupFileName:(NSString*)_name :(NSString*)_extension { NSString* tmpName =
我正在尝试实现 CFDictionaryGetValue(),如 this appledoc 中所示 list 3: server = CFDictionaryGetValue(credentialD
我正在尝试从主包中检索一些信息: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { CFSt
我的文件中有以下代码 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"images" ofType:@"plist"];
我正在转换一个将 CFStringRef 转换为 NSString 的 Objective-C 函数,就像这样(其中 FSEventStreamCopyDescription 返回一个 CFStrin
import Foundation import MobileCoreServices func checkFileExtension(fileName: NSString){ println
我一直在寻找从 CFStringRef 出发的正确方法至NSString在 ARC 中避免内存泄漏,一些主要投票的答案建议: NSString * string = (__bridge NSStrin
我在代码中调用以下方法: NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myappname" withExtension:@"mo
如果是很基础的问题请多多包涵。我尝试使用 __bridge 将 CFStringRef 转换为 NSString,因为我启用了 ARC。 mydevUUIDString = CFUUIDCreateS
我是一名优秀的程序员,十分优秀!