ios - Swift iOS - 参数标签 '(float3:)' 与任何可用的重载不匹配

标签 ios objective-c swift

我下载了 MetalPetal pod,然后尝试使用另一个 pod 中的一些文件。我不断收到 (float3:) 的这些错误和(float2:) :

enter image description here

enter image description here

该文件是 Objective-C 语言,而 Obj-C 不是我的强项。我尝试导入 #import <simd/simd.h>到文件,但没有什么区别。我该如何修复这些错误?

Controller.h 文件:

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h> // I added this import statement. It wasn't initially here

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, MTIVectorScalarType) {
    MTIVectorScalarTypeFloat,
    MTIVectorScalarTypeInt,
    MTIVectorScalarTypeUInt NS_SWIFT_NAME(uint)
} NS_SWIFT_NAME(MTIVector.ScalarType);

@interface MTIVector : NSObject <NSCopying, NSSecureCoding>

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

- (instancetype)initWithFloatValues:(const float *)values count:(NSUInteger)count NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithIntValues:(const int *)values count:(NSUInteger)count NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithUIntValues:(const uint *)values count:(NSUInteger)count NS_DESIGNATED_INITIALIZER NS_SWIFT_NAME(init(uintValues:count:));

+ (instancetype)vectorWithFloatValues:(const float *)values count:(NSUInteger)count;
+ (instancetype)vectorWithIntValues:(const int *)values count:(NSUInteger)count;
+ (instancetype)vectorWithUIntValues:(const uint *)values count:(NSUInteger)count NS_SWIFT_NAME(init(uintValues:count:));

@property (readonly, nonatomic) MTIVectorScalarType scalarType;

@property (readonly, nonatomic) NSUInteger count;

+ (instancetype)vectorWithX:(float)X Y:(float)Y;
+ (instancetype)vectorWithCGPoint:(CGPoint)point NS_SWIFT_NAME(init(value:));
+ (instancetype)vectorWithCGSize:(CGSize)size NS_SWIFT_NAME(init(value:));
+ (instancetype)vectorWithCGRect:(CGRect)rect NS_SWIFT_NAME(init(value:));

@property (readonly) CGPoint CGPointValue;
@property (readonly) CGSize CGSizeValue;
@property (readonly) CGRect CGRectValue;

@end

@interface MTIVector (Contents)

@property (readonly) NSUInteger byteLength;

- (const void *)bytes NS_RETURNS_INNER_POINTER;

@end

NS_ASSUME_NONNULL_END

Controller.m 文件:

#import "MTIVector.h"

@interface MTIVector ()

@property (nonatomic, copy, readonly) NSData *data;

@end

@implementation MTIVector

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (instancetype)initWithIntValues:(const int *)values count:(NSUInteger)count {
    if (self = [super init]) {
        NSParameterAssert(values);
        NSParameterAssert(count > 0);
        _count = count;
        _data = [NSData dataWithBytes:values length:count * sizeof(int)];
        _scalarType = MTIVectorScalarTypeInt;
    }
    return self;
}

- (instancetype)initWithFloatValues:(const float *)values count:(NSUInteger)count {
    if (self = [super init]) {
        NSParameterAssert(values);
        NSParameterAssert(count > 0);
        _count = count;
        _data = [NSData dataWithBytes:values length:count * sizeof(float)];
        _scalarType = MTIVectorScalarTypeFloat;
    }
    return self;
}

- (instancetype)initWithUIntValues:(const uint *)values count:(NSUInteger)count {
    if (self = [super init]) {
        NSParameterAssert(values);
        NSParameterAssert(count > 0);
        _count = count;
        _data = [NSData dataWithBytes:values length:count * sizeof(uint)];
        _scalarType = MTIVectorScalarTypeUInt;
    }
    return self;
}

+ (instancetype)vectorWithFloatValues:(const float *)values count:(NSUInteger)count {
    return [[MTIVector alloc] initWithFloatValues:values count:count];
}

+ (instancetype)vectorWithIntValues:(const int *)values count:(NSUInteger)count {
    return [[MTIVector alloc] initWithIntValues:values count:count];
}

