'-[__ NSDictionaryI length]:无法识别的选择器已发送到实例'-试图找出原因 [英] '-[__NSDictionaryI length]: unrecognized selector sent to instance' - trying to figure out why

查看:112
本文介绍了'-[__ NSDictionaryI length]:无法识别的选择器已发送到实例'-试图找出原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其标题带有自定义UILabel,而另一个则带有副标题,以便将文本向右对齐.

I've a UITableView with custom UILabel for title and one another to subtitle, in order to align the text to right.

我的数据来自Web服务,我将其解析并存储在NSDictionaryNSArray中. 某些标题或副标题可以是nil/empty-text. 为此,我将其放在try/catch中.

My data come from web service, which I parse and store in NSArray of NSDictionary. Some of the title or the subtitle can be nil/empty-text. For this I'm putting it in try/catch.

表中的一行使我的应用程序崩溃,我不知道为什么.

One of the lines in my table making my app crush and I cannot figure out why.

我认为其中一个(标题/字幕)返回为NSDictionary,但是我为我的对象([UILabel text])的类做了日志打印,看来一切正常.

I think one of them (title/subtitle) return as NSDictionary but I made a log print for class for my objects, for my [UILabel text], seems like everything is ok.

异常和堆栈跟踪:

2014-04-29 10:47:55.985 Cellular Radar[3226:60b] -[__NSDictionaryI length]: unrecognized selector sent to instance 0x16d007f0
2014-04-29 10:47:55.993 Cellular Radar[3226:60b] -[Home tableView:willDisplayCell:forRowAtIndexPath:], exception.reason: -[__NSDictionaryI length]: unrecognized selector sent to instance 0x16d007f0
2014-04-29 10:47:55.999 Cellular Radar[3226:60b] -[__NSDictionaryI length]: unrecognized selector sent to instance 0x16d007f0
2014-04-29 10:47:56.006 Cellular Radar[3226:60b] CRASH:
-[__NSDictionaryI length]: unrecognized selector sent to instance 0x16d007f0
2014-04-29 10:47:56.062 Cellular Radar[3226:60b] Stack Trace:
(
    0   CoreFoundation                      0x30e02feb <redacted> + 154
    1   libobjc.A.dylib                     0x3b5b1ccf objc_exception_throw + 38
    2   CoreFoundation                      0x30e06967 <redacted> + 202
    3   CoreFoundation                      0x30e05253 <redacted> + 706
    4   CoreFoundation                      0x30d547b8 _CF_forwarding_prep_0 + 24
    5   UIKit                               0x33637ed3 <redacted> + 38
    6   UIKit                               0x337595bb <redacted> + 3682
    7   UIKit                               0x33758031 <redacted> + 60
    8   UIKit                               0x3361ed79 <redacted> + 380
    9   QuartzCore                          0x3329c62b <redacted> + 142
    10  QuartzCore                          0x33297e3b <redacted> + 350
    11  QuartzCore                          0x33297ccd <redacted> + 16
    12  QuartzCore                          0x332976df <redacted> + 230
    13  QuartzCore                          0x332974ef <redacted> + 314
    14  QuartzCore                          0x332eac49 <redacted> + 516
    15  IOMobileFramebuffer                 0x3606375d <redacted> + 104
    16  IOKit                               0x31aed451 IODispatchCalloutFromCFMessage + 248
    17  CoreFoundation                      0x30dc2ef9 <redacted> + 136
    18  CoreFoundation                      0x30dcdab7 <redacted> + 34
    19  CoreFoundation                      0x30dcda53 <redacted> + 346
    20  CoreFoundation                      0x30dcc227 <redacted> + 1398
    21  CoreFoundation                      0x30d36f0f CFRunLoopRunSpecific + 522
    22  CoreFoundation                      0x30d36cf3 CFRunLoopRunInMode + 106
    23  GraphicsServices                    0x35c3b663 GSEventRunModal + 138
    24  UIKit                               0x3368216d UIApplicationMain + 1136
    25  Cellular Radar                      0x00077757 main + 70
    26  libdyld.dylib                       0x3babeab7 <redacted> + 2
)
2014-04-29 10:47:56.075 Cellular Radar[3226:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0x16d007f0'
*** First throw call stack:
(0x30e02fd3 0x3b5b1ccf 0x30e06967 0x30e05253 0x30d547b8 0x33637ed3 0x337595bb 0x33758031 0x3361ed79 0x3329c62b 0x33297e3b 0x33297ccd 0x332976df 0x332974ef 0x332eac49 0x3606375d 0x31aed451 0x30dc2ef9 0x30dcdab7 0x30dcda53 0x30dcc227 0x30d36f0f 0x30d36cf3 0x35c3b663 0x3368216d 0x77757 0x3babeab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

相关代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor = [UIColor lightGrayColor];

    UILabel *textLabel = (UILabel*)[cell viewWithTag:kTAG_TITLE];
    UILabel *detailTextLabel = (UILabel*)[cell viewWithTag:kTAG_SUBTITLE];
    UIImageView *imageView = (UIImageView*)[cell viewWithTag:kTAG_IMAGE];

    textLabel.frame = CGRectMake(
                                 textLabel.frame.origin.x,
                                 textLabel.frame.origin.y - 10,
                                 textLabel.frame.size.width,
                                 textLabel.frame.size.height
                                 );
    cell.textLabel.frame = textLabel.frame;

    cell.tag = indexPath.row;
    cell.backgroundColor = [UIColor whiteColor];

    cell.textLabel.font = cell.detailTextLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];

    imageView.layer.cornerRadius = 10.0;
    imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
    imageView.layer.borderWidth = 0;
    imageView.clipsToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFill;

    textLabel.text = @"";

    textLabel.textColor = [UIColor darkGrayColor];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentRight;
    textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
    textLabel.textColor = [UIColor darkGrayColor];

    detailTextLabel.text = @"";
    detailTextLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
    detailTextLabel.backgroundColor = [UIColor clearColor];
    detailTextLabel.textAlignment = NSTextAlignmentRight;
    detailTextLabel.textColor = [UIColor darkGrayColor];

    if (self.arrayGlobal.count > 0)
    {
        NSDictionary *dict = self.arrayGlobal[indexPath.row];
        NSString *imageUrl = dict[@"imageUrl"];

        @try
        {
            cell.textLabel.text = dict[@"Title"];
        }
        @catch (NSException *exception)
        {
            NSLog(@"%s, %@", __PRETTY_FUNCTION__, exception.reason);

            cell.textLabel.text = @"";
        }

        @try
        {
            detailTextLabel.text = dict[@"Text"];
        }
        @catch (NSException *exception)
        {
            detailTextLabel.text = @"";
        }

        if (imageUrl.length > 0)
        {
            NSString *completeURL = @"";
            completeURL = [completeURL stringByAppendingString:kPROFILE_IMAGE_URL];
            completeURL = [completeURL stringByAppendingString:@"/2/"];
            completeURL = [completeURL stringByAppendingString:imageUrl];
            completeURL = [completeURL stringByAppendingString:@".png"];

            NSString *fileName = [NSString stringWithFormat:@"%@.png", imageUrl];
            NSString* path = GetMediaFolder();
            BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:fileName]];

            if (imageExists)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];

                    UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE];
                    imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
                    imageView.layer.cornerRadius = 23.0;
                    imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
                    imageView.layer.borderWidth = 1.0;
                    imageView.clipsToBounds = YES;
                    imageView.contentMode = UIViewContentModeScaleAspectFill;

                    [updateCell setNeedsLayout];
                });
            }
            else
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSURL *imageURL=[NSURL URLWithString:completeURL];
                    NSData *image=[NSData dataWithContentsOfURL:imageURL];
                    [image writeToFile:[path stringByAppendingPathComponent:fileName] atomically:YES];

                    // Update UI
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];

                        UIImageView *imageView = (UIImageView*)[updateCell viewWithTag:kTAG_IMAGE];
                        imageView.image=[UIImage imageWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
                        imageView.layer.cornerRadius = 23.0;
                        imageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
                        imageView.layer.borderWidth = 1.0;
                        imageView.clipsToBounds = YES;
                        imageView.contentMode = UIViewContentModeScaleAspectFill;

                        [updateCell setNeedsLayout];
                    });
                });
            }
        }
    }
    return cell;
}

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"4");

    @try {
        cell = [Netroads willDisplayFlatDesignCell:cell];
    }
    @catch (NSException *exception) {
        NSLog(@"%s, exception.reason: %@", __PRETTY_FUNCTION__, exception.reason);
    }
}

