iXcoder
iXcoder

Reputation: 1564

how to pass pointer and got the result in function?

please see code as follow :

- (BOOL)postAction :( NSString*) url  params:(NSDictionary*) params  bodySize:(NSNumber**)bodySize
{
... 
int size = 1999;

NSNumber* value =[[NSNumber alloc] initWithInt:size];


bodySize =  &value;


 ...}

use the function as follows:

NSNumber* size ;  
[self postAction:@"http://webserver/ss.php" params:params bodySize:&size];



// can not got the size value at all…


 int i = [size intValue]; 
//will throw nil exception !

my question is that how to correct this code above ?

many thanks for your help !

Regards

Upvotes: 0

Views: 1253

Answers (4)

bbum
bbum

Reputation: 162712

If you are using pass-by-reference in iOS or Mac OS X, you are probably doing it wrong.

Seriously-- pass by reference is an exceedingly rare pattern to use. It is pretty much entirely reserved to (NSError**) across the APIs.

Not to say that there isn't a reason to use pass-by-reference, but this isn't it.

Specifically, if you need to return an NSNumber, then return it!

- (NSNumber *) foo;

If that method returns nil, that is just as good as returning a BOOL NO. And it sets you up to follow the very common pattern of using NSError:

- (NSNumber *) foo: (NSError **) error;

Upvotes: 4

Di Wu
Di Wu

Reputation: 6448

I agree with the above two answers. To be more clear, NSNumber is an object (Unlike NSInteger or NSUInteger). So you should directly point your pointer to that object.

Upvotes: 0

Costique
Costique

Reputation: 23722

Ideally, you should always check the pointer before dereferencing it:

If ( bodySize )
    *bodySize = value;

Upvotes: 2

filipe
filipe

Reputation: 3380

I think you want this:

//bodySize =  &value;
*bodySize = value;

Upvotes: 1

Related Questions