尝试将字符串拆分为数组,但它给出错误" [__ NSArrayI componentsSeparatedByString:]:无法识别的选择器发送到实例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 {
}
}
尝试分裂时会被杀死。为什么如此,我没有得到。如果有人遇到这种情况,请指导她的错误。
答案 0 :(得分:1)
您可以将字符串拆分为NSArray,如下所示......
NSString *yourString = @"comment,comment,comment";
NSArray *strArray = [yourString componentsSeparatedByString:@","];
NSLog(@"\n\n Array is ==>> %@",strArray);
答案 1 :(得分:0)
“[__ NSArrayI componentsSeparatedByString:]:
您的错误说明您尝试将上述方法发送到NSArray
,但没有。{/ p>
如果你想在索引0处拆分数组,你应该这样做:
NSArray *splitArray=[yourArray[0] componentsSeparatedByString:@","];
此处yourArray是您从Server获得的数组。
答案 2 :(得分:0)
NSString *strr = [[NSString stringWithFormat:@"%@", [self.mCommArr objectAtIndex:indexVal]];
NSArray *arr2 = [strr componentsSeparatedByString:@","];
我认为你现在正在传递str
,它可以是一个数组(正如你的错误指出的那样)。
让我知道结果。