nonuma
nonuma

Reputation: 399

Possibility to check if NSData contains NSString

I'm using NSData to store XML from a webpage. Is it possible to check the NSData if it contains <?xml version="1.0"?>?

    NSURL *url = [NSURL URLWithString:link];
    NSData *data = [NSData dataWithContentsOfURL:url]; 

Upvotes: 2

Views: 2178

Answers (2)

Tobi
Tobi

Reputation: 5519

An NSData object contains raw binary data. To evaluate it's contents you first need to convert that data to specific data type - in your case a string. You need to know which encoding was used to convert the data object.

Then you can use

NSString *myXMLContent = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Now that you have the content of your XML file in that string you can evaluate it as needed.

NSRange range = [myXMLContent rangeOfString:@"whatImLookingFor" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
//found the string

Upvotes: 5

jailani
jailani

Reputation: 2270

 NSString* str = [[[NSString alloc] initWithData:theData
                                     encoding:NSUTF8StringEncoding] autorelease];


 if (str && [str length] > 0){
     NSLog(@"Contains string");
}else{
    NSLog(@"Does't contains string");

 }

Upvotes: 1

Related Questions