简体   繁体   English

使用id代替NSString

[英]Using id in place of an NSString

I'm parsing some JSON. 我正在解析一些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. 有时,JSON返回一个NSNull,我允许它进入一个字符串,然后我将检查String,以查看它是否与NSNull冲突,然后进行处理。 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 在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 所以我虽然正确的处理方式可能是在NSString位置更改为id

Monograph.h Monograph.h

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

But this still generated another warning 但这仍然产生了另一个警告

passing argument 1 of 'setCautions:' from incompatible pointer type 从不兼容的指针类型传递'setCautions:'的参数1

What is the correct way to handle this? 处理此问题的正确方法是什么? I'm guessing letting NSNull go into an NSString object was wrong. 我猜测让NSNull进入NSString对象是错误的。 But putting an id object in its place didnt seem right. 但是,将id对象放在其位置似乎并不正确。

Many Thanks -Code 非常感谢-Code

An id is already declared as a pointer to an NSObject so get rid of the asterisk: 一个id已经被声明为一个指向NSObject的指针,因此不要使用星号:

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

Using NSString is perfect; 使用NSString非常完美; just modify the condition to 只需将条件修改为
if ((theMonograph.cautions != nil) && (theMonograph.cautions != [NSNull null]) && [theMonograph.cautions length])
Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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