如何在iphone中的正则表达式上给出componentsSeparatedByString

时间:2011-09-12 08:52:50

标签: iphone objective-c

朋友你好,

我在这里进行规则我创建所有并且我存储在数组中并在控制台中打印但是我也在组中给予但是当我给componentsSeparatedByString不工作时为什么我在做这个代码时我错了我的应用程序goin崩溃和日志消息打印此meassage: -

[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x4e283a0
    2011-09-12 14:05:48.400 RegexKitLiteDemo[10576:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x4e283a0

这是我的代码请一些人帮我正确的方向

-(void)sendRequest

{
    // create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bookryanair.com/SkySales/FRSearch.aspx"]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {

        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);
    } else {
        // inform the user that the download could not be made
    }   

}

/// this for checking is that Sync is work or not 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{   
    [webData setLength: 0]; 


} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{         
    [webData appendData:data]; 

} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{     
    [connection release];  
    [webData release]; 
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{      
    loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 



    [connection release];

    NSString *regexString = @"Stations\\[""(.*)""\\] = new Station\\((.*)new Array\\((.*)\\)\\);"; 
    matchArray = [loginStatus arrayOfCaptureComponentsMatchedByRegex:regexString];




    NSLog(@"matchArray: %@", matchArray);


    group = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];

    for (int i = 0; i < [matchArray count]; i++) {

         NSString *temp = [[matchArray objectAtIndex: i] componentsSeparatedByString: @","];

        [group addObject: temp];
    NSLog(@"group: %@", group);  
    }   
} 

3 个答案:

答案 0 :(得分:2)

componentsSeparatedByString方法应用于NSString并返回NSArray;

- (NSArray *)componentsSeparatedByString:(NSString *)separator

e.g。 NSArray *array = [aString componentsSeparatedByString:@","];

因此,在您的代码中,对于初学者来说,以下行是错误的;

NSString *temp = [[matchArray objectAtIndex: i] componentsSeparatedByString: @","];

答案 1 :(得分:0)

如果您尝试将NSString对象的NSArray转换为用逗号分隔的单个字符串,请尝试这样:

NSString *temp = [[matchArray objectAtIndex:i] componentsJoinedByString:@","];
调用数组的

componentsJoinedByString将从数组组件返回一个字符串。

调用字符串的

componentsSeparatedByString将返回由字符串组件组成的数组(取决于分隔符)

答案 2 :(得分:0)

当您调用方法componentsSeparatedByString的实例无效时,也会遇到正在接收的错误。在这里因为您已经使用NSLog打印MAtcharray,我建议您检查它是否正确打印。此外,因为您尝试逐个元素访问并调用组件....方法,通过逐个元素打印来确认它是否确实存在。