0

我知道打印线是NSLog. 如何在 Objective-C 中打印一行 10 次?

4

9 回答 9

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");
}
于 2012-07-19T21:24:31.523 回答
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;
}

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

于 2012-07-19T20:16:12.380 回答
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");
            }];
    }
}
于 2012-07-20T03:27:30.440 回答
11

以另一种方式使用Grand Central Dispatch :

dispatch_apply(10, dispatch_get_main_queue(), ^(size_t curr_iteration){
    NSLog(@"a line");
});
于 2012-07-19T19:48:44.993 回答
11

使用递归:

print(10);

void print(int i){
   if(i == 0)
      return;
   NSLog(@"print this line");
   print(i - 1);
}
于 2012-07-19T19:44:48.013 回答
9

使用 goto 语句:

  int i = 0;
print:
  NSLog(@"print this line");
  if (i++ < 10) goto print;
于 2012-07-19T19:39:00.950 回答
8

使用 for 循环:

for (int i = 0; i < 10; i++) {
    NSLog(@"print this line");
}
于 2012-07-19T19:21:48.103 回答
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);
于 2012-07-19T19:44:58.000 回答
1

使用 while 循环:

int i = 0;
while (i < 10) {
    NSLog(@"print this line");
    i += 1;
}
于 2012-07-19T19:36:43.120 回答