+ (instancetype)vectorWithUIntValues:(const uint *)values count:(NSUInteger)count {
    return [[MTIVector alloc] initWithUIntValues:values count:count];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    NSData *data = [coder decodeObjectOfClass:[NSData class] forKey:@"data"];
    NSNumber *scalarTypeValue = [coder decodeObjectOfClass:[NSNumber class] forKey:@"scalarType"];
    if (data == nil || scalarTypeValue == nil) {
        return nil;
    }
    switch ([scalarTypeValue integerValue]) {
        case MTIVectorScalarTypeFloat:
            return [self initWithFloatValues:data.bytes count:data.length/sizeof(float)];
        case MTIVectorScalarTypeInt:
            return [self initWithIntValues:data.bytes count:data.length/sizeof(int)];
        case MTIVectorScalarTypeUInt:
            return [self initWithUIntValues:data.bytes count:data.length/sizeof(uint)];
        default:
            return nil;
    }
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:_data forKey:@"data"];
    [coder encodeObject:@(_scalarType) forKey:@"scalarType"];
}

+ (BOOL)supportsSecureCoding {
    return YES;
}

- (NSUInteger)hash {
    return _data.hash;
}

- (BOOL)isEqual:(id)object {
    if (self == object) {
        return YES;
    }
    if ([object isKindOfClass:[MTIVector class]]) {
        MTIVector *other = object;
        if (self.count != other.count) {
            return NO;
        }
        return [_data isEqual:other -> _data];
    } else {
        return NO;
    }
}

+ (instancetype)vectorWithX:(float)X Y:(float)Y {
    float values[2] = {X, Y};
    return [[self alloc] initWithFloatValues:values count:2];
}

+ (instancetype)vectorWithCGPoint:(CGPoint)p {
    float values[2] = {(float)p.x, (float)p.y};
    return [[self alloc] initWithFloatValues:values count:2];
}

- (CGPoint)CGPointValue {
    if (self.count == 2 && self.scalarType == MTIVectorScalarTypeFloat) {
        const float * bytes = self.bytes;
        return CGPointMake(bytes[0], bytes[1]);
    }
    return CGPointZero;
}

+ (instancetype)vectorWithCGSize:(CGSize)s {
    float values[2] = {(float)s.width, (float)s.height};
    return [[self alloc] initWithFloatValues:values count:2];
}

- (CGSize)CGSizeValue {
    if (self.count == 2 && self.scalarType == MTIVectorScalarTypeFloat) {
        const float * bytes = self.bytes;
        return CGSizeMake(bytes[0], bytes[1]);
    }
    return CGSizeZero;
}

+ (instancetype)vectorWithCGRect:(CGRect)r {
    float values[4] = {(float)r.origin.x, (float)r.origin.y, (float)r.size.width, (float)r.size.height};
    return [[self alloc] initWithFloatValues:values count:4];
}

- (CGRect)CGRectValue {
    if (self.count == 4 && self.scalarType == MTIVectorScalarTypeFloat) {
        const float * bytes = self.bytes;
        return CGRectMake(bytes[0], bytes[1], bytes[2], bytes[3]);
    }
    return CGRectZero;
}

@end

@implementation MTIVector (Contents)

- (NSUInteger)byteLength {
    return _data.length;
}

- (const void *)bytes {
    return _data.bytes;
}

@end

最佳答案

MTIVector类没有采用 float3 的初始化程序或float2争论。有

- (instancetype)initWithFloatValues:(const float *)values count:(NSUInteger)count NS_DESIGNATED_INITIALIZER;

初始化程序被导入到 Swift 中

public init(floatValues values: UnsafePointer<Float>, count: UInt)

并且可以从 Swift 调用,例如

let vec = MTIVector(floatValues: [0.0, 1.0, 2.0], count: 3)

这是有效的,因为传递了 [Float]数组到 C 函数,采用 UnsafePointer<Float>参数自动传递一个指向数组元素存储的指针。您还可以定义一个

extension MTIVector {
    convenience init(floatArray: [Float]) {
        self.init(floatValues: floatArray, count: UInt(floatArray.count))
    }
}

并将其用作

let vec = MTIVector(floatArray: [0.0, 1.0, 2.0])

关于ios - Swift iOS - 参数标签 '(float3:)' 与任何可用的重载不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55850095/

相关文章:

javascript - 从浏览器将约会写入 iOS 日历

java - 音频频谱效果 iOS/Android

ios - 添加几个 NSStrings 到 NSMutableArray

ios - Swift:从 Firebase 打印坐标:0,1 - 2,3 而不是 0,1 - 1,2

ios - 使用标签栏 Controller 和导航 Controller

ios - SKAction 序列是否真的等到 Action 结束?

ios - 在某些页面上显示主视图并防止用户隐藏它

ios - NSURLConnection 返回多个 php session ID

ios - 显示不正确值的自定义 UITableViewCell

json - 无需在 Swift 中关闭应用程序即可重新加载已解析的 URL