PDA

View Full Version : Beginner question for passing in objects




thomasjt
May 1, 2009, 04:28 AM
Hello,

Apologies for my very beginner question, but I've just been trying to optimise some old code and I'm having some problems.

My question is: is it possible to initialise new objects inside another object initialisation? For example, I've been doing this:

NSString *bob = @"bob";
NSURL *jeff = [NSURL urlWithString:@"http://bob.com"];
NSArray *namesAndSites = [NSArray arrayWithObjects: bob, jeff];

Is there a way to init objects for the array inside the array's initialisation? For example:

NSArray *namesAndSites = [NSArray arrayWithObjects: NSString mystring = whatever, NSURL urlWithString = whatever];

I am just finding it very annoying with NSArrays and NSDictionary's that I can't (or really, don't know how) to set up the array/dict and create the variables for the array/dict inside that declaration.

I hope this makes sense...any help much apprecaited.



dalvin200
May 1, 2009, 04:33 AM
why would you want to do that though? it makes it more difficult to read doesn't it?

i would stick with the method you already use...

just my opinion coming from a coding background

robbieduncan
May 1, 2009, 04:42 AM
1) You should be nil terminating your list in arrayWithObjects: as clearly stated in the documentation (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/Reference/Reference.html#//apple_ref/occ/clm/NSArray/arrayWithObjects:).

2) Yes, of course you can, although you cannot assign the objects to variables at the same time:

NSArray *namesAndSites = [NSArray arrayWithObjects: @"bob", [NSURL urlWithString:@"http://bob.com"], nil];

BlackWolf
May 1, 2009, 05:51 AM
1) You should be nil terminating your list in arrayWithObjects: as clearly stated in the documentation (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/Reference/Reference.html#//apple_ref/occ/clm/NSArray/arrayWithObjects:).

2) Yes, of course you can, although you cannot assign the objects to variables at the same time:

NSArray *namesAndSites = [NSArray arrayWithObjects: @"bob", [NSURL urlWithString:@"http://bob.com"], nil];

I'm surprised it works without the nil termination, normally the app should crash, shouldn't it?

robbieduncan
May 1, 2009, 05:57 AM
I'm surprised it works without the nil termination, normally the app should crash, shouldn't it?

I'd have thought so. I imagine that it was an example, not actual code copied and pasted from an app, especially with those variable names!

thomasjt
May 1, 2009, 08:25 AM
Thank you very much for all your help. I did terminate the arrays with nil, I just keep forgetting to do it in examples :)

So what do you think the best way is to do it? Set up the strings/urls before and then add them in, or do it all in the initialisation of the array? Is there any sort of best practice idea here?

robbieduncan
May 1, 2009, 08:29 AM
So what do you think the best way is to do it? Set up the strings/urls before and then add them in, or do it all in the initialisation of the array? Is there any sort of best practice idea here?

It's a matter of style but personally I'd say if you are declaring a variable that you will use exactly once to just pass the value to another call (like here) then you don't need that variable. Just do it all in one line.