【问题标题】:NSCFArray length Error iOSNSCFArray 长度错误 iOS
【发布时间】:2014-01-21 14:00:19
【问题描述】:

我试图将 NSArray 保存到包含元素或对象的现有 MutableArray。

我成功地将数组添加到 mutablemarray 并记录了数组并记录了它的数组计数。一切似乎都很好,直到我想将 5 个保存的值添加到我的 UITableView

我的应用程序崩溃并出现一个奇怪的异常。我已经尝试了所有方法,但似乎没有任何效果。

我不知道从哪里继续调试。

在应用开始时保存首选项。

for (NSMutableDictionary *defineXMLData in getSpecialsAuth) {

NSArray * imageUrl = [defineXMLData objectForKey:@"imageUrl"];
[specialsimageUrlArray addObject: imageUrl];
[specialsimageUrlArray addObject: test];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:specialsimageUrlArray forKey:@"specialsImageUrlArray"];

}

SpecialsViewController.m

-(void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.specialsArray = [prefs arrayForKey:@"specialsImageUrlArray"];
NSMutableArray *specialsimageUrlArray =[[NSMutableArray alloc]init];
test = [NSArray arrayWithObjects:@"http://dev.wigroup.co/bells/cvs_images/bells3.jpg", nil];           
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSData *imageData= [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self.specialsArray objectAtIndex:indexPath.row]]];    
UIImageView *imageViewForCell = [[UIImageView alloc]initWithFrame:CGRectMake(3,3,30,30)];
imageViewForCell.image = [UIImage imageWithData:imageData];
[imv addSubview:imageViewForCell];

 }

NSLog

 SpecialsArray (
"http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
"http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
"http://dev.wigroup.co/bells/cvs_images/bells3.jpg",
"http://dev.wigroup.co/bells/cvs_images/bells2.jpg",
    (
    "http://dev.wigroup.co/bells/cvs_images/bells3.jpg"
)
)

Specials Count (
 5
 )

错误日志

  -[__NSCFArray length]: unrecognized selector sent to instance 0x15dde4f0
CRASH: -[__NSCFArray length]: unrecognized selector sent to instance 0x15dde4f0
 Stack Trace: (
0   CoreFoundation                      0x2f3bbe9b <redacted> + 154
1   libobjc.A.dylib                     0x397186c7 objc_exception_throw + 38
2   CoreFoundation                      0x2f3bf7b7 <redacted> + 202
3   CoreFoundation                      0x2f3be0af <redacted> + 706
4   CoreFoundation                      0x2f30cdc8 _CF_forwarding_prep_0 + 24
5   CoreFoundation                      0x2f311983 _CFURLInitWithURLString + 38
6   Foundation                          0x2fcf49a1 <redacted> + 212
7   Foundation                          0x2fcf48b3 <redacted> + 50
8   Bells                               0x0009b157 -[SpecialsDetailViewController downloadSpecialImage] + 110
9   Foundation                          0x2fd9633f __NSFireDelayedPerform + 414
10  CoreFoundation                      0x2f386e7f <redacted> + 14
11  CoreFoundation                      0x2f386a9b <redacted> + 794
12  CoreFoundation                      0x2f384e23 <redacted> + 1218
13  CoreFoundation                      0x2f2ef471 CFRunLoopRunSpecific + 524
14  CoreFoundation                      0x2f2ef253 CFRunLoopRunInMode + 106
15  GraphicsServices                    0x340292eb GSEventRunModal + 138
16  UIKit                               0x31ba4845 UIApplicationMain + 1136
17  Bells                               0x00142f61 main + 116
18  libdyld.dylib                       0x39c11ab7 <redacted> + 2
)

【问题讨论】:

  • 可以加这个方法吗? [SpecialsDetailViewController 下载SpecialImage]
  • specialsimageUrlArray的类型是什么?
  • 我怀疑消息 (length) 最初打算发送到一个字符串(对我来说似乎是 URL 解析的东西),但由于某种原因(内存损坏?)它有已发送到数组。

标签: ios objective-c uiimage nsarray nsdata


【解决方案1】:

这个日志:

 SpecialsArray (
    "http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
    "http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
    "http://dev.wigroup.co/bells/cvs_images/bells3.jpg",
    "http://dev.wigroup.co/bells/cvs_images/bells2.jpg",
    (
        "http://dev.wigroup.co/bells/cvs_images/bells3.jpg"
    )
)

显示您的特价数组包含 4 个字符串和 1 个数组。因此,当您尝试将每个项目用作字符串时,您就会遇到问题。

原因是:

[specialsimageUrlArray addObject: test];

因为test 是一个数组,所以你应该使用addObjectsFromArray: 来添加它的内容。

【讨论】:

    【解决方案2】:
    SpecialsArray (
        "http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
        "http://dev.wigroup.co/bells/cvs_images/bells1.jpg",
        "http://dev.wigroup.co/bells/cvs_images/bells3.jpg",
        "http://dev.wigroup.co/bells/cvs_images/bells2.jpg",
        (
            "http://dev.wigroup.co/bells/cvs_images/bells3.jpg"
        )
    )
    

    当您尝试从前 4 个 URL 获取 NSData 时,它正在工作,但第 5 个元素是 NSArray(或 NSMutableArray)。

    所以NSData *imageData= [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self.specialsArray objectAtIndex:indexPath.row]]]; 行在执行[NSURL URLWithString:[self.specialsArray objectAtIndex:indexPath.row]] 时遇到问题,即将您的数组对象从NSString 转换为NSURL但它是一个数组。这就是原因。

    你应该使用isKindOfClass检查对象的类型

    一个小例子

    if([[self.specialsArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
    
    {
      // It's NSString
    }
    else if([[self.specialsArray objectAtIndex:indexPath.row] isKindOfClass:[NSArray class]] || [[self.specialsArray objectAtIndex:indexPath.row] isKindOfClass:[NSMutableArray class]])
    {
       // It's an array
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2020-11-11
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2015-02-10
      相关资源
      最近更新 更多