印刷行はNSLog
です。Objective-Cで1行を10回印刷するにはどうすればよいですか?
質問する
1359 次
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
再帰を使用する:
print(10);
void print(int i){
if(i == 0)
return;
NSLog(@"print this line");
print(i - 1);
}
于 2012-07-19T19:44:48.013 に答える
11
グランドセントラルディスパッチを別の方法で使用します。
dispatch_apply(10, dispatch_get_main_queue(), ^(size_t curr_iteration){
NSLog(@"a line");
});
于 2012-07-19T19:48:44.993 に答える
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
グランドセントラルディスパッチを使用する:
__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 に答える