Xcode:Objective-C:类型不匹配
产品构建成功,但测试失败。如何通过下面的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
type
属性是一个指向TransactionType
的指针(这可能不是有意的),而TransactionTypePay
是一个实际的 <代码>交易类型。Your
type
property is a pointer to aTransactionType
(which is probably not intended) whileTransactionTypePay
is an actualTransactionType
.您的
type
属性被声明为枚举的指针,但事实可能不应该如此Your
type
property is declared as a pointer to enum, which probably should not be so将
transaction.type
转换为TransactionType
可以解决问题:但是,既然我声明了:
Casting
transaction.type
toTransactionType
fixes the issue:But, why should I have to do that since I declare: