在Objective-C中打印一行10次?

时间:2012-07-19 19:20:20

标签: objective-c

我知道打印行是NSLog。如何在Objective-C中打印10行?

9 个答案:

答案 0 :(得分:20)

我还没有机会进行重构,但这样的事情应该有效!

for(int i = 0; i < 10; i++)
{
    if(i == 0)
        NSLog(@"A line");
    else if (i == 1)
        NSLog(@"A line");
    else if (i == 2)
        NSLog(@"A line");
    else if (i == 3)
        NSLog(@"A line");
    else if (i == 4)
        NSLog(@"A line");
    else if (i == 5)
        NSLog(@"A line");
    else if (i == 6)
        NSLog(@"A line");
    else if (i == 7)
        NSLog(@"A line");
    else if (i == 8)
        NSLog(@"A line");
    else if (i == 9)
        NSLog(@"A line");
}

答案 1 :(得分:19)

另一个while循环选项。

//
//  main.m
//  Pritner
//
//  Created by Joshua Caswell on 7/19/12.
//

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

// A Pritner instance holds and displays a passed-in string. The string is publicly
// unchangeable.
@interface Pritner : NSObject

+ (id)pritnerWithLine: (NSString *)newLine;
- (void)printLine;

@property (readonly, copy, nonatomic) NSString * line;

@end

// Extension to manage "class variable" for counting number of created instances
@interface Pritner ()

+ (NSUInteger)numPritnersCreated;
+ (void)setNumPritnersCreated:(NSUInteger)n;
+ (NSUInteger)maxNumPritners;

@property (readwrite, copy, nonatomic) NSString * line;

- (id)initWithLine: (NSString *)line;

@end

@implementation Pritner

@synthesize line;

+ (id)pritnerWithLine: (NSString *)newLine {
    id newInstance = [[self alloc] initWithLine:newLine];
    if( newInstance ){
        NSUInteger createdSoFar = [self numPritnersCreated];
        // Only allow maxNumPritners to ever be created; keeping track of them
        // is the client's problem.
        if( createdSoFar >= [self maxNumPritners] ){
            abort();
        }
        [self setNumPritnersCreated:createdSoFar + 1];
    }

    return newInstance;
}

// Fake class variable using associated objects; keep count of created instances 
char numPritnerKey;
+ (NSUInteger)numPritnersCreated {
    NSNumber * n = objc_getAssociatedObject(self, &numPritnerKey);
    if( !n ){
        n = [NSNumber numberWithUnsignedInteger:0];
        [self setNumPritnersCreated:0];
    }
    return [n unsignedIntegerValue];
}

+ (void)setNumPritnersCreated:(NSUInteger)n {
    objc_setAssociatedObject(self, 
                             &numPritnerKey, 
                             [NSNumber numberWithUnsignedInteger:n], 
                             OBJC_ASSOCIATION_RETAIN);
}

// Maximum number of instances ever allowed to be created
+ (NSUInteger)maxNumPritners {
    return 10;
}

- (id)initWithLine: (NSString *)newLine {

    self = [super init];
    if( !self ) return nil;

    line = [newLine copy];

    return self;
}

- (void)printLine {
    NSLog(@"%@", [self line]);
}


@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        while( YES ){
            Pritner * p = [Pritner pritnerWithLine:@"I figure, if you're going to build a time machine out of a car, why not do it with some style?"];
            [p printLine];
        }

    }
    return 0;
}

请不要在现实生活中使用它。

答案 2 :(得分:12)

不要忘记“Objective-C”中的“对象”。

NSArray *lines = [@"123456789" componentsSeparatedByCharactersInSet:[NSCharacterSet alphanumericCharacterSet]];
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL* stop) {
    NSLog(@"a line");
}];

如果您认为Objective-C对其Smalltalk根源不够真实,您可以执行以下操作。

typedef void (^LoopBlock)(NSNumber*);
@interface NSNumber (loop)
-(void)to:(NSNumber*)upTo do:(LoopBlock)block;
-(void)timesRepeat:(LoopBlock)block;
@end

@implementation NSNumber (loop)
static NSString *loopSeparator = @"_";
-(void)to:(NSNumber*)upTo do:(LoopBlock)block {
    [   [   [@"" stringByPaddingToLength:[upTo unsignedIntegerValue]-[self unsignedIntegerValue]
                              withString:loopSeparator
                         startingAtIndex:0
            ] componentsSeparatedByString:loopSeparator
        ] enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL* stop) {
            block([NSNumber numberWithUnsignedInteger:i+[self unsignedIntegerValue]]);
        }
    ];
}

-(void)timesRepeat:(LoopBlock)block {
    [[NSNumber numberWithUnsignedInteger: 1] to:self do:block];
}
@end


int main() {
    @autoreleasepool {
        [[NSNumber numberWithInt:10] 
          timesRepeat:^(NSNumber* i){
                NSLog(@"a line");
            }];
    }
}

答案 3 :(得分:11)

使用递归:

print(10);

void print(int i){
   if(i == 0)
      return;
   NSLog(@"print this line");
   print(i - 1);
}

答案 4 :(得分:11)

以另一种方式使用Grand Central Dispatch

dispatch_apply(10, dispatch_get_main_queue(), ^(size_t curr_iteration){
    NSLog(@"a line");
});

答案 5 :(得分:9)

使用goto语句:

  int i = 0;
print:
  NSLog(@"print this line");
  if (i++ < 10) goto print;

答案 6 :(得分:8)

使用for循环:

for (int i = 0; i < 10; i++) {
    NSLog(@"print this line");
}

答案 7 :(得分:6)

使用Grand Central Dispatch:

__block int i = 0;
__block dispatch_block_t print_block = ^() {
    NSLog(@"print this line");
    i += 1;
    if (i < 10) dispatch_sync(dispatch_get_main_queue(), print_block);
}
dispatch_sync(dispatch_get_main_queue(), print_block);

答案 8 :(得分:1)

使用while循环:

int i = 0;
while (i < 10) {
    NSLog(@"print this line");
    i += 1;
}