如何将[Number numberWithChar:c]的NSArray加入NSString中? [英] How do you join an NSArray of [Number numberWithChar: c]'s into an NSString?

查看:224
本文介绍了如何将[Number numberWithChar:c]的NSArray加入NSString中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 componentsJoinedByString 时,我只需要一个长字符串。

When I use componentsJoinedByString, I just get a long string of digits.

任务是混乱。为什么地球上会有人填充NSArray与NSNumbers如果他们想要一个NSString?答案是我正在写一个可扩展单元测试框架。基本功能 genNum genBool genChar 生成NSNumbers分别使用随机int,BOOL和char值。然后有 genArray ,它使用指定的生成器生成一个随机数组。因此,要构造一个随机的NSString,可以使用 genChar 生成器运行 genArray ,并将生成的NSArray转换为NSString 。

I realize this task is confusing. Why on earth would anyone populate an NSArray with NSNumbers if they wanted an NSString? The answer is that I'm writing an extensible unit test framework. The basic functions genNum, genBool, and genChar generate NSNumbers with random int, BOOL, and char values respectively. Then there's genArray which generates a random array using a specified generator. So to construct a random NSString, one would run genArray using the genChar generator, and transform the resulting NSArray into an NSString.

字符仅存储为 NSNumber s,而不是 char s由于技术性: genArray 接受块,并调用块100次填充NSArray。块必须保存ObjC对象;不是原始类型。

The characters are only stored as NSNumbers instead of chars due to a technicality: genArray accepts a block and calls the block 100-odd times to populate the NSArray. Blocks must hold ObjC objects; not primitive types.

所以问题仍然是:如何将[NSNumber numberWithChar:c]的NSArray加入NSString?

So the question remains: How do you join an NSArray of [NSNumber numberWithChar: c]'s into an NSString?

推荐答案

+ (NSString *) genString {
    NSArray* arr = [self genArray: ^() { return [ObjCheck genChar]; }];

    NSMutableString* s = [NSMutableString stringWithCapacity: [arr count]];

    int i;
    for (i = 0; i < [arr count]; i++) {
        [s appendString: [NSString stringWithFormat: @"%c", [[arr objectAtIndex: i] charValue]]];
    }

    return s;
}

这篇关于如何将[Number numberWithChar:c]的NSArray加入NSString中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