Search code examples
iosobjective-cuiimagensarraynsdata

NSCFArray length Error iOS


I tried to save am NSArray to an Existing MutableArray that contained elements or objects.

I successfully added the array to the mutablemarray and logged the array and also logged the array count for it. Everything seemed to be okay until I wanted to add the 5 saved values to my UITableView.

My App crashes with a weird exception. I have tried absolutely everything but nothing seems to work.

I have no idea of where to continue debugging.

Saved Prefs in beginning of app.

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
 )

Error Log

  -[__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
)

Solution

  • This log:

     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"
        )
    )
    

    Shows that your specials array contains 4 string and 1 array. So when you try to use each item as a string you have a problem.

    The cause is:

    [specialsimageUrlArray addObject: test];
    

    Because test is an array so you should be using addObjectsFromArray: to add its contents.