【问题标题】:How would I output this array of strings?我将如何输出这个字符串数组?
【发布时间】:2011-04-08 02:50:50
【问题描述】:

假设我有一个字符串数组...

[NSArray arrayWithObjects: @"red", @"blue", @"green", @"yellow", nil]

我将如何实现这样的输出...?

红色是一种颜色
蓝色是一种颜色
绿色是一种颜色
黄色是一种颜色

谢谢!

【问题讨论】:

  • 你想要什么语言的?
  • 你希望这个输出去哪里?到文件,到标准输出,到标准错误?

标签: objective-c arrays string


【解决方案1】:
NSArray *colors = ...;
for (NSString *color in colors) {
  NSLog(@"%@ is a color", color);
}

【讨论】:

  • 不要将 NSLog() 用于普通字符串输出。它预先添加了很多问题没有要求的信息。
  • 好吧,我在 xcode 中使用 Obj c,我需要输出为 NSString 格式,以便我可以在 textView 中显示结果列表
【解决方案2】:
NSArray *initializedNSArray = [NSArray arrayWithObjects: @"red", 
                                                         @"blue", 
                                                         @"green", 
                                                         @"yellow", 
                                                         nil]
for( int i=0; i<4; ++i )
    NSLog(@"%@ is a color \n", [initializedNSArray objectAtIndex: i];
                             // ^^^^^^^^^^^^^^^^^  to array it is earlier initialized to

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2020-08-30
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多