I am coming to Objective-C from C# without any intermediate knowledge of C. (Yes, yes, I will need to learn C at some point and I fully intend to.) In Apple's Certificate, Key, and Trust Services Programming Guide, there is the following code:
我是从C#来到Objective-C而没有任何C语言的中间知识。(是的,是的,我需要在某些时候学习C并且我完全打算这样做。)在Apple的证书,密钥和信任服务编程指南中,那里是以下代码:
static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0";
static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0";
I have an NSString
that I would like to use as an identifier here and for the life of me I can't figure out how to get that into this data structure. Searching through Google has been fruitless also. I looked at the NSString Class Reference and looked at the UTF8String
and getCharacters
methods but I couldn't get the product into the structure.
我有一个NSString,我想在这里使用它作为标识符,对于我的生活,我无法弄清楚如何将其纳入这个数据结构。通过谷歌搜索也没有结果。我查看了NSString类参考,并查看了UTF8String和getCharacters方法,但我无法将产品放入结构中。
What's the simple, easy trick I'm missing?
我错过了什么简单易行的技巧?
4 个解决方案
#1
I think some people are missing the point here. Everyone has explained the two constant arrays that are being set up for the tags, but if you want to use an NSString
, you can simply add it to the attribute dictionary as-is. You don't have to convert it to anything. For example:
我想有些人在这里忽略了这一点。每个人都解释了为标记设置的两个常量数组,但是如果要使用NSString,则可以简单地将其添加到属性字典中。您不必将其转换为任何内容。例如:
NSString *publicTag = @"com.apple.sample.publickey";
NSString *privateTag = @"com.apple.sample.privatekey";
The rest of the example stays exactly the same. In this case, there is no need for the C string literals at all.
示例的其余部分保持完全相同。在这种情况下,根本不需要C字符串文字。
#2
Those are C strings: Arrays (not NSArrays, but C arrays) of characters. The last character is a NUL, with the numeric value 0.
那些是C字符串:字符的数组(不是NSArrays,但是C数组)。最后一个字符是NUL,数值为0。
“UInt8” is the CoreServices name for an unsigned octet, which (on Mac OS X) is the same as an unsigned char.
“UInt8”是无符号八位字节的CoreServices名称,(在Mac OS X上)与unsigned char相同。
static
means that the array is specific to this file (if it's in file scope) or persists across function calls (if it's inside a method or function body).
static表示该数组特定于此文件(如果它在文件范围内)或者在函数调用之间保持不变(如果它在方法或函数体内)。
const
means just what you'd guess: You cannot change the characters in these arrays.
const意味着你猜的是:你不能改变这些数组中的字符。
\0
is a NUL, but including it explicitly in a ""
literal as shown in those examples is redundant. A ""
literal (without the @
) is NUL-terminated anyway.
\ 0是一个NUL,但是在那些示例中所示的“”文字中明确地包含它是多余的。无论如何,“”字面(没有@)是NUL终止的。
C doesn't specify an encoding. On Mac OS X, it's generally something ASCII-compatible, usually UTF-8.
C未指定编码。在Mac OS X上,它通常与ASCII兼容,通常是UTF-8。
To convert an NSString to a C-string, use UTF8String
or cStringUsingEncoding:
. To have the NSString extract the C string into a buffer, use getCString:maxLength:encoding:
.
要将NSString转换为C字符串,请使用UTF8String或cStringUsingEncoding:。要让NSString将C字符串提取到缓冲区中,请使用getCString:maxLength:encoding:。
#3
Obtaining a char*
(C string) from an NSString isn't the tricky part. (BTW, I'd also suggest UTF8String, it's much simpler.) The Apple-supplied code works because it's assigning a C string literal to the static const
array variables. Assigning the result of a function or method call to a const
will probably not work.
从NSString获取char *(C字符串)并不是一个棘手的部分。 (顺便说一句,我也建议使用UTF8String,它更简单。)Apple提供的代码可以工作,因为它为静态const数组变量分配了一个C字符串文字。将函数或方法调用的结果赋给const可能不起作用。
I recently answered an SO question about defining a constant in Objective-C, which should help your situation. You may have to compromise by getting rid of the const
modifier. If it's declared static
, you at least know that nobody outside the compilation unit where it's declared can reference it, so just make sure you don't let a reference to it "escape" such that other code could modify it via a pointer, etc.
我最近回答了一个关于在Objective-C中定义常量的问题,这应该有助于你的情况。你可能不得不通过摆脱const修饰符来妥协。如果它被声明为静态,你至少知道编译单元之外没有人声明它可以引用它,所以只要确保你不要让它的引用“转义”,这样其他代码就可以通过指针等修改它。 。
However, as @Jason points out, you may not even need to convert it to a char* at all. The sample code creates an NSData object for each of these strings. You could just do something like this within the code (replacing steps 1 and 3):
但是,正如@Jason指出的那样,你甚至可能根本不需要将它转换为char *。示例代码为每个字符串创建一个NSData对象。您可以在代码中执行类似的操作(替换步骤1和3):
NSData* publicTag = [@"com.apple.sample.publickey" dataUsingEncoding:NSUnicodeStringEncoding];
NSData* privateTag = [@"com.apple.sample.privatekey" dataUsingEncoding:NSUnicodeStringEncoding];
That sure seems easier to me than dealing with the C arrays if you already have an NSString.
如果你已经拥有一个NSString,这对我来说比处理C数组更容易。
#4
try this
NSString *newString = @"This is a test string.";
char *theString;
NSString * newString = @“这是一个测试字符串。”; char * theString;
theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];
theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];
#1
I think some people are missing the point here. Everyone has explained the two constant arrays that are being set up for the tags, but if you want to use an NSString
, you can simply add it to the attribute dictionary as-is. You don't have to convert it to anything. For example:
我想有些人在这里忽略了这一点。每个人都解释了为标记设置的两个常量数组,但是如果要使用NSString,则可以简单地将其添加到属性字典中。您不必将其转换为任何内容。例如:
NSString *publicTag = @"com.apple.sample.publickey";
NSString *privateTag = @"com.apple.sample.privatekey";
The rest of the example stays exactly the same. In this case, there is no need for the C string literals at all.
示例的其余部分保持完全相同。在这种情况下,根本不需要C字符串文字。
#2
Those are C strings: Arrays (not NSArrays, but C arrays) of characters. The last character is a NUL, with the numeric value 0.
那些是C字符串:字符的数组(不是NSArrays,但是C数组)。最后一个字符是NUL,数值为0。
“UInt8” is the CoreServices name for an unsigned octet, which (on Mac OS X) is the same as an unsigned char.
“UInt8”是无符号八位字节的CoreServices名称,(在Mac OS X上)与unsigned char相同。
static
means that the array is specific to this file (if it's in file scope) or persists across function calls (if it's inside a method or function body).
static表示该数组特定于此文件(如果它在文件范围内)或者在函数调用之间保持不变(如果它在方法或函数体内)。
const
means just what you'd guess: You cannot change the characters in these arrays.
const意味着你猜的是:你不能改变这些数组中的字符。
\0
is a NUL, but including it explicitly in a ""
literal as shown in those examples is redundant. A ""
literal (without the @
) is NUL-terminated anyway.
\ 0是一个NUL,但是在那些示例中所示的“”文字中明确地包含它是多余的。无论如何,“”字面(没有@)是NUL终止的。
C doesn't specify an encoding. On Mac OS X, it's generally something ASCII-compatible, usually UTF-8.
C未指定编码。在Mac OS X上,它通常与ASCII兼容,通常是UTF-8。
To convert an NSString to a C-string, use UTF8String
or cStringUsingEncoding:
. To have the NSString extract the C string into a buffer, use getCString:maxLength:encoding:
.
要将NSString转换为C字符串,请使用UTF8String或cStringUsingEncoding:。要让NSString将C字符串提取到缓冲区中,请使用getCString:maxLength:encoding:。
#3
Obtaining a char*
(C string) from an NSString isn't the tricky part. (BTW, I'd also suggest UTF8String, it's much simpler.) The Apple-supplied code works because it's assigning a C string literal to the static const
array variables. Assigning the result of a function or method call to a const
will probably not work.
从NSString获取char *(C字符串)并不是一个棘手的部分。 (顺便说一句,我也建议使用UTF8String,它更简单。)Apple提供的代码可以工作,因为它为静态const数组变量分配了一个C字符串文字。将函数或方法调用的结果赋给const可能不起作用。
I recently answered an SO question about defining a constant in Objective-C, which should help your situation. You may have to compromise by getting rid of the const
modifier. If it's declared static
, you at least know that nobody outside the compilation unit where it's declared can reference it, so just make sure you don't let a reference to it "escape" such that other code could modify it via a pointer, etc.
我最近回答了一个关于在Objective-C中定义常量的问题,这应该有助于你的情况。你可能不得不通过摆脱const修饰符来妥协。如果它被声明为静态,你至少知道编译单元之外没有人声明它可以引用它,所以只要确保你不要让它的引用“转义”,这样其他代码就可以通过指针等修改它。 。
However, as @Jason points out, you may not even need to convert it to a char* at all. The sample code creates an NSData object for each of these strings. You could just do something like this within the code (replacing steps 1 and 3):
但是,正如@Jason指出的那样,你甚至可能根本不需要将它转换为char *。示例代码为每个字符串创建一个NSData对象。您可以在代码中执行类似的操作(替换步骤1和3):
NSData* publicTag = [@"com.apple.sample.publickey" dataUsingEncoding:NSUnicodeStringEncoding];
NSData* privateTag = [@"com.apple.sample.privatekey" dataUsingEncoding:NSUnicodeStringEncoding];
That sure seems easier to me than dealing with the C arrays if you already have an NSString.
如果你已经拥有一个NSString,这对我来说比处理C数组更容易。
#4
try this
NSString *newString = @"This is a test string.";
char *theString;
NSString * newString = @“这是一个测试字符串。”; char * theString;
theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];
theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];