0

I have an NSArray that has been populated by:

NSString *strURL = [NSString stringWithFormat:@"%@%@", @"http://website/getinfo.php?dept=", dept];
NSArray *arrayDetail = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];

The SQL database is empty but following the call arrayDetail has 'one object' in it but I have no idea what this object is.

I have tried:

NSLog(@"value - %@", [arrayDetail objectAtIndex:0]);

The NSLog displayed is "value - "

I have tried:

if ([arrayDetail objectAtIndex:0]  == (id)[NSNull null])
{
   NSLog(@"null");
}
else
{
 //do other stuff
}

but it skips over to 'do other stuff'.

I have also tried:

if ([[arrayDetail objectAtIndex:0]  isEqualToString:@""])

but again it skips to 'do other stuff'.

Any ideas?

UPDATE:

Following the advice from below I have tried:

 NSLog(@"value - %@", [arrayDetail class])

with the response:

value - __NSCFConstantString

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • What about breakpoint and `NSLog(@"%@", [arrayDetail.lastObject class])`? – Valentin Shamardin Feb 19 '14 at 12:33
  • See this http://stackoverflow.com/a/19059374/716216 – Mick MacCallum Feb 19 '14 at 12:34
  • 1
    Please use NSLog(@"%@", arrayDetail); then you can check any value on 0 index. – Sandeep Agrawal Feb 19 '14 at 12:36
  • If you do `NSLog(@"%@", arrayDetail);` it should print out `(` and `)` characters with the array elements in-between. If not, then you probably received a `nil` value back from the array create operation because the URL was bogus. My guess is that the URL is bogus. – Hot Licks Feb 19 '14 at 12:57
  • This is the result: value - ( "" ) . I know the URL is not bogus as I get the correct results when data is in the SQL database. – RGriffiths Feb 19 '14 at 12:59
  • @SandeepAgrawal - see previous comment – RGriffiths Feb 19 '14 at 13:01
  • @RichardGriffiths your approach can't work, the database is not delivering a string representation of NSArray but whatever else. You'll need to parse the result and create an NSArray from that. – Volker Feb 19 '14 at 13:31
  • If there is data in the database it works fine. If there is not the return is (""). I think the issue is how initWithContentsOfURL deals with a return of (""). – RGriffiths Feb 19 '14 at 13:59
  • Your above result would indicate you're receiving an NSString of zero length. – Hot Licks Feb 19 '14 at 17:01

4 Answers4

2

Might contain a escape character. & check for the NSArray - NSLog(@"Res %@",arrayDetail);

Try this Out :

if ([arrayDetail objectAtIndex:0]  == (id)[NSNull null] || [[arrayDetail objectAtIndex:0]length] < 1)
{
   NSLog(@"null");
}
else
{
 //do other stuff
}
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • 1
    This does the job - thanks. I am still would like to know the root of the problem (i.e. what is it that is in the array) but this works so great. – RGriffiths Feb 19 '14 at 13:17
2

Only if your web service delivers a string representation of an array produced by the writeToURL:atomically: method the array will contain anything useful.

Since usually this does not happen from web services, you will have chunk but no content. Probably it is not even initialized but nil. And thus showing the behavior you get.

Volker
  • 4,640
  • 1
  • 23
  • 31
1

check

if ([[arrayDetail objectAtIndex:0]  isEqualToString:@""])

instead of

if ([[observationsArrayClass objectAtIndex:0]  isEqualToString:@""])

Let me know your output.Why have you used observationsArrayClass when your output gets stored in arrayDetail?

z22
  • 10,013
  • 17
  • 70
  • 126
1

From the documentation for initWithContentsOfURL::

[It] Returns [a] nil if the location can’t be opened or if the contents of the location can’t be parsed into an array.

Pranav
  • 680
  • 6
  • 11