开发者

Xcode: Objective-C: Type mismatch

开发者 https://www.devze.com 2023-03-26 18:29 出处:网络
The product build succeeds, but the test fails. How do I pass the type-mismatch failure reported on the line with STAssertEquals below?

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

@syn开发者_Go百科thesize type;

@end


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


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消