【问题标题】:How to format string encoded with NSASCIIStringEncoding如何格式化使用 NSASCIIStringEncoding 编码的字符串
【发布时间】:2020-12-04 20:54:16
【问题描述】:

在我的 Objective-C 项目中,我正在从 cloudKit 中获取一个密钥,并想像这样对其进行格式化,并想知道格式化有什么问题,

    [cloudKitDB performQuery:query inZoneWithID:nil completionHandler:^(NSArray<CKRecord *> * _Nullable results, NSError * _Nullable error) {

    NSString *key = [[NSString alloc] initWithData:[results.firstObject objectForKey:@"keyvalue"] encoding:NSASCIIStringEncoding];
    NSLog(@"First Key Output: %@",key);
    NSLog(@"Second Key Output: %@",key);

    NSString *formattedKey = [NSString stringWithFormat:@"%@%@",[key stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]], @"1234"];
    NSLog(@"Formatted Key: %@",formattedKey);

这里是我的日志

2020-08-15 21:08:14.704650+0700 myapp[2208:79554] First Key Output: ADEvXp2F2BFJ2E4Lm2dSnYvHENhkFrK8
2020-08-15 21:08:14.704828+0700 myapp[2208:79554] Second Key Output: ADEvXp2F2BFJ2E4Lm2dSnYvHENhkFrK8
2020-08-15 21:08:14.705456+0700 myapp[2208:79554] Formatted Key: ADEvXp2F2BFJ2E4Lm2dSnYvHENhkFrK8

虽然,我尝试修剪空白,但仍然没有成功!

谢谢

【问题讨论】:

  • 我认为问题不在于 ASCII 编码。我认为可能会在键上附加一些内容,例如换行符,这会导致 1234 在下一行打印。您能否粘贴完整的输出而不只是在 cmets 中显示?
  • 您能解释一下您的“记录”输出有什么问题/出乎意料吗?看起来不错。
  • @matt 格式化的密钥输出应该是 ADEv...1234
  • 您能解释一下如何重现您的结果吗?我确实得到了“1234”。

标签: objective-c nsstring cloudkit nsstringencoding


【解决方案1】:

这是一个奇怪的问题,但我可以用下面的代码重现它。

基于此,我仍然怀疑字符串背后有一些有趣的地方,并且已经给出了在代码中修复它的方法。

        // Note C strlen will stop at the first 0
        char     * s            = "3Lm9dGmeTf3Lm9dGmeTf3Lm9dGmeTfdd\0\0\0";
        NSData   * data         = [NSData dataWithBytes:s length:strlen( s ) + 2];
        NSString * key          = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSString * keyFormatted = [NSString stringWithFormat:@"%@%@", key, @"1234"];

        NSLog ( @"Key          : %@", key );
        NSLog ( @"Formatted key: %@", keyFormatted );

        // Use strlen to get rid of funnies
        NSData   * dataFixed = [data subdataWithRange:NSMakeRange( 0, strlen( s ) )];
        NSString * keyFixed  = [[NSString alloc] initWithData:dataFixed encoding:NSASCIIStringEncoding];
        NSString * keyForm2  = [NSString stringWithFormat:@"%@%@", keyFixed, @"1234"];

        NSLog ( @"Fixed key    : %@", keyFixed );
        NSLog ( @"Formatted key: %@", keyForm2 );

        // Use C isprint to trim ... crude but works
        while ( key.length && ! isprint ( [key characterAtIndex:key.length - 1] ) )
        {
            key = [key substringToIndex:key.length - 1];
        }

        keyFormatted = [NSString stringWithFormat:@"%@%@", key, @"1234"];

        NSLog ( @"Key          : %@", key );
        NSLog ( @"Formatted key: %@", keyFormatted );

【讨论】:

  • 我很高兴有人能够复制它!干得好。
  • 马特!!!谢谢 - 我怀疑一些通讯内容被遗留在数据后面......或者编码可能不正确......但奇怪的是你可以欺骗格式丢弃最后一个参数......
  • @skaak 非常感谢您的帮助,您的解决方案有效!!!
猜你喜欢
  • 1970-01-01
  • 2018-03-30
  • 2013-03-17
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
相关资源
最近更新 更多