简体   繁体   English

目标 C:如何将 NSString 转换为 UInt8 数组

[英]Objective C : how to convert NSString to UInt8 array

I had one tag in my code as below我的代码中有一个标签,如下所示

static const UInt8 publicKeyIdentifier[] = "com.example.zone\0";
Now in place of "com.example.zone" i need to use following String "nameWithZoneId" 

NSString id = @"123456";
NSString *nameWithZoneId = [@"com.example.zone" stringByAppendingString:id];

so i can use tag as dynamic way eg.所以我可以使用标签作为动态方式,例如。 "com.example.zone123456\\0"; "com.example.zone123456\\0"; Thanks in advance.提前致谢。

You could create a Tag class to keep count of the tag value for each specified identifier ( "com.example.zone" in your example).您可以创建一个Tag类来记录每个指定标识符的标签值(在您的示例中为"com.example.zone" )。

Tag.h:标签.h:

@interface Tag : NSObject
+ (NSString *)nextTagForIdentifier:(NSString *)identifier
@end

Tag.m:标签:

static NSMutableDictionary *_identifiers = nil;

@implementation Tag

+ (NSString *)nextTagForIdentifier:(NSString *)identifier
{
    if (_identifiers == nil)
        _identifiers = [NSMutableDictionary new];
    NSUInteger value = [_identifiers[identifier] unsignedInteger] + 1;
    _identifiers[identifier] = @(value);
    return [NSString stringWithFormat:@"%@%lu", identifier, value];
}

@end

And you would use it like this:你会像这样使用它:

NSString *nameWithZoneId = [Tag nextTagForIdentifier:@"com.example.zone"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM