【问题标题】:Xcode: Objective-C: Type mismatchXcode:Objective-C:类型不匹配
【发布时间】:2011-08-07 20:47:51
【问题描述】:

产品构建成功,但测试失败。如何通过下面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

【问题讨论】:

    标签: objective-c xcode enums typedef type-mismatch


    【解决方案1】:

    您的type 属性是一个指针,指向TransactionType(这可能不是有意的),而TransactionTypePay 是一个实际的TransactionType

    【讨论】:

      【解决方案2】:

      您的type 属性被声明为枚举的指针,可能不应该这样

      【讨论】:

      【解决方案3】:

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

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

      但是,既然我声明了,我为什么要这样做:

      @property (nonatomic) TransactionType *type;
      

      【讨论】:

      • 糟糕。去掉type之前的星号(*)。然后,您不必投射它。
      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多