首页 > 解决方案 > 在 Swift 中扩展 Google 的 MLKit 类的问题

问题描述

我正在尝试扩展 Google 的 MLKit StrokePoint 以使其可编码:

import Foundation
import MLKit

class CodableStrokePoint: StrokePoint, Codable {
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.x, forKey: .x)
        try container.encode(self.y, forKey: .y)
        try container.encode(self.t!.intValue, forKey: .t)
    }
    
    public required convenience init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let _x = try values.decode(Float.self, forKey: .x)
        let _y = try values.decode(Float.self, forKey: .y)
        let _t = try values.decode(Int.self, forKey: .t)
        self.init(x: _x, y: _y, t: _t)
    }

    enum CodingKeys: String, CodingKey {
        case x
        case y
        case t
    }
}

但是它给了我一个链接器错误:

Undefined symbol: _OBJC_METACLASS_$_MLKStrokePoint

StrokePoint是这样定义的:

/** A single touch point from the user. */
NS_SWIFT_NAME(StrokePoint)
@interface MLKStrokePoint : NSObject

/** Horizontal coordinate. Increases to the right. */
@property(nonatomic, readonly) float x;

/** Vertical coordinate. Increases downward. */
@property(nonatomic, readonly) float y;

/** Time when the point was recorded, in milliseconds. */
@property(nonatomic, readonly, nullable) NSNumber *t;

/** Unavailable. Use `init(x:y:t:)` instead. */
- (instancetype)init NS_UNAVAILABLE;

/**
 * Creates a `StrokePoint` object using the coordinates provided as argument.
 *
 * Scales on both dimensions are arbitrary but be must be identical: a displacement of 1
 * horizontally or vertically must represent the same distance, as seen by the user.
 *
 * Spatial and temporal origins can be arbitrary as long as they are consistent for a given ink.
 *
 * @param x Horizontal coordinate. Increases to the right.
 * @param y Vertical coordinate. Increases going downward.
 * @param t Time when the point was recorded, in milliseconds.
 */
- (instancetype)initWithX:(float)x y:(float)y t:(long)t;

/**
 * Creates an `MLKStrokePoint` object using the coordinates provided as
 * argument, without specifying a timestamp. This method should only be used
 * when it is not feasible to include the timestamp information, as the
 * recognition accuracy might degrade.
 *
 * Scales on both dimensions are arbitrary but be must be identical: a
 * displacement of 1 horizontally or vertically must represent the same
 * distance, as seen by the user.
 *
 * Spatial origin can be arbitrary as long as it is consistent for a given ink.
 *
 * @param x horizontal coordinate. Increases to the right.
 * @param y vertical coordinate. Increases going downward.
 */
- (instancetype)initWithX:(float)x y:(float)y;

@end

您能否建议是否可以做我想要实现的目标以及如何做到这一点?

标签: iosswiftgoogle-mlkit

解决方案


推荐阅读