Xcode:Objective-C:类型不匹配

发布于 2024-11-28 12:07:59 字数 825 浏览 4 评论 0原文

产品构建成功,但测试失败。如何通过下面的 STAssertEquals 传递报告的类型不匹配失败?

// TransactionSpec.m

#import "Transaction.h"

@interface TransactionSpec : SenTestCase
@end

@implementation TransactionSpec

#pragma mark Properties

- (void)testProperties {
    Transaction *transaction = [[Transaction alloc] init];
    transaction.type = TransactionTypePay;

    STAssertNotNil(transaction, @"transaction exists");
    STAssertEquals(transaction.type, TransactionTypePay, @"type property works"); // Type mismatch
}

@end

// Transaction.h

typedef enum {
    TransactionTypePay,
    TransactionTypeCharge
} TransactionType;

@interface Transaction : NSObject

@property (nonatomic) TransactionType *type;

@end

// Transaction.m

#import "Transaction.h"

@implementation Transaction

@synthesize type;

@end

The product build succeeds, but the test fails. How do I pass the type-mismatch failure reported on the line with STAssertEquals below?

// TransactionSpec.m

#import "Transaction.h"

@interface TransactionSpec : SenTestCase
@end

@implementation TransactionSpec

#pragma mark Properties

- (void)testProperties {
    Transaction *transaction = [[Transaction alloc] init];
    transaction.type = TransactionTypePay;

    STAssertNotNil(transaction, @"transaction exists");
    STAssertEquals(transaction.type, TransactionTypePay, @"type property works"); // Type mismatch
}

@end

// Transaction.h

typedef enum {
    TransactionTypePay,
    TransactionTypeCharge
} TransactionType;

@interface Transaction : NSObject

@property (nonatomic) TransactionType *type;

@end

// Transaction.m

#import "Transaction.h"

@implementation Transaction

@synthesize type;

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我太投入 2024-12-05 12:07:59

您的 type 属性是一个指向 TransactionType指针(这可能不是有意的),而 TransactionTypePay 是一个实际的 <代码>交易类型。

Your type property is a pointer to a TransactionType (which is probably not intended) while TransactionTypePay is an actual TransactionType.

绻影浮沉 2024-12-05 12:07:59

您的 type 属性被声明为枚举的指针,但事实可能不应该如此

Your type property is declared as a pointer to enum, which probably should not be so

相权↑美人 2024-12-05 12:07:59

transaction.type 转换为 TransactionType 可以解决问题:

STAssertEquals((TransactionType)transaction.type, TransactionTypePay, @"type property works");

但是,既然我声明了:

@property (nonatomic) TransactionType *type;

Casting transaction.type to TransactionType fixes the issue:

STAssertEquals((TransactionType)transaction.type, TransactionTypePay, @"type property works");

But, why should I have to do that since I declare:

@property (nonatomic) TransactionType *type;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文