My Code For The First Challenge


#1

This is my code, it’s not the best, but it works. Is it right?

int main (int argc, const char * argv[]) { @autoreleasepool { NSString *one = [ NSString stringWithFormat:@"Cups"]; NSString *two = @"Pen"; NSString *three = [NSString stringWithFormat:@"Milk"]; NSMutableArray *shoppingList = [NSMutableArray array]; [shoppingList addObject:two]; [shoppingList addObject:three]; [shoppingList insertObject:one atIndex:0]; for (NSString *s in shoppingList) NSLog(@"buy %@", shoppingList); } return 0; }


#2

The code looks fine to me, though it is a little long. Here is mine for comparison.

//Create a Mutable array and add some grocery items
NSMutableArray *groceriesList = [NSMutableArray arrayWithObjects:@"Bacon", @"Eggs", @"Peanut butter", @"Milk", @"Chicken", nil];
        
//iterate through array to print out list
for (NSString *g in groceriesList) {
     NSLog(@"-%@", g);

I added a little dash to the beginning of each grocery item just for flare.

Also to add objects to the array you don’t need to make an NSString Object you can just use the insertObject: atIndex: method and type in the string on the fly. Like so…

[groceriesList insertObject:@"White rice" atIndex:2];

#3

[quote=“Sherlock”]This is my code, it’s not the best, but it works. Is it right?

int main (int argc, const char * argv[]) { @autoreleasepool { NSString *one = [ NSString stringWithFormat:@"Cups"]; NSString *two = @"Pen"; NSString *three = [NSString stringWithFormat:@"Milk"]; NSMutableArray *shoppingList = [NSMutableArray array]; [shoppingList addObject:two]; [shoppingList addObject:three]; [shoppingList insertObject:one atIndex:0]; for (NSString *s in shoppingList) NSLog(@"buy %@", shoppingList); } return 0; }[/quote]
I can’t tell if it is wrong, but I can tell it is a bit more steps than needed.
It is right to make strings for the groceries, but you can add string objects in a neater way, as indicated by mwmatter below.

If you prefer to declare the groceries outside of the array, instead of inside, like mwmatter does, you could use
[shoppingList addObject:@“Milk”];
etc without first assigning Milk to an NSString object one, two three.

I think that your use of stringWithFormat may be wrong; the only stringsWFormat I see so far, are strings with a token in it, like %@.

But… maybe I have it all wrong, hopefully someone else will confirm of correct this.


#4

No, it is not wrong:

NSString *one = [NSString stringWithFormat:@"Cups"];
...
NSString *three = [NSString stringWithFormat:@"Milk"];

It is just that no conversion specifiers exist in the format strings and thus no arguments follow.


#5

Okay, that’s good to know.

Why would we use the stringWFormat like this:

instead of

Or are they not the same?


#6

They are functionally the same, but the first one could be wasteful if the the compiler does not optimise it.
Therefore, it is better to use:

NSString *one = @"Cups"

#7

Thanks, I will keep that in mind.


#8

[quote=“mwmatter”]The code looks fine to me, though it is a little long. Here is mine for comparison.

//Create a Mutable array and add some grocery items
NSMutableArray *groceriesList = [NSMutableArray arrayWithObjects:@"Bacon", @"Eggs", @"Peanut butter", @"Milk", @"Chicken", nil];
        
//iterate through array to print out list
for (NSString *g in groceriesList) {
     NSLog(@"-%@", g);

I added a little dash to the beginning of each grocery item just for flare.

Also to add objects to the array you don’t need to make an NSString Object you can just use the insertObject: atIndex: method and type in the string on the fly. Like so…

[groceriesList insertObject:@"White rice" atIndex:2]; [/quote]

Simple and beautiful !