Reputation: 1564
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
Reputation: 162712
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
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
Reputation: 23722
Ideally, you should always check the pointer before dereferencing it:
If ( bodySize )
*bodySize = value;
Upvotes: 2