-(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/
中获取字符串的代码不工作。如何让它发挥作用?
最佳答案
替换
[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]);
}
关于iphone - 来自字符串的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464546/