【问题标题】:How to import objective c header file in swift project如何在swift项目中导入objective c头文件
【发布时间】:2016-11-20 15:44:28
【问题描述】:

enter image description here

我正在尝试在 Swift 项目中使用 SDWebImage。我将 SDWebImage 库文件夹拖放到我的 Swift 项目中,并创建了一个名为 listingApp-Bridging-Header.h 的桥接头

然后我将头文件导入到我的 Swift 文件中;像这样导入

    import UIImageView+WebCache.h  

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell : TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell


    let strTitle : NSString = arrDict[indexPath.row].valueForKey("clip_name") as! NSString

      cell.clipName.text = strTitle as String


    cell.videoImage sd_setImageWithURL=NSURL URLWithString,[NSString stringWithFormat:@"%@%@",K_Server_imageurl,[record valueForKey:@"clip_image_path"]];



        return cell

}

它给了我一个错误提示 add ; before + 我如何将上面的文件正确导入到我的 Swift 项目中?

【问题讨论】:

标签: ios objective-c swift bridging-header


【解决方案1】:

这还不够,您还必须在 Build Settings - Swift Compiler Code Generation 中添加此桥头文件名,如下图所示:

也不要忘记这一点(SDWebImage 要求):

关于下一步:

imageDownloader.downloadImageWithURL(
                        NSURL(string: urlString!),
                        options: SDWebImageDownloaderOptions.UseNSURLCache,
                        progress: nil,
                        completed: { (image, data, error, bool) -> Void in
                            if image != nil {
                                self.bannerImageView.image = image

                            }
                    })

【讨论】:

  • 它在我将头文件作为“UIImageView+WebCatche.h”文件未找到时给出错误
  • 已完成所有建议的步骤,但现在在桥接头文件中出现错误:“UIImageView+WebCatche.h”文件未找到
  • 你拖拽完整的项目了吗?
  • 现在它在 viewController.swift 文件中给我一个错误:在这一行 import UIImageView+WebCache.h 像这样它给我错误说 add ; before + 表示连续语句必须用;分隔
  • 现在它在 viewController.swift 文件中给我一个错误:在这一行 import UIImageView+WebCache.h 像这样它给我错误说 add ; before + 表示连续语句必须用;分隔
【解决方案2】:

在桥接头中导入时需要引用头文件名。像这样:

#import "UIImageView+WebCache.h";

这与在objc中导入头文件的语法完全相同。

【讨论】:

  • 在桥接头中也导入了像#import UIImageView+WebCatche.h这样的头文件
  • 那么你应该可以在 swift 中使用代码,而无需在 swift 文件中导入任何内容。
  • 它在我将头文件作为“UIImageView+WebCatche.h”文件未找到时给出错误
  • 需要勾选项目头搜索路径。并且添加“伞”标题会比单独添加标题更好。
  • 你不需要在你的 swift 文件中导入任何东西,因为你已经在桥接头中导入了它们。
猜你喜欢
  • 2015-10-27
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
相关资源
最近更新 更多