Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将NSMutableArray中的值存储到字符串?_Ios_Objective C_Arrays_Iphone - Fatal编程技术网

Ios 如何将NSMutableArray中的值存储到字符串?

Ios 如何将NSMutableArray中的值存储到字符串?,ios,objective-c,arrays,iphone,Ios,Objective C,Arrays,Iphone,我得到了一个像这样的NSMutableArray Selected Id : ( 2, 92, 154 ) 154,12 它不是静态的,这些值是在我选择多个表视图行时添加的 目前我选择了3行,其id正在该数组中打印, 如果我再选择一行,即4行,那么数组将如下所示 Slected Id : ( 2, 92, 154, 12 ) 6,87,154,65,45 如果我取消选择任何一行,假设我取消选择了两行,那么新数组将被创建 Slecte

我得到了一个像这样的NSMutableArray

Selected Id : (
    2,
    92,
    154
)
154,12
它不是静态的,这些值是在我选择多个表视图行时添加的

目前我选择了3行,其id正在该数组中打印, 如果我再选择一行,即4行,那么数组将如下所示

Slected Id : (
    2,
    92,
    154,
    12
)
6,87,154,65,45
如果我取消选择任何一行,假设我取消选择了两行,那么新数组将被创建

Slected Id : (

    154,
    12
)
所以,我想把这些值存储在任何对象中,这些对象会给我这样的输出

Selected Id : (
    2,
    92,
    154
)
154,12
i、 用逗号分隔的对象

我选择了5个值,它必须这样存储

Slected Id : (
    2,
    92,
    154,
    12
)
6,87,154,65,45
如何实施

我的项目代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

         self.selectedPath = indexPath;

        if ([tableView isEditing]) {

            //  [selectedArray addObject:[_mutableArray objectAtIndex:indexPath.row]];

            [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

            count1=(int)[selectedArray count];
            NSLog(@"Selected count is :%i",count1);
            NSLog(@"Slected Id : %@",selectedArray);


        }else{

         /// other action
    }

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    self.selectedPath = indexPath;


 //   [selectedArray removeObject:[_mutableArray objectAtIndex:indexPath.row]];
    [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

    count1=(int)[selectedArray count];
    NSLog(@"Selected count is :%i",count1);
    NSLog(@"Slected Id : %@",selectedArray);



    if (!selectedArray.count) {
        [self.tableView setEditing:NO animated:YES];
    }
}
在这里,我将获得选定的数据

NSLog(@"Slected Id : %@",selectedArray);

为此,您应该使用下面的
UITableView
的委托方法

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
  //your code...      
 }
现在,通过如下所示的一个可变数组来管理选择和取消选择行id

var selectedRowArr = [String]() // Change if your id is Int or other type.
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
     //your code...

     // First check "rowId" already added or not
     if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array 
        selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list
      }
      else {
          selectedRowArr.append(rowID)
      }
 }
现在您需要更新上面的委托方法,如下所示

var selectedRowArr = [String]() // Change if your id is Int or other type.
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
     //your code...

     // First check "rowId" already added or not
     if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array 
        selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list
      }
      else {
          selectedRowArr.append(rowID)
      }
 }
将数组转换为字符串

let myString = selectedRowArr.joined(separator: ",")
输出应该是

1,12,13

更新:

首先删除
didSelectRowAtIndexPath
方法我将编辑您的
didSelectRowAtIndexPath
方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

         self.selectedPath = indexPath;

        if ([tableView isEditing]) {

            First you need to check "c_uid" is already added in the Array or not ?
            if ([selectedArray containsObject:someObject]) {
              [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

                count1=(int)[selectedArray count];
              NSLog(@"Selected count is :%i",count1);
                 NSLog(@"Slected Id : %@",selectedArray);



                   if (!selectedArray.count) {
                     [self.tableView setEditing:NO animated:YES];
                    }
            }
            else {
               [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

               count1=(int)[selectedArray count];
               NSLog(@"Selected count is :%i",count1);
               NSLog(@"Slected Id : %@",selectedArray);
            }


        }else{

         /// other action
    }
如果要将数组转换为字符串,则

NSString * result = [selectedArray componentsJoinedByString:@","];
输出应该是

1,12,13


为此,您应该使用下面的
UITableView
的委托方法

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
  //your code...      
 }
现在,通过如下所示的一个可变数组来管理选择和取消选择行id

var selectedRowArr = [String]() // Change if your id is Int or other type.
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
     //your code...

     // First check "rowId" already added or not
     if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array 
        selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list
      }
      else {
          selectedRowArr.append(rowID)
      }
 }
