[英]How to add NSMutableArray as objectAtIndex for NSMutableArray
如何將NSMutableArray
添加為另一個NSMutableArray
的對象(i)
我的代碼是:
yearImages = [[NSMutableArray alloc]init];
tempImages = [[NSMutableArray alloc]init];
for(int i =0; i< [yearImagesName count]; i++)
{
for(int j=0; j<[totalImagesName count]; j++)
{
if ([[totalImagesName objectAtIndex:j] rangeOfString:[yearImagesName objectAtIndex:i]].location != NSNotFound)
{
[tempImages addObject:[totalImagesName objectAtIndex:j]];
}
}
[yearImages addObject:tempImages];
[tempImages removeAllObjects];
}
NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]); // getting null result
在這里,我需要為yearImages添加tempImages作為(i)的對象。
我需要如下結果:
[yearImages objectAtIndex:i];// result need as arrayobjects here
會編譯嗎? k
在[yearImages insertObject:tempImages atIndex:k]
完全不聲明。
你遇到了什么錯誤?
為了簡化您的代碼,您可以使用此代碼擺脫索引。
yearImages = [[NSMutableArray alloc]init];
tempImages = [[NSMutableArray alloc]init];
for(NSString *yearImageName in yearImagesName)
{
for(NSString *totalImageName in totalImagesName)
{
if ([totalImageName rangeOfString:yearImageName].location != NSNotFound)
{
[tempImages addObject:totalImageName];
}
}
[yearImages addObject:tempImages];
[tempImages removeAllObjects];
}
添加對象后,您將它們從tempImages中刪除,因此結果將是一個空數組。 您應該改為添加tempImages的副本: [yearImages addObject:tempImages.copy]
(如果需要,還可以添加mutableCopy)
非常簡單。
將[yearImages objectAtIndex:i]
替換為[yearImages addObject:tempImages.copy]
現在查看完整代碼:
for(int i =0; i< [yearImagesName count]; i++)
{
for(int j=0; j<[totalImagesName count]; j++)
{
if (// Your Conditon)
{
[tempImages addObject:[totalImagesName objectAtIndex:j]];
}
}
[yearImages addObject:tempImages.copy]; // each array stored as an object
[tempImages removeAllObjects];
}
NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.