ios - NSString Category Method Not Getting Called -
the problem trying solve convert nil strings empty strings can put value in nsdictionary
.
i have following code:
//accessed in class prospect.title = [self.titlelabel.text emptywhennil]; // nsstring+additions category -(nsstring *) emptywhennil { if(self == nil) return @""; return self; }
but reason emptywhennil
method never invoked!! lost..
the problem is, when string pointer nil
, have no object send category message to. messages nil
not executed (and return nil
) in objective-c.
if want put "null" objects collection, such nsdictionary
, or nsarray
, can use nsnull
class instead.
alternatively implement method class method:
+ (nsstring *)emptystringifnil:(nsstring *)str { if (!str) return @""; return str; }
you call
prospect.title = [nsstring emptystringifnil:self.titlelabel.text];
Comments
Post a Comment