+ (UITableViewCell*)willDisplayFlatDesignCell:(UITableViewCell*)cell
{
    UILabel *label = [[UILabel alloc] initWithFrame:cell.frame];
    label.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1]; // light gray color, native iphone
    cell.selectedBackgroundView = label;

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    cell.textLabel.textColor = [UIColor darkGrayColor];

    if (cell.detailTextLabel.text) {
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
    }

    return cell;
}

编辑-1:

我为NSDictionary记录了一个日志,正如@sleepwalkerfx所指出的那样,我那行的暗恋结果是:

I made a log for my NSDictionary as @sleepwalkerfx pointed out and the result for my row that crush is:

2014-04-29 12:01:15.182 Cellular Radar[3331:60b] {
    Date = "22/01/14";
    Owner = "\U05de\U05e2\U05e8\U05db\U05ea";
    Text =     {
    };
    Time = "09:27";
    Title =     {
    };
    WallID = 1;
    imageUrl = system;
}

这让我认为那些对象(文本,标题)如何像[NSString class]一样通过检查?

Which makes me think that how can those objects (text, title) pass the check as [NSString class]?

推荐答案

您正在将单元格的text属性设置为字典:

You are setting the cell's text property to a dictionary:

cell.textLabel.text = dict[@"Title"];

从您的dict日志中:

Title =     {
    };

和:

detailTextLabel.text = dict[@"Text"];

从您的dict日志中:

Text =     {
    };

目前尚不清楚dict应该使用什么值,但不应该是那些字典.

It's not clear exactly what values should be used from dict, but it should not be those dictionaries.

这篇关于'-[__ NSDictionaryI length]:无法识别的选择器已发送到实例'-试图找出原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