How do you benefit from creating an NSArray in Objective-C?
Well, I've been learning Objective-C for a while now, and I don't get why creating an NSArray would be beneficial for you. It's just a collection of some stuff right? Why can't yo开发者_运维百科u just use them without making an NSArray. Or can you use the objects in it in the implementation of every one of your methods (even if it's a local ivar).
So, any help would be appreciated. Thanks guys!
You could make instance variables for every “item” you need:
NSString *str1;
NSString *str2;
NSString *str3;
…but that’s hard to work with, an array is simply more convenient. What would you do if you wanted to print all these strings?
NSLog(@"%@", str1);
NSLog(@"%@", str2);
NSLog(@"%@", str3);
Wouldn’t it be easier to loop over an array?
NSArray *strings = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
for (NSString *str in strings)
NSLog(@"%@", str);
How about if you have ten, twenty strings? And what if you don’t know how many strings you will need? What if you want to pass all these items to somebody else? Are you going to pass them one by one?
- (void) doSomethingWithString1: (NSString*) str1 andString2: (NSString*) str2…;
Or would you rather pass an array?
- (void) doSomethingWithMyStrings: (NSArray*) strings;
Very often, the NSArray
you see is a front for an NSMutableArray
working behind the scenes.
Billing it in the interface as an NSArray
is just a way to ensure that it cannot be manipulated from without, which could have moderately disastrous consequences. (Thus, when asking for an NSArray
from an object, you'll usually get a copy of the NSMutableArray
used internally.)
Understanding why creating an NSMutableArray
is beneficial is left as an exercise to the reader.
Different contexts.
When you use an NSString
it's generally used for a single value. It's quite simple.
NSArray
If you want to loop through many keys/values, use an NSArray
. It is not possible to loop through NSStrings
because they don't have the order structer that NSArray
's do.
Why use NSString
It would be silly to call myArray[0]
and foo[0]
every time you saved just a value. NSString is simpler than NSArray, since you can easily set a value on a GUI element (without having to call myArray[0
])
Why use NSArray
It is more flexible than NSString
because NSArray
can hold multiple values, which may be needed in certain situations.
Weighing pros and cons
Pros (NSArray
):
Can make your code cleaner (call myArray[1]
and myArray[0]
instead of mystring
and foo
)
Simplified memory management (easier to release one NSArray
than many NSStrings
)
Cons (NSArray)
:
Can make your code very complicated (was that on myArray[0
] or 1?)
Might be difficult to know what type of data is in the array (if it's an NSString
, it's always going to be a string, an array can hold many different types of data). You wouldn't use 50 tissues to dry yourself off from the shower but use a towel.
So it boils down to one thing. What context are you using?
精彩评论