【问题标题】:can't add objects of an array in another array不能在另一个数组中添加一个数组的对象
【发布时间】:2016-02-01 07:23:24
【问题描述】:

我试图在用户离线时显示选定的单元格复选标记,但数组没有添加另一个数组对象,请帮助我

1.appDelegate.SelectedIDArray保存选中的单元格
2.buildingObject.selectedIDString保存检查单元格索引逗号分隔

//首先我从数组中删除所有对象

[appDelegate.SelectedIDArray removeAllObjects];

//然后我在数组中添加字符串值(例如 3,2,7)

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

//现在在appDelegate.SelectedIDArray中添加tempArray数组对象

[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];

//现在在 appDelegate.SelectedIDArray 中显示添加对象的计数

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

【问题讨论】:

  • appDelegate.SelectedIDArray == nil?
  • appDelegate.SelectedIDArray == nil?
  • 是的,我正在删除所有对象
  • 哦,FFS。 您分配了阵列吗?
  • 检查我的答案好友

标签: ios objective-c iphone arrays


【解决方案1】:

说离线,你的意思是应用程序被终止,那么如果你没有将你的数组保存在NSUserDefaults 它已经为零。那么你应该初始化它。如果不是这种情况,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);

【讨论】:

    【解决方案2】:

    我采用了与您的代码类似的示例测试用例:

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

    它对我来说工作正常。确保 appDelegate.SelectedIDArray 是 NSMutableArray。或者,如果您想要新鲜的数组,只需使用

    appDelegate.SelectedIDArray = [NSMutableArray arraywitharray:tempArr];

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      希望你没有忘记分配 appDelegate.SelectedIDArray。

      【讨论】:

      • 我在 appdelegate 中有 alloc
      【解决方案4】:

      你好朋友请试试这个

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

      如果它不起作用,那么您的数组中的一个可能是空的。请检查

      【讨论】:

      • tempArray 计数正常,但 SelectedIDArray 计数仍为零
      【解决方案5】:

      使用它,如果您使用非弧线,它将修复 appDelegate.SelectedIDArray = [tempArray 保留];

      【讨论】:

        猜你喜欢
        • 2018-10-04
        • 1970-01-01
        • 2021-12-23
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多