代码之家  ›  专栏  ›  技术社区  ›  dontWatchMyProfile

如何动态地建立NSLog的参数?

  •  0
  • dontWatchMyProfile  · 技术社区  · 14 年前

    A

    B

    C

    E

    对于这个问题,我选择NSLog是因为NSPredicate和SUBQUERY可能更简单。在使用NSMutableString和appendF时,似乎不可能动态构建NSPredicate格式字符串ormat:... 它总是导致谓词的编译错误。我猜NSPredicate提供的格式值与NSMutableString-appendFormat不同。

    因此,如果有一种方法可以为NSPredicate提供: 2) 一个巨大的,动态创建的“参数列表”

    那太酷了。

    3 回复  |  直到 14 年前
        1
  •  1
  •   Rob Napier    14 年前

    你根本的问题不应该是个问题。只是使用 +predicateWithFormat:argumentArray: . 你有什么问题要建立这个?

        2
  •  3
  •   progrmr    14 年前

    NSMutableString* logMsg = [NSMutableString stringWithFormat:@"%@ %@ %@", A, B, C];
    
    if (C) [logMsg appendFormat:@" %@", C];
    if (D) [logMsg appendFormat:@" %@", D];
    
    [logMsg appendFormat:@" %@ %@", E, F];
    
    NSLog(@"%@", logMsg);
    
        3
  •  1
  •   ohhorob    14 年前

    如果您正在收集一次要输出的字符串的可变列表,只需使用 NSMutableArray 根据需要添加一行日志输出。然后在流程结束时,使用字符串连接组件:

    NSMutableArray *logLines = [[NSMutable alloc] initWithCapacity:10];
    ...
    NSLog(@"Multiple-line output:\n%@",[logLines componentsJoinedByString:@"\n"]);
    [logLines release];