【问题标题】:Passing data through modal segue: [__NSArrayI length]: unrecognized selector sent to instance通过模态segue传递数据:[__NSArrayI length]: unrecognized selector sent to instance
【发布时间】:2016-04-21 17:40:57
【问题描述】:

我正在尝试将 dogData 从我的 ViewController 传递到 PetViewController(通过模态 segue)。也就是说,由于某种原因,当我尝试传递 dogData (NSMutableArray) 时,我得到了这个错误:

[__NSArrayI 长度]: 无法识别的选择器发送到实例

知道为什么会这样吗?请参阅下面的代码(希望这是足够的信息,尝试修剪它)。

ViewController.h

@property (strong, nonatomic) NSMutableArray *dogData;

ViewController.m

 NSMutableDictionary *viewParamsDogs = [NSMutableDictionary new];
    [viewParamsDogs setValue:@"mydogs" forKey:@"view_name"];
    [DIOSView viewGet:viewParamsDogs success:^(AFHTTPRequestOperation *operation, id responseObject) {


      self.dogData = [responseObject mutableCopy];
        NSLog(@"This is the dog photo data %@", self.dogData);

        [operation responseString];


        NSDictionary *dic = [responseObject valueForKey: @"field_pet_photo_path"];
                             NSArray *arr = [dic valueForKey: @"und"];
                             NSDictionary *dic2= [arr objectAtIndex : 0];
       NSString *path = [NSString stringWithFormat:@"%@", [dic2 valueForKey: @"safe_value"]];


        NSMutableCharacterSet *characterSetToTrim = [NSMutableCharacterSet characterSetWithCharactersInString:@"()\""];
        [characterSetToTrim formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        path = [path stringByTrimmingCharactersInSet:characterSetToTrim];



  if([path length]>0) {



      NSURL *url = [NSURL URLWithString:path];

      NSData *data = [NSData dataWithContentsOfURL:url];
      UIImage *image = [UIImage imageWithData:data];
      self.dogimageView.image = image;


        } else {

            NSString *ImageURL = @"http://url.ca/paw.png";
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
            self.dogimageView.image = [UIImage imageWithData:imageData];
      }


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];



        - (IBAction)openPetProfile:(id)sender {

            PetViewController *petProfile = [[PetViewController alloc] init];
            petProfile.petSubDetail = self.dogData;
            [self presentViewController:petProfile animated:YES completion:nil];

        }

PetViewController.h

@property (nonatomic, copy) NSMutableArray *petSubDetail;
@property (weak, nonatomic) IBOutlet UILabel *petName;

PetViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

      self.petName.text = [self.petSubDetail valueForKey:@"petname"];


}

数据输出:

  "field_petname" =         {
        und =             (
                            {
                format = "<null>";
                "safe_value" = Pebbles;
                value = Pebbles;
            }
        );
    };

【问题讨论】:

  • 你为什么在数组上调用`valueForKey:`?这会给你另一个数组。
  • @rmaddy 我应该使用 objectForKey 吗(我以为我将它用于字典)?对不起,新问题。应该是什么?
  • 你需要明确你想要从数组中得到什么。您需要澄清数组中的内容。你说得对,objectForKey 用于字典。您似乎想要从数组中取出一些标题。但是数组中的哪个位置是您想要的标题?
  • 你的 dogData 数组包含什么。如果它包含字典,请尝试 NSDictionary *temp = [self.petSubDetail objectAtIndex:0];然后 self.petName.text = [temp valueForKey:@"title"];
  • @rmaddy 查看上面的更新代码 - 添加了输出结构(见标题)。

标签: ios objective-c unrecognized-selector


【解决方案1】:

你的代码应该是这样的,

 NSdictionary *dict =  [self.petSubDetail objectForKey:@"field_petname"];
 NSArray *arr = [dict objectForKey:@"und"];
 NSdictionary *dict1 = [arr objectAtIndex : 0];
 self.petName.text = [dict1  valueForKey:@"value"];

您需要" 的关键用途\" 希望这会有所帮助:)

【讨论】:

  • 这一行不起作用:NSDictionary *dict = [self.petSubDetail objectForKey:@"field_petname"];错误是:NSMutableArray 没有可见的@interface 声明选择器 objectForKey
猜你喜欢
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多