2014-01-21 1 views
0

NSArray를 요소 또는 개체가 포함 된 Expected MutableArray에 저장하려고했습니다.NSCFArray 길이 오류 iOS

성공적으로 배열을 mutablemarray에 추가하고 배열을 기록하고 그에 대한 배열 수를 기록했습니다. 내 UITableView에 5 개의 저장된 값을 추가 할 때까지 모든 것이 괜찮은 것 같습니다.

내 앱이 이상한 예외로 중단됩니다. 나는 모든 것을 시도했지만 아무것도 작동하지 않는 것 같습니다.

어디에서 디버깅을 계속할 것인지 잘 모릅니다.

앱의 시작 부분에 저장된 환경 설정입니다.

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 
) 
+0

를 사용하여 객체의 종류를 확인해야합니까? [SpecialsDetailViewController downloadSpecialImage] –

+0

'specialsimageUrlArray'의 종류는 무엇입니까? –

+0

나는 그 메시지 (원래'length')가 원래 문자열로 보내지는 것으로 의심된다. ​​(어떤 종류의 URL을 파서 링하는 것처럼 보이지만) 어떤 이유로 (메모리 손상?) 배열로 보내졌다. –

답변

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

specials 배열에 4 개의 문자열과 1 개의 배열이 포함되어 있음을 보여줍니다. 따라서 각 항목을 문자열로 사용하려고하면 문제가 발생합니다.

원인은 다음과 같습니다 당신이 그 내용을 추가 할 수 addObjectsFromArray:를 사용해야하므로 배열 인

[specialsimageUrlArray addObject: test]; 

test 때문입니다.

0
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]]을 수행하는 중에 행을 검색하는 중입니다. 즉, 배열 객체를 NSURL에서 NSString으로 변환하는 중 문제가 발생합니다. 하지만 ARRAY입니다. 그게 이유 야.

당신은 당신이이 방법을 추가 할 수 있습니다 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 
}