简体   繁体   中英

Using id in place of an NSString

I'm parsing some JSON.

Sometimes the JSON returns an NSNull which i was allowing to go into a string and then i would check the String to see if it contrained the NSNull and then handled it. The code worked as intended but the compiler complained with a warning.

if ((theMonograph.cautions != nil) && (theMonograph.cautions != [NSNull null]))
{   
    //draw string
}

In Monograph.h

NSString *cautions;
@property (nonatomic, retain) NSString *cautions;

So I though maybe the correct way to handle it would be to change to an id in the place of the NSString

Monograph.h

id *cautions;
@property (nonatomic, retain) id *cautions;

But this still generated another warning

passing argument 1 of 'setCautions:' from incompatible pointer type

What is the correct way to handle this? I'm guessing letting NSNull go into an NSString object was wrong. But putting an id object in its place didnt seem right.

Many Thanks -Code

An id is already declared as a pointer to an NSObject so get rid of the asterisk:

id cautions;
@propertry (nonatomic, retain) id cautions;

Using NSString is perfect; just modify the condition to
if ((theMonograph.cautions != nil) && (theMonograph.cautions != [NSNull null]) && [theMonograph.cautions length])
Hope it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM