【问题标题】:-[__NSArrayM length]: unrecognized selector sent to instance 0x145ecca0-[__NSArrayM 长度]:无法识别的选择器发送到实例 0x145ecca0
【发布时间】:2017-01-26 10:21:52
【问题描述】:

我收到以下错误,无法弄清楚它在哪里。请帮我找出问题所在。 TIA

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayM 长度]:无法识别的选择器发送到实例 0x145ecca0” *** 首先抛出调用栈:

- (void)viewDidLoad
    {
        [super viewDidLoad];
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont fontWithName:@"Helvetica neue" size:20.0];
        //label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        //label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor whiteColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = @"My Details";
        [label sizeToFit];
    }

    #pragma mark -
    #pragma mark Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.

        return 30;
    }


    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.textLabel.font=[UIFont fontWithName:@"Helvetica neue" size:14.0];
            cell.detailTextLabel.font=[UIFont fontWithName:@"Helvetica neue" size:13.0];
            cell.textLabel.textColor=[UIColor colorWithRed:199.0/255.0 green:65.0/255.0 blue:69.0/255.0 alpha:1.0];
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        }

        NSArray *keys = [myDict allKeys];

        for(int i=0 ; i<[keys count]; i++ )
        {

            NSString *value=[myDict objectForKey:[keys objectAtIndex:i]];
            if ([value isEqual: [NSNull null]]||[value length]==0) {

                [myDict setValue:@"--" forKey:[keys objectAtIndex:i]];

            }

        }

        if(indexPath.row==0)
        {
            cell.textLabel.text = @"Relationship";
            cell.detailTextLabel.text=[myDict objectForKey:@"Relationship"];
        }

        if(indexPath.row==1)
        {
            cell.textLabel.text = @"Person Name";
            cell.detailTextLabel.text=[myDict objectForKey:@"PersonName"];
        }

        if(indexPath.row==2)
        {
            cell.textLabel.text = @"Shopping";
            cell.detailTextLabel.text=[myDict objectForKey:@"shopping"];
        }
        if(indexPath.row==3)
        {
            cell.textLabel.text = @"Quantity";
            cell.detailTextLabel.text=[myDict objectForKey:@"Quantity"];
        }
        if(indexPath.row==4)
        {
            cell.textLabel.text = @"Pants";
            cell.detailTextLabel.text=[myDict objectForKey:@"pants"];
        }
        if(indexPath.row==5)
        {
            cell.textLabel.text = @"Shirts";
            cell.detailTextLabel.text=[myDict objectForKey:@"shirts"];
        }
        if(indexPath.row==6)
        {

            cell.textLabel.text = @"Other";
            cell.detailTextLabel.text=[myDict objectForKey:@"other"];
        }
        if(indexPath.row==7)
        {
            cell.textLabel.text = @"Start Date";
            NSString *str=[myDict objectForKey:@"startDate"];
            NSString *str2= [str substringToIndex:10];
            cell.detailTextLabel.text=str2;
        }
        if(indexPath.row==8)
        {
            cell.textLabel.text = @"End Date";
            NSString *str=[myDict objectForKey:@"endDate"];
            NSString *str2= [str substringToIndex:10];
            cell.detailTextLabel.text=str2;
        }
        if(indexPath.row==9)
        {
           cell.textLabel.text = @"Address";
           cell.detailTextLabel.text=[myDict objectForKey:@"address"];
        }
        if(indexPath.row==10)
        {
            cell.textLabel.text = @"Notes";
            cell.detailTextLabel.text=[myDict objectForKey:@"Notes"];
        }
        if(indexPath.row==11)
        {
            cell.textLabel.text = @"Bill";
            cell.detailTextLabel.text=[myDict objectForKey:@"bill"];
        }
        if(indexPath.row==12)
        {
            cell.textLabel.text = @"Grand Total";


            cell.detailTextLabel.text=[myDict objectForKey:@"total"];

        }
        if(indexPath.row==13)
        {
            cell.textLabel.text = @"Signature";
            cell.detailTextLabel.text=[myDict objectForKey:@"sign"];
        }

        return cell;
    }

【问题讨论】:

  • 除了这个问题,考虑使用数组作为数据源和/或静态单元格。

标签: ios objective-c iphone xcode nsarray


【解决方案1】:

NSArray 不回复length,而是回复count
很可能在value 中,您期望的是NSString,但实际上有一个数组。
设置值时出现问题。

【讨论】:

    【解决方案2】:

    可能是您期待一些不同的东西并且它具有其他类型的值。可能是您期待 NSString 并且它提供任何其他类型。 您可以使用 id 数据类型,以便它可以提供帮助。这为我解决了。希望对你也有用。

    【讨论】:

      【解决方案3】:

      这个:

      [myDict objectForKey:[keys objectAtIndex:i]];
      

      返回的是NSArray,而不是NSString

      【讨论】:

        猜你喜欢
        • 2016-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-16
        • 2015-03-02
        • 2015-09-09
        • 2014-05-22
        • 1970-01-01
        相关资源
        最近更新 更多