【问题标题】:NSLog not showing upNSLog 没有出现
【发布时间】:2014-02-05 04:22:02
【问题描述】:

我正在尝试让 NSLog 打印出 JSON 数据。但它不会出现在日志中。我是 Objective-C 的新手。代码一定有什么问题?

@implementation demo2ViewController

-(void)viewDidLoad
{
   [super viewDidLoad];
   title = @"title";
   thumbnail = @"thumbnail";
   author = @"author";

   myObject = [[NSMutableArray alloc] init];

   NSData *jsonSource = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString:@""]];
// note : i have the ip address, but stackoverflow wont allow me to post it. so i deleted.


    NSArray*  jsonObjects = [NSJSONSerialization JSONObjectWithData:
                      jsonSource options:NSJSONReadingMutableContainers  error:nil];

    for (NSDictionary *dataDict in jsonObjects) {
        NSString *title_data = [dataDict objectForKey:@"title"];
        NSString *thumbnail_data = [dataDict objectForKey:@"thumbnail"];
        NSString *author_data = [dataDict objectForKey:@"author"];

        NSLog(@"TITLE: %@",title_data);
        NSLog(@"THUMBNAIL: %@",thumbnail_data);
        NSLog(@"AUTHOR: %@",author_data);

        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      title_data, title,
                      thumbnail_data, thumbnail,
                      author_data,author,
                      nil];
        [myObject addObject:dictionary];
    }
}

[ { 
title: "Post From Wordpress Title", 
content: "Some Content from Post on Wordpress", 
author: "Banyapon Poolsawasd", thumbnail: "" 
},{ 
title: "Post From Wordpress Title", 
content: "Some Content from Post on Wordpress", 
author: "Banyapon Poolsawasd", 
thumbnail: "" 
 } ]

您的帮助将不胜感激。 谢谢。

【问题讨论】:

  • 下面是json数据
  • 你没有传递任何 urlString ` NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];`
  • 在您的 nslog 语句上放置一个断点,以确保它们甚至正在被执行。
  • 检查 jsonObjects 数组是否包含对象或其 nil
  • 也许可以尝试使用 NSLog([dataDict allKeys]);查看您的 dataDict 中是否包含所有这些键,例如“标题”、“缩略图”。如果没有,可能是您的 json 对象有问题。

标签: ios objective-c json nslog


【解决方案1】:

尝试将NSLog转为jsonSource

NSData *jsonSource = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString:@""]]; //Here you are not call url string
NSlog(@"jsonSource:%@", jsonSource);

jsonSource 将返回 nil

    NSArray*  jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers  error:nil];

 NSlog(@"jsonObjects:%@", jsonObjects);

jsonObjects 也将返回 nil

所以控制器不会进入 for 循环,这就是你没有打印 slog 的原因..

【讨论】:

  • 当我这样做时 NSString *pageSource = [[NSString alloc] initWithData:jsonSource encoding:NSUTF8StringEncoding]; NSLog(@"页面源%@",pageSource);
  • 它将返回 2014-02-05 12:37:16.771 demo2[23908:15d03] page source [ { title: "Post From Wordpress Title", content: "Some Content from Post on Wordpress" , author: "Banyapon Poolsawasd", thumbnail: "youweb.com/wp-content/uploads/2013/08/image.png" },{ title: "Post From Wordpress Title", content: "Some Content from Post on Wordpress", author: "Banyapon Poolsawasd", thumbnail: "@987654322 @"
  • 它确实返回了一些东西
【解决方案2】:

您的数据:

[ { 
title: "Post From Wordpress Title", 
content: "Some Content from Post on Wordpress", 
author: "Banyapon Poolsawasd", thumbnail: "" 
},{ 
title: "Post From Wordpress Title", 
content: "Some Content from Post on Wordpress", 
author: "Banyapon Poolsawasd", 
thumbnail: "" 
 } ]

在上述情况下,您的数据从NSArray 开始,不是NSDictionary。这就是不显示任何内容的原因。
使用下面的代码获取 Title 和其他值,例如:

 NSLog(@"Title :%@",[[jsonObjects objectAtIndex:0]objectForKey:@"title"]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多