iphone - 来自字符串的子字符串

标签 iphone ios cocoa-touch

-(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/

相关文章:

ios - UINavigationController pushViewController 在中途暂停/卡住

iphone - 高效使用 Interface Builder

android - Flutter - 在 ThemeData 中使用自定义颜色和主题

objective-c - 在缩放 UIScrollView subview 时保持恒定的线宽

iOS-核心蓝牙

ios - 突出显示整个 UIButton,而不仅仅是左侧的图像?

objective-c - 是否可以读取设备的视频库?

ios - 多次调用 UICollectionView cellForItemAtIndexPath

iphone - 调整 UIWebView 高度以适应内容

ios - 如何转换 iPhone 应用程序以支持 iPad