Question

I'm trying to use TBXML's method:

+ (id)tbxmlWithURL:(NSURL*)aURL success:(TBXMLSuccessBlock)successBlock failure:(TBXMLFailureBlock)failureBlock;

from the TBXML+HTML.h But I can't find any information about TBXMLSuccessBlock and TBXMLFailureBlock. Even on official tutorials you can only see:

TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.w3schools.com/XML/note.xml"]] retain];

But when I try it out, Xcode tells me that there is no such class method. BTW, I'm using ARC (so i don't write retain).

So the question is: how can I load an XML document using TBXML's methods from given url?

Was it helpful?

Solution

TBXMLSuccessBlock and TBXMLFailureBlock are used as an efficient way for implementing conditions for successful and unsuccessful retrieval of the XML data from the given URL. Look at Apple's Blocks programming guide for more in-depth information about Objective-C blocks.

If you just want some example code on how to use it, here it is (using the latest version of TBXML):

TBXMLSuccessBlock s = ^(TBXML *tbxml) {
    NSLog(@"yay");
    // Do something with TBXML object "tbxml
    // ...
    // An example - print the name of an element
    TBXMLElement *e = [TBXML childElementNamed:@"head" parentElement:tbxml.rootXMLElement];
    NSString *a = [TBXML elementName:e];
    NSLog(@"%@",a);
};

TBXMLFailureBlock f = ^(TBXML *tbxml, NSError *error) {
    // Do something to recover from the failure here
    // ....
    NSLog(@"nay");
};

[TBXML newTBXMLWithURL:[NSURL URLWithString:@"http://www.google.co.nz"]
                              success: s
                              failure: f];

When you execute this code, TBXMLSuccessBlock should execute if you have access to the following URL. If you can't reach the website, the TBXMLFailureBlock will execute.

As a side note, if a library you are using is non-ARC (assuming the library is source code and not a static or dynamic library ) and you are trying to integrate it into ARC-ified source, you can do this under Xcode by going to your xcodeproj file -> Build Phases -> Compile sources and for each .m file that does not use ARC, enter the compiler flag -fno-objc-arc which will tell the compiler not to compile it with ARC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top