开发者

Releasing an object that can be [NSNull null] or not

开发者 https://www.devze.com 2023-04-09 03:16 出处:网络
I have a memory management problem I can\'t get my head around on iOS. I\'m getting data from a SQLite db where some cells can be empty. So to handle this case, I assign [NSNull null] to my recipient

I have a memory management problem I can't get my head around on iOS. I'm getting data from a SQLite db where some cells can be empty. So to handle this case, I assign [NSNull null] to my recipient if empty, or the value if not:

NSString *email = (const char *) sqlite3_column_text(statement, 6) == NULL ? [NSNull null] : [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)];

But then if I do that:

[email release];

the analyser doesn't like it and I think it crashes my program when the object is actually [NSNull null].

So I've tried:

(id) email == [NSNull null] ? nil:[email release];

But it doesn't work (still crashes and analyser doesn't like it). Any ideas?

Thanks in adv开发者_Python百科ance


I'm using in that case nil value. For example:

NSString *email = (const char *) sqlite3_column_text(statement, 6) == NULL ? nil : [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 6)];

In such case you can safely call [email release]; // email can be NSString or nil and all will work as you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消