2015-03-27 9 views
1

Какие строки кода позволяют печатать итерации посредством быстрого перечисления без повторения предваряющего утверждения? Мой код выглядит следующим образом:Распечатка для быстрого перечисления

#import <Foundation/Foundation.h> 

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

     // Create an empty mutable array 
     NSMutableArray *listGroceries = [NSMutableArray array]; 

     // Add two dates to the array 
     [listGroceries addObject:@"Container of milk"]; 
     [listGroceries addObject:@"Stick of butter"]; 

     // Add yesterday at the beginning of the list 
     [listGroceries insertObject:@"Loaf of bread" atIndex:0]; 

     // How many dates are in the array? 
     NSLog(@"There are %lu Groceries", [listGroceries count]); 

     for (NSDate *d in listGroceries) { 
      NSLog(@"My grocery list is: %@", d); 
     } 

    } 
    return 0; 
} 

Результат кода заключается в следующем:

2015-03-26 14:05:42.553 Groceries[3131:129994] My grocery list is: Loaf of bread 
2015-03-26 14:05:42.553 Groceries[3131:129994] My grocery list is: Container of milk 
2015-03-26 14:05:42.553 Groceries[3131:129994] My grocery list is: Stick of butter 

Как сделать программу производить следующее без My grocery list is повторяется каждую строку впоследствии:

My grocery list is: 
    Loaf of bread 
    Container of milk 
    Stick of butter 
+0

использовать printf() вместо NSLog и применять действительную логику – itsji10dra

ответ

1

Вот ваш ответ:

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

    // Create an empty mutable array 
    NSMutableArray *listGroceries = [NSMutableArray array]; 

    // Add two dates to the array 
    [listGroceries addObject:@"Container of milk"]; 
    [listGroceries addObject:@"Stick of butter"]; 

    // Add yesterday at the beginning of the list 
    [listGroceries insertObject:@"Loaf of bread" atIndex:0]; 

    // How many dates are in the array? 
    NSLog(@"There are %lu Groceries", [listGroceries count]); 

    printf("My grocery list is: \n"); 

    for (NSString *d in listGroceries) { 

     printf("%s \n", d.UTF8String); 
    } 


    return 0; 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^