how to avoid NULL value in objective c

Please consider this code:

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        NSURL *url = [NSURL URLWithString:@"http://localhost/faq.php?faqType=2"]; // Modify this to 
        NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
        NSLog(@"jsonreturn=%@",jsonreturn); // Look at the console and you can see what the restults are
        NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
        NSError *error = nil;

        // In "real" code you should surround this with try and catch
        NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

        if (dict)
        {
            rows = [[dict objectForKey:@"faq"] retain];
        }
        [jsonreturn release];    
    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  {

        // Configure the cell.
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }

        NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];
        NSLog(@"%@", dict1);
        cell.textLabel.text = [dict1 objectForKey:@"faqQues"];
  }


//if it is not getting NULL value then UItableView is ok
{"faq":[{"faqQues":"this is mr.mack?"},{"faqQues":"is he good man?"}]}

//but if the data is like NULL 

    {"faq":[{"faqQues":"this is mr.mack?"},{"faqQues":null}]}  // then it is creating EXEC_BAD_ACCESS error, 

so how to avoid NULL or check null value, or how can i fix this issue?

Answers


You can query the value before using it.

if ([dict objectForKey:@"faqQues"] == [NSNull null]) {
    // value is null, use your own value here
} else {
    // good value to use
}

You can also do this while enumerating as well.

for (id value in dict) {
    if (value == [NSNull null]) {
        // null
    }

}

Need Your Help

SQL Order By query problem

mysql sql sql-order-by

I have a table with following dataset

Expand Div with jQuery

jquery html tabs

I like to do something terribly simple

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.