how to give componentsSeparatedByString on regular expression in iphone
Hello friends,
I am doing regulation here i create all and i store in array and print that in console also but i give in group also but when i give componentsSeparatedByString
is not working why where i am wrong when i doing this code my application goin crash and log message print this meassage:-
[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x4e283a0
2011-09-12 14:05:48.400 RegexKitLi开发者_如何学运维teDemo[10576:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x4e283a0
This is my code please some one help me in right direction
-(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);
}
}
The componentsSeparatedByString method is applied on a NSString and it returns and NSArray;
- (NSArray *)componentsSeparatedByString:(NSString *)separator
e.g. NSArray *array = [aString componentsSeparatedByString:@","];
So, in your code, for starters, the following line is wrong;
NSString *temp = [[matchArray objectAtIndex: i] componentsSeparatedByString: @","];
If you are trying to turn an NSArray of NSString objects into a single string separated by a comma, try it this way:
NSString *temp = [[matchArray objectAtIndex:i] componentsJoinedByString:@","];
componentsJoinedByString
called on an array will return a single string from the array components.
componentsSeparatedByString
called on a string will return an array made up of the string components (depending on the separator)
Also the error u are receiving is encountered when the instance you are calling the method componentsSeparatedByString is invalid. Here since you are already printing MAtcharray using NSLog, i suggest you check if it is properly printed. Also since you are trying to access element by element and call the components.... method on that do check by printing element by element to confirm if it actually exists.
精彩评论