【问题标题】:NSCFString objectAtIndex: unrecognized selector sent to instance Objective C [closed]NSCFString objectAtIndex:无法识别的选择器发送到实例Objective C [关闭]
【发布时间】:2013-05-16 07:35:26
【问题描述】:

我正在尝试获取一些 json 数据并将其显示为 UILabel 中的文本,但我不断收到应用程序崩溃并出现以下错误 -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 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

【问题讨论】:

  • defineJsonData = [json objectForKey:@"Name"]字符串 “Bilbo Baggins”,那么您为什么希望 [defineJsonData objectAtIndex:0] 起作用呢?
  • 不确定。我只是想在我的 uilabel 中显示 json 响应名称?
  • 您的 JSON 包含 "Name":"Bilbo Baggins",因此 [json objectForKey:@"Name"] 返回字符串“Bilbo Baggins”。

标签: objective-c xcode json nsstring


【解决方案1】:

结合了——抱歉——糟糕的 var 名称和迷失在复杂的结构中。

首先: 在这里你可以得到完整的 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 应该被称为表达 this。让我们改变一下:

    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:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多