现在您需要更新上面的委托方法,如下所示

var selectedRowArr = [String]() // Change if your id is Int or other type.
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
     //your code...

     // First check "rowId" already added or not
     if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array 
        selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list
      }
      else {
          selectedRowArr.append(rowID)
      }
 }
将数组转换为字符串

let myString = selectedRowArr.joined(separator: ",")
输出应该是

1,12,13

更新:

首先删除
didSelectRowAtIndexPath
方法我将编辑您的
didSelectRowAtIndexPath
方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

         self.selectedPath = indexPath;

        if ([tableView isEditing]) {

            First you need to check "c_uid" is already added in the Array or not ?
            if ([selectedArray containsObject:someObject]) {
              [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

                count1=(int)[selectedArray count];
              NSLog(@"Selected count is :%i",count1);
                 NSLog(@"Slected Id : %@",selectedArray);



                   if (!selectedArray.count) {
                     [self.tableView setEditing:NO animated:YES];
                    }
            }
            else {
               [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];

               count1=(int)[selectedArray count];
               NSLog(@"Selected count is :%i",count1);
               NSLog(@"Slected Id : %@",selectedArray);
            }


        }else{

         /// other action
    }
如果要将数组转换为字符串,则

NSString * result = [selectedArray componentsJoinedByString:@","];
输出应该是

1,12,13


希望下面的代码会对你有所帮助

NSMutableString *strProductId = [[NSMutableString alloc]init];
[strProductId setString:@""];
NSArray *arrTemp =  [selectedArray copy];
for (int i=0; i<[arrTemp count]; i++)
{
    if ([arrTemp count]-1 == i)
    {
        [strProductId appendFormat:@"%@",[arrTemp objectAtIndex:i]];
    }
    else
    {
        [strProductId appendFormat:@"%@,",[arrTemp objectAtIndex:i]];
    }
}

NSLog(@"Selected Id : %@",strProductId);
NSMutableString*strProductId=[[NSMutableString alloc]init];
[strProductId设置字符串:@”“;
NSArray*arrTemp=[selectedArray copy];

for(inti=0;i希望对您以下代码有所帮助

NSMutableString *strProductId = [[NSMutableString alloc]init];
[strProductId setString:@""];
NSArray *arrTemp =  [selectedArray copy];
for (int i=0; i<[arrTemp count]; i++)
{
    if ([arrTemp count]-1 == i)
    {
        [strProductId appendFormat:@"%@",[arrTemp objectAtIndex:i]];
    }
    else
    {
        [strProductId appendFormat:@"%@,",[arrTemp objectAtIndex:i]];
    }
}

NSLog(@"Selected Id : %@",strProductId);
NSMutableString*strProductId=[[NSMutableString alloc]init];
[strProductId设置字符串:@”“;
NSArray*arrTemp=[selectedArray copy];

对于(int i=0;iyou当前正在获取所选id的数组?如
Slected id:(154,12)
yea right…!我想要没有括号或空白的154、12…等。你能粘贴你的示例代码吗..你是如何形成你的数组的?在你的代码中,当你选择下一行时,DidDeceloseRowatindexpath会自动调用,然后DidDeceloseRowatindexpath会给出上一行的Indexath。所以你以前选择的行是自动的ally remove.所有方法都在工作,我想要的是id在1,2,3…等中,就是这样。您当前正在获取所选id的数组?如
Slected id:(154,12)
yea right…!我想要没有括号或空白的154、12…等。你能粘贴你的示例代码吗..你是如何形成你的数组的?在你的代码中,当你选择下一行时,DidDeceloseRowatindexpath会自动调用,然后DidDeceloseRowatindexpath会给出上一行的Indexath。所以你以前选择的行是自动的ally remove.所有方法都在工作,我想要的是id在1,2,3…等中。你能不能根据我在目标c中的代码转换你的答案,因为我不熟悉swift代码。NSString*结果=[selectedArray Components通过字符串连接:@],“];这个方法对我很有效。我知道这是一个非常基本的问题,但我很困惑,所以我请求帮助。无论如何,非常感谢@iPatelU plz根据我在目标c中的代码转换你的答案,因为我不熟悉swift代码。NSString*result=[selectedArray componentsJoinedByString:@],“];这种方法对我很有效。我知道这是一个非常基本的问题,但我很困惑,所以我请求帮助。无论如何,非常感谢@ipatel