NSDictionary dictionaryWithObject问题

时间:2014-10-13 10:33:11

标签: ios nsarray nsdictionary nsurlrequest

我试图从服务器获取Title数据,但遗憾的是我没有得到任何内容,因为它显示在下面的NSLog中。难道我不能得到Title字典吗?请问我的问题在哪里?

虽然我想在UITableVeiw

中设置该数据
- (void)getNews{

NSURL *url = [NSURL URLWithString:@"http://www.example.ashx"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response,
                                           NSData *data, NSError *connectionError)
 {
     if (data.length > 0 && connectionError == nil)
     {
         NSDictionary *getData = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:0
                                                                    error:NULL];

         if([[[getData objectForKey:@"Success"] stringValue] isEqualToString:@"1"]){

             [dataNewsArray addObjectsFromArray:[[greeting objectForKey:@"Response"] objectForKey:@"Datasource"]];
         }

         NSDictionary *aDict = [NSDictionary dictionaryWithObject:dataHaberlerArray forKey:@"Title"];

         NSLog(@"Check %@", aDict);
       }
   }];
}

NSLog结果就像这样;

Check {
Title =     (
            {
        Content = "";
        Date = "13.10.2014";
        Time = "01:17:34";
        Title = "example";
    },

NSLog for dataNewsArray

2014-10-13 13:40:14.828 new_8[8742:346345] Check (
    {
    Content = " ";
    Date = "13.10.2014";
    Time = "01:38:53";
    Title = "*test*";
},

1 个答案:

答案 0 :(得分:0)

编写以下代码以获得一个标题

[[[aDict objectForKey:@"Title"] objectAtIndex:0] objectForKey:@"Title"]];
所有标题的

使用以下代码

  NSMutableArray *titleArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[[aDict objectForKey:@"Title"] count]; i++) {

        [titleArray addObject:[[[aDict objectForKey:@"Title"] objectAtIndex:i] objectForKey:@"Title"]];
    }

上述解决方案提供了答案,但还有一些其他方法使用谓词来获取数据