Compilation error using TFHpple framework from Swi

2019-02-11 09:50发布

问题:

I have this HTML,

 <div class="header1" style="text-align: center;">


 <div class="s">
          <center>
                  <table>
       <tr>
                          <td>
                     <ul style="position: relative;">
                       <li>
                    <a>
         <p>
   Apple Kid

        </p>

and I want to get the the word "Apple kid" as an output using Swift.

I used the TFHpple Framework for this, but after adding all the necessary things it showing an error. Like in the Objective-C file

the  XPathQuery.h
//
//  XPathQuery.h
//  FuelFinder
//
//  Created by Matt Gallagher on 4/08/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query);
NSArray *PerformHTMLXPathQueryWithEncoding(NSData *document, NSString *query,NSString *encoding);
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);
NSArray *PerformXMLXPathQueryWithEncoding(NSData *document, NSString *query,NSString *encoding);

It showing an error at these functions about Unknown type.

The XpathQuery.m

//
//  XPathQuery.m
//  FuelFinder
//
//  Created by Matt Gallagher on 4/08/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "XPathQuery.h"

#import <libxml/tree.h>
#import <libxml/parser.h>
#import <libxml/HTMLparser.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>

NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult,BOOL parentContent);
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query);

回答1:

You should share the precise errors you received, but I had no problems using TFHpple from Swift. As you do with Objective-C, you have to

  • make sure you link to the libxml library in your build phases

  • add $(SDKROOT)/usr/include/libxml2 to your header search paths in the build settings

See the installation instructions on Wenderlich's How To Parse HTML on iOS or Cocoa with Love's Using libxml2 for XML parsing and XPath queries in Cocoa.

When you add the TFHpple source to your project, you should be prompted to create a bridging header. Do so, and then add #import "TFHpple" to it. For more information, see the Importing Objective-C into Swift section of the Swift and Objective-C in the Same Project chapter of the Using Swift with Cocoa and Objective-C guide.

Once you do that, you should be able to use TFHpple without incident from Swift.