-(void)ClkRandomBtn:(id)sender
{
NSMutableURLRequest *req = [[ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://chinesepokerwithfriends.com/chinesepoker/rand_user.php?"]]; //POST set up.
NSMutableString *postBody = [NSMutableString stringWithFormat:@""];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPMethod:@"POST"];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
NSString *returnString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
returnString= [returnString stringByReplacingOccurrencesOfString:@"\n"withString:@"" ];
NSLog(@"returnString=%@",returnString);
randomplayer=[[NSMutableArray alloc]init];
[randomplayer addObject:[returnString componentsSeparatedByString:@"/"]];
NSMutableArray *Pinfo=[[NSMutableArray alloc]init];
for (int i=0; i<[randomplayer count]; i++)
{
NSLog(@"%@",[randomplayer objectAtIndex:i]);
NSString *strplayerinfo=[[NSString alloc]init];
strplayerinfo=[randomplayer objectAtIndex:i];
[Pinfo addObject:[strplayerinfo componentsSeparatedByString:@" "]];
}
for (int i=0; i<[Pinfo count]; i++)
{
NSLog(@" second array%@",[Pinfo objectAtIndex:i]);
}
}
这是我在dheeru 9411279057 dheeru@gmail.com/abc 9876543215 abc@gmail.com/
中获取字符串的代码我希望将每个字符串拆分为单个字符串,但此代码无效。如何使它工作?
答案 0 :(得分:2)
替换
[randomplayer addObject:[returnString componentsSeparatedByString:@"/"]];
与
[randomplayer addObjectsFromArray:[returnString componentsSeparatedByString:@"/"]];
快速修复。
您可以使用下面的代码段替换方法的结束片段 -
NSString * trimmedString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"];
NSArray * chunks = [trimmedString componentsSeparatedByString:@"/"];
NSMutableArray *Pinfo=[[NSMutableArray alloc]init];
for (int i = 0; i < [chunks count]; i++)
{
NSString * playerInfo = [chunks objectAtIndex:i];
[Pinfo addObject:[playerInfo componentsSeparatedByString:@" "]];
}
for (int i = 0; i < [Pinfo count]; i++)
{
NSArray * info = [Pinfo objectAtIndex:i];
NSLog(@"Player %d", i);
NSLog(@"Name: %@", [info objectAtIndex:0]);
NSLog(@"Phone: %@", [info objectAtIndex:1]);
NSLog(@"Email: %@", [info objectAtIndex:2]);
}