2014-01-21 71 views
0

我試圖將NSArray保存到包含元素或對象的現有MutableArray。NSCFArray長度錯誤iOS

我成功地將數組添加到了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 
) 
+0

可以添加此方法檢查對象的類型? [SpecialsDetailViewController downloadSpecialImage] –

+0

'specialsimageUrlArray'的類型是什麼? –

+0

我懷疑這個消息('length')最初是要發送給一個字符串的(看起來像某種URL解析給我的東西),但由於某種原因(內存損壞?)它已經發送到一個數組。 – 2014-01-21 14:07:35

回答

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個數組。所以,當你嘗試將每個項目用作字符串時,就會出現問題。

的原因是:

[specialsimageUrlArray addObject: test]; 

因爲test是一個數組,所以你應該使用addObjectsFromArray:添加其內容。

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個網址,那麼它的工作得到的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 
}