Objective C pointer to pointer - how to distinguish passing nil vs not initialized variable set to nil -
i have method accepts pointer pointer param:
+(void)somemethod:(nsstring*_nullable*_nullable)parsed; now, if call method , pass "nil" parameter, , dereference inside of method like:
*parsed = soemthing; i crash bad access. question is, how construct condition distinguish passing "nil" vs passing not initilized variable like:
nsstring* s; // technically nil too, dereference works , doesn't crash [someobj somemethod:&s]; to prevent crash mentioned in first case.
the solution relatively simple, should never pass uninitialized variable. consider doing this:
nsstring *s = nil; [someobj somemethod:&s]; note &s have declared somemethod require nsstring **. doing &s parsed defined, variable stored somewhere in memory. value, stored @ *parsed have been initialized nil. should able things like:
+(void)somemethod:(nsstring* _nullable *_nullable)parsed { if( null != parsed ) { if( nil == *parsed ) { *parsed = @"our variable *s set!"; } } } enjoy!
Comments
Post a Comment