如何检查对象是ios中的字符串或数组

时间:2013-10-10 04:11:01

标签: ios nsstring nsmutablearray

我从两个不同的Web服务获取响应数组。但对于相同的方法。问题是有2种不同的Web服务给我一些不同的响应。这些是那些数组

(NSMutableArray *) $2 = 0x003e9210 <__NSArrayM 0x3e9210>(

{

addedOn = "03/09/2013";

album = "Surendra Perera";

artistGroup = Female;

artists = "Surendra Perera";

bpm = 0;

categories = "Love Songs";

duration = "250.00";

energy = "";

era = Millenium;

extroTime = "0.00";

extroType = "";

genders = "";

id = 50;

imageUrl = "http://sample.com/CloudMusic/Images/sngfile.png";

introTime = "0.00";

language = Sinhala;

lyrics = "";

mediaUrl = "http://sample.com/CloudMusic/Music/0476/50.mp3";

moods = Lonely;

movie = "";

musicLabel = Evoke;

musician = "";

sDuration = "00:04:10";

soundCodes = "";

tempos = "";

textures = "";

thumbUrl = "http://sample.com/CloudMusic/Images/sngfile.png";

title = "Mee Mai Gaha";

writer = "";

year = "";

}

)

其他人

addedOn = "19/09/2013";

albumName = Massina;

artists =     (

            {

        artistGroup = 0;

        description = "<null>";

        id = 290;

        imageUrl = "<null>";

        name = Daddy;

        noOfSongs = 0;

        thumbUrl = "<null>";

    }

);

duration = "260.00";

id = 2575;

imageUrl = "http://sample.com/CloudMusic/Images/sngfile.png";

mediaUrl = "http://sample.com/CloudMusic/Music/0905/2575.mp3";

sDuration = "00:04:20";

songMoods =     (

            {

        id = 3;

        name = Sad;

        noOfSongs = 0;

    }

);

thumbUrl = "http://sample.com/CloudMusic/Images/sngfile.png";

title = "Aai Kale";

year = "";

}

)

我想要做的就是为这个艺术家阵列做准备。如何检查这些艺术家作为一个数组或只是一个字符串。请帮帮我。

由于

2 个答案:

答案 0 :(得分:3)

这将说明您的问题 在_recievedData中获取数据rx,然后检查对象的类。

id object = [NSJSONSerialization
                 JSONObjectWithData:_recievedData
                 options:kNilOptions
                 error:&error];
if (error)
{
     NSLog(@"Error in rx data:%@",[error description]);
}
if([object isKindOfClass:[NSString class]] == YES)
{
     NSLog(@"String rx from server");
}
else if ([object isKindOfClass:[NSDictionary class]] == YES)
{
     NSLog(@"Dictionary rx from server");
}
else if ([object isKindOfClass:[NSArray class]] == YES)
{
     NSLog(@"Array rx from server");
}

答案 1 :(得分:0)

这应该对你有帮助。

id object = [responseData objectForKey:@"artists"];

if ([object isMemberOfClass:[NSString class]]) {
    // do the stuff for NSString
}
else if ([object isMemberOfClass:[NSArray class]]) {
    // do the stuff for NSArray
}