简体   繁体   中英

can't add objects of an array in another array

i am trying to show selected cell checkmark when user is offline,but the array is not adding another array object, kindly help me

1. appDelegate.SelectedIDArray saving selected cell
2. buildingObject.selectedIDString saving checked cell index coma separated

//first i am removing all objects from array

[appDelegate.SelectedIDArray removeAllObjects];

//then i am adding string values in array(eg 3,2,7)

 NSMutableArray *tempArray = (NSMutableArray*)[buildingObject.selectedIDString componentsSeparatedByString:@","];

//now adding tempArray array objects in appDelegate.SelectedIDArray

[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];

//now showing count of added object in appDelegate.SelectedIDArray

txtID.text=[NSString stringWithFormat:@"%zd of 23 selected",appDelegate.SelectedIDArray.count];

by saying offline, you mean app is terminated, then if you didn't save your array in NSUserDefaults it is nil already. then you should initialize it. if this is not the case another problem may be with appDelegate:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *vals = @"1,2,3,4,5,6,7,8";
    NSMutableArray *tempArray = [[vals componentsSeparatedByString:@","] mutableCopy];
    if (!appDelegate.SelectedIDArray) {
        appDelegate.SelectedIDArray = [NSMutableArray new];
    }
    else
    { 
        [appDelegate.SelectedIDArray removeAllObjects];
    }
    [appDelegate.SelectedIDArray addObjectsFromArray:tempArray];
    NSLog(@"%@", appDelegate.SelectedIDArray);

I've took sample test case similar to your code:

NSString *str1 = @"1,2,3,4,5,6,7,8,9";
NSMutableArray *tempArray = (NSMutableArray*)[str1 componentsSeparatedByString:@","];
NSMutableArray *arr = [NSMutableArray array];
[arr addObjectsFromArray:tempArray];
NSLog(@"%@",arr);

Its working fine for me. Make sure appDelegate.SelectedIDArray is NSMutableArray. Or if you want fresh array simply use

appDelegate.SelectedIDArray = [NSMutableArray arraywitharray:tempArr];

Hope this helps.

希望您不要忘记分配appDelegate.SelectedIDArray。

Hello buddy please try this

NSArray *tempArray = [buildingObject.selectedIDString componentsSeparatedByString:@","];

[appDelegate setSelectedIDArray:[tempArray mutableCopy]];

if it didnot work then possibly one of your array is empty.Please check

使用它,它将解决您是否使用非弧形appDelegate.SelectedIDArray = [tempArray保留];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM