0

试图将字符串拆分为数组,但它给出错误“[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance 0x11741b20'”。字符串包含值,该值来自数组的第一个索引,然后需要将字符串拆分并存储在数组中。

这是数组值。

mcommarr:(
":comment",
":comment",
":comment"

NSString *strr = [[NSString alloc]init];
strr = [self.mCommArr objectAtIndex:indexVal];
NSArray *arr2 = [str componentsSeparatedByString:@","];

这是我使用它的完整方法。

-(void)loadData:(int)indexVal;
{    
indexVal=serialIndexVal;
serialIndexVal++;

NSLog(@"arrLike:%d", [self.mArrLike count]);
NSLog(@"arrPid:%d", [self.mArrPid count]);

status = [NSString stringWithFormat:@"get"];
[self.mButtonsStatusDict setObject:status forKey:@"status"];

[self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"];


[self.activityIndicator startAnimating];

@try
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{

    NSString *status = [NSString stringWithFormat:@"get"];
    [self.mButtonsStatusDict setObject:status forKey:@"status"];
    [self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"];

    self.mButtonsCommentsDict = [MyEventApi showComments:self.mButtonsPidDict];

    self.mButtonsDict = [MyEventApi likeDislike:self.mButtonsUidDict post:self.mButtonsPidDict postStatus:self.mButtonsStatusDict];

    dispatch_sync(dispatch_get_main_queue(), ^{

        [self.activityIndicator stopAnimating];
         NSLog(@"buttons data dict:%@", self.mButtonsDict);

        if([self.mButtonsDict count] == 0)
        {
            NSLog(@"server problem no response");
            [self.mArrLike addObject: @"0"];
            [self.mArrDislike addObject: @"0"];
        }else{
        [self.mArrLike addObject: [self.mButtonsDict valueForKey:@"like"]];
        [self.mArrDislike addObject: [self.mButtonsDict valueForKey:@"dislike"]];
        }

        if([self.mButtonsCommentsDict count] == 0)
        {
            NSLog(@"server problem no response");
             [self.mCommArrTot addObject: @"0"];
        }
        else{
            self.dictComm = [self.mButtonsCommentsDict valueForKey:@"comments"];
            [self.mCommArr addObject:[self.dictComm valueForKey:@"comment"]];
            NSLog(@"count:%d",[self.mCommArr count]);
          //  NSString *strTot = [NSString stringWithFormat:@"%d",tot];
           // [self.mCommArrTot addObject:strTot];
            NSLog(@"dictcomm:%@", self.dictComm );
            NSLog(@"mcommarr:%@", [self.mCommArr objectAtIndex:indexVal]);
            strr = [[NSString alloc]init];
            strr = [self.mCommArr objectAtIndex:indexVal];
            //NSString *strr = [[NSString stringWithFormat:@"%@", [self.mCommArr objectAtIndex:indexVal]];
           // NSArray *arr1 = [self string:strr];

           // NSArray *splitArray=[self.mCommArr[0] componentsSeparatedByString:@","];

             //[strr componentsSeparatedByString:@","];
           // NSLog(@"arrSep:%@", arr1);
            //int count = [arr1 count];
            //NSLog(@"arrcount:%d", count);
           // NSString *strTot = [NSString stringWithFormat:@"%d",count];
            // [self.mCommArrTot addObject:strTot];
            //NSLog(@"mcommarrtot:%@", [self.mCommArrTot objectAtIndex:indexVal]);
        }
       // NSLog(@"arrLike:%@", [self.mArrLike objectAtIndex:indexVal]);
        // NSLog(@"arrDisLike:%@", [self.mArrLike objectAtIndex:indexVal]);

        [self.mPublicFriendTable reloadData];

    });
});
}
@catch (NSException *exception) {
    NSLog(@"main: Caught %@: %@", [exception name], [exception reason]); 
}
@finally {

}
}

它在尝试分裂时被杀死。为什么这样,我没有得到。如果有人遇到过这种情况,请指导她出了什么问题。

4

3 回答 3

1

您可以将字符串拆分为 NSArray,如下所示...

NSString *yourString = @"comment,comment,comment";
NSArray *strArray = [yourString componentsSeparatedByString:@","];
NSLog(@"\n\n Array is ==>> %@",strArray);
于 2013-04-15T12:44:27.070 回答
0
NSString *strr = [[NSString stringWithFormat:@"%@", [self.mCommArr objectAtIndex:indexVal]];
NSArray *arr2 = [strr componentsSeparatedByString:@","];

我认为您现在正在传递str,它可以是一个数组(正如您的错误指出的那样)。让我知道结果。

于 2013-04-15T13:02:47.487 回答
0

"[__NSArrayI componentsSeparatedByString:]:

您的错误表明您尝试将上述方法发送到NSArray,但没有。

由于您想在索引 0 处拆分数组。您可能应该这样做:

NSArray *splitArray=[yourArray[0] componentsSeparatedByString:@","];

这里 yourArray 是您从 Server 获得的数组。

于 2013-04-15T12:49:06.983 回答