NSCFString objectAtIndex:发送到实例Objective C的无法识别的选择器

时间:2013-05-16 07:35:27

标签: objective-c xcode json nsstring

我正在尝试获取一些json数据并将其显示为UILabel中的文本,但我继续使用以下错误获得应用程序崩溃 - [__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x1f8cfff0?

这是我的代码和json响应。我在我的日志中看到我从通话中获取了名称,但该应用程序炸毁了该错误。我有2个UILabel块,其中一个显示json响应的文本格式,另一个显示文本中的实际json响应。

我试图拉出这个人的名字,当json回来时,我可以在日志中看到Bilbo Baggins。

这是我的json输出:

{"ProfileID":34,"ProfilePictureID":20,"Name":"Bilbo Baggins","Clients":[{"ClientID":91,"Name":"Fnurky"},{"ClientID":92,"Name":"A different client"},{"ClientID":95,"Name":"Second Community"},{"ClientID":96,"Name":"Britehouse"}]}

我的代码尝试将其显示为文本的uilabel。

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString: @"http://www.ddproam.co.za/Central/Profile/JSONGetProfileForUser"] //2

#import "JsonViewController.h"

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}

-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;    
}
@end

@implementation JsonViewController

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}

 - (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                     options:kNilOptions 
                                                       error:&error];
NSArray* defineJsonData = [json objectForKey:@"Name"]; //2

NSLog(@"Name: %@", defineJsonData); //3

// 1) Get the latest loan
NSDictionary* loan = [defineJsonData objectAtIndex:0];


// 3) Set the label appropriately
humanReadble.text = [NSString stringWithFormat:@"Hello: %@",
                     [(NSDictionary*)[loan objectForKey:@"Name"] objectForKey:@"Name"]];

//build an info object and convert to json
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                      [loan objectForKey:@"Name"],
                      nil];

//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info 
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

//print out the data contents
jsonSummary.text = [[NSString alloc] initWithData:jsonData
                                         encoding:NSUTF8StringEncoding];

}

@end

1 个答案:

答案 0 :(得分:2)

组合 - 抱歉 - 名字很差,在复杂的结构中丢失。

第一: 在这里,您将获得完整的JSON作为词典:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                     options:kNilOptions 
                                                       error:&error];

根据你的Q,这有这样的结构:

{
   "ProfileID":34,
   "ProfilePictureID":20,
   "Name":"Bilbo Baggins",
   "Clients":
   [
      {
         "ClientID":91,
         "Name":"Fnurky"
      },
      {  
         "ClientID":92,
         "Name":"A different client"
      },
      {
         "ClientID":95,
         "Name":"Second Community"
      },
      {
         "ClientID":96,
         "Name":"Britehouse"
      }
   ]
}

第二: 在下一个声明中,你只需要得到一个像人一样的名字:

NSArray* defineJsonData = [json objectForKey:@"Name"]; //2

有根:

你得到了什么 - 看看你的JSON - 是:

   "Name":"Bilbo Baggins",
  • 您获得了密钥Name的对象。保持对结果的引用的var应该被称为表达它。让我们改变一下:

    NSArray * name = [json objectForKey:@“Name”]; // 2

  • 下一步 - 查看您的JSON - 该密钥背后的对象是NSString的实例,而不是NSArray。我们来修理一下:

    NSString * name = [json objectForKey:@“Name”]; // 2

第三

这样做会使编译器抛出错误。这是因为这句话:

NSDictionary* loan = [defineJsonData objectAtIndex:0];

更改为新的var名称:

NSDictionary* loan = [name objectAtIndex:0];

编译器是对的:您没有数组,因此无法发送objectAtIndex: