目标 C 错误 -[__NSArrayI 长度]:无法识别

Posted

技术标签:

【中文标题】目标 C 错误 -[__NSArrayI 长度]:无法识别【英文标题】:Objective C Error -[__NSArrayI length]: unrecognized 【发布时间】:2014-07-28 07:19:47 【问题描述】:

我正在尝试加载一个导航视图控制器,然后该控制器会导致一个表格视图。表视图控制器使用一个标签、一个文本视图和另一个标签从 Parse Cloud 收集信息。所有这些代码都在我制作的源代码中运行,但我现在无法弄清楚。这是我收到的错误消息:

2014-07-28 00:15:13.922 Screamery[7882:1236765] WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <AllDataViewController: 0x14f6088e0>.  Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior.  This method will no longer be called in a future release.
    2014-07-28 00:15:15.323 Screamery[7882:1236765] -[__NSArrayI length]: unrecognized selector sent to instance 0x178225b40
    2014-07-28 00:15:15.323 Screamery[7882:1236765] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x178225b40'
    *** First throw call stack:
    (0x181eccbe4 0x19190c1ac 0x181ed3bf4 0x181ed09a8 0x181dd659c 0x182d046b8 0x186547f90 0x1000ba578 0x100103ed4 0x186792400 0x1867871fc 0x186570560 0x186570418 0x100102ea0 0x1000ba914 0x100103504 0x100123be0 0x100371a3c 0x1003719fc 0x1003758c8 0x181e84688 0x181e82730 0x181db0bd4 0x18acf360c 0x186502a0c 0x1000bda28 0x191f82a08)
    libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的 AllDataViewController:

- (id)initWithCoder:(NSCoder *)aCoder

    self = [super initWithCoder:aCoder];
    if (self) 
        // Custom the table

        // The className to query on
        self.parseClassName = @"Reviews";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"name";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;

        // The number of objects to show per page
        //self.objectsPerPage = 10;
    
    return self;



- (void)viewDidLoad

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshTable:)
                                                 name:@"refreshTable"
                                               object:nil];


- (void)refreshTable:(NSNotification *) notification

    // Reload the data
    [self loadObjects];



- (void)viewDidUnload

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshTable" object:nil];


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);


- (PFQuery *)queryForTable

    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    /*    if ([self.objects count] == 0) 
     query.cachePolicy = kPFCachePolicyCacheThenNetwork;
     */

    //    [query orderByAscending:@"name"];

    return query;


-(UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath: (NSIndexPath *)indexPath 

    return UITableViewCellAccessoryNone;




// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object

    static NSString *simpleTableIdentifier = @"DataCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

        cell.accessoryType = UITableViewCellAccessoryNone;

    



    // Configure the cell


    UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
    nameLabel.text = [object objectForKey:@"name"];

    UILabel *priceLabel = (UILabel*) [cell viewWithTag:102];
    priceLabel.text = [object objectForKey:@"price"];

    UITextView *informaationLabel = (UITextView *) [cell viewWithTag:103];
    informaationLabel.text = [object objectForKey:@"information"];

    return cell;


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    // Remove the row from data model
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) 
        [self refreshTable:nil];
    ];


- (void) objectsDidLoad:(NSError *)error

    [super objectsDidLoad:error];

    NSLog(@"error: %@", [error localizedDescription]);


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if ([segue.identifier isEqualToString:@"showDataDetail"])  //showRecipeDetail
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        DataDetailViewController *destViewController = segue.destinationViewController;

        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        AllData *data = [[AllData alloc] init];
        data.name = [object objectForKey:@"name"];
        data.price = [object objectForKey:@"price"];
        data.information = [object objectForKey:@"information"];
        destViewController.data = data;

    

非常感谢大家!

【问题讨论】:

你明白错误信息是什么意思吗? 然后找出来;这非常常见,而且不会是您最后一次看到它。 你能告诉我们错误发生在哪里吗? 我很困惑。当应用程序加载该视图时,我将其作为初始视图。数据未加载它开始加载时冻结 当你在 Xcode 中运行它时,堆栈跟踪将帮助你找出它发生在哪里。我的猜测是,当您认为它是NSString 时,您使用[object objectForKey:] 获得的对象之一是NSArray。这是因为您没有使用正确的自定义模型类并遵循 MVC,而是将数据保存在 NSDictionary 中,而该 NSDictionary 无法在业务级别验证其保存的数据。 【参考方案1】:

length()NSString 类的实例方法。所以崩溃应该发生在下面的某处

nameLabel.text = [object objectForKey:@"name"];

priceLabel.text = [object objectForKey:@"price"];

informaationLabel.text = [object objectForKey:@"information"];

检查这些对象中是否有任何对象持有数组引用。

为了更清晰

 NSMutableString ingredientText = [NSMutableString string]; 
for (NSString ingredient in _data.information) 
 
[ingredientText appendFormat:@"%@\n", ingredient]; 
 
self.informationTextView.text = ingredientText;

【讨论】:

@Nicholas 所以你试图将你的数组用作字符串,这是完全错误的。 我在哪里尝试将其用作字符串? informationLabel.text = [object objectForKey:@"information"]; ? @Nicholas informationLabel.textNSString 文本是一个字符串属性。当您将对象分配给它时。 UIkit 框架将在内部检查长度属性。这就是它发生的原因。 我在 8 小时内无法回答我自己的问题,但我也更改了它 NSMutableString ingredientText = [NSMutableString string]; for (_data.information 中的 NSString 成分) [ingredientText appendFormat:@"%@\n", 成分]; self.informationTextView.text = 成分文本;【参考方案2】:

当您实际需要 NSString 实例时,您似乎正在尝试使用 NSArray 实例。 UIKit 尝试获取该“字符串”的长度(使用 -length 消息)但未能这样做,因为它不是字符串,而是数组。

【讨论】:

对这个常见异常的毫无意义的解释。 @***foe 谢谢,队长。我应该修复海报的错误吗?

以上是关于目标 C 错误 -[__NSArrayI 长度]:无法识别的主要内容,如果未能解决你的问题,请参考以下文章

C#字符串长度错误

目标数组的长度不足以复制集合中的所有项目。检查数组索引和长度

关于blob转到目标库报ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值错误解决方案

链接器错误 - 目标 C

执行 segue 抛出错误

目标 C:UILongPressGestureRecognizer 错误