ios - 将 swift 代码移植到 Objective-C 项目后使用未声明的标识符

标签 ios objective-c swift

我想使用最初用 Swift 编写的类

按照我应该在网上找到的说明:

  1. 将 swift 代码添加到目标
  2. 在我想要使用 swift 中定义的类的 .m 文件中添加 #import "projectName-swift.h" header 。

这样做之后,我尝试使用用 swift 代码定义的类,如下所示:

  JPEG *jpeg = nil;

但是编译器遵守使用未声明的标识符“JPEG”

这是发生的事情:

enter image description here

这是 JPEG 类:

import Foundation
import MobileCoreServices
import ImageIO

class JPEG {
    fileprivate let kFigAppleMakerNote_AssetIdentifier = "17"
    fileprivate let path : String

    init(path : String) {
        self.path = path
    }

    func read() -> String? {
        guard let makerNote = metadata()?.object(forKey: kCGImagePropertyMakerAppleDictionary) as! NSDictionary? else {
            return nil
        }
        return makerNote.object(forKey: kFigAppleMakerNote_AssetIdentifier) as! String?
    }

    func write(_ dest : String, assetIdentifier : String) {
        guard let dest = CGImageDestinationCreateWithURL(URL(fileURLWithPath: dest) as CFURL, kUTTypeJPEG, 1, nil)
            else { return }
        defer { CGImageDestinationFinalize(dest) }
        guard let imageSource = self.imageSource() else { return }
        guard let metadata = self.metadata()?.mutableCopy() as! NSMutableDictionary! else { return }

        let makerNote = NSMutableDictionary()
        makerNote.setObject(assetIdentifier, forKey: kFigAppleMakerNote_AssetIdentifier as NSCopying)
        metadata.setObject(makerNote, forKey: kCGImagePropertyMakerAppleDictionary as String as String as NSCopying)
        CGImageDestinationAddImageFromSource(dest, imageSource, 0, metadata)
    }

    fileprivate func metadata() -> NSDictionary? {
        return self.imageSource().flatMap {
            CGImageSourceCopyPropertiesAtIndex($0, 0, nil) as NSDictionary?
        }
    }

    fileprivate func imageSource() ->  CGImageSource? {
        return self.data().flatMap {
            CGImageSourceCreateWithData($0 as CFData, nil)
        }
    }

    fileprivate func data() -> Data? {
        return (try? Data(contentsOf: URL(fileURLWithPath: path)))
    }
}

发生了什么?如何做到这一点?

最佳答案

我最终通过使 NSObjectJPEG swift 类子类实现了这项工作,不知道为什么,但它现在可以工作了。

class JPEG:NSObject {
    ...
}

关于ios - 将 swift 代码移植到 Objective-C 项目后使用未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47545428/

相关文章:

当前位置的 iPhone 蜂窝三角测量

ios - ios 9 中意外的 CFBundleExecutable 键

iphone - 当调用 NSSet 的 setByAddingX 方法时,哪个集合的对象获胜?

objective-c - UIInterfaceOrientationMaskPortrait 和 UIInterfaceOrientationPortrait 有什么区别

ios - Swift:为蓝牙中央管理器选择队列

ios - 访问 Metal 文件中的快速结构或类

iphone - 在ios中创建PDF到图像

iOS:以编程方式在 UITextView 中向上、向下、向左和向右移动光标

ios - 核心数据 NSPredicate 使用 in 子句获取实体关系

Apple 解释的 Swift 示例