迭代包含字符串的 NSMutableArray

发布于 2024-12-15 17:31:53 字数 372 浏览 3 评论 0原文

我将字符串添加到 NSMutableArray 中,现在我想迭代该数组:

    NSEnumerator *e = [numbers objectEnumerator];
    id object;

    NSMutableString* theString = [NSMutableString string];

    while (object = [e nextObject]) {
        [theString appendString:[NSString stringWithFormat:@"%i;", object]];
    }

但实际上 object 似乎不是我添加到数组中的字符串,而只是一个数字。我怎样才能得到字符串?

I add strings to an NSMutableArray and now I want to iterate that array:

    NSEnumerator *e = [numbers objectEnumerator];
    id object;

    NSMutableString* theString = [NSMutableString string];

    while (object = [e nextObject]) {
        [theString appendString:[NSString stringWithFormat:@"%i;", object]];
    }

But actually object does not seem to be the string I added to the array but just a number. How can I get the string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-12-22 17:31:53

只需附加字符串即可。无需(昂贵的)格式转换。

NSMutableString *theString = [NSMutableString string];

for (NSString *number in numbers)
    [theString appendString:number];

Just append the string. No need for (expensive) format conversion.

NSMutableString *theString = [NSMutableString string];

for (NSString *number in numbers)
    [theString appendString:number];
拍不死你 2024-12-22 17:31:53

"%i" 是整数的格式字符串,因此它将对象的地址视为整数。对象的格式为"%@"

"%i" is the format string for an integer, so it's treating the object's address as an integer. The format for an object is "%@".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文