【问题标题】:Incompatible pointer to integer conversion指向整数转换的不兼容指针
【发布时间】:2013-10-18 08:40:36
【问题描述】:

我正在学习 Objective-C,但我在项目中遇到了一些问题。 我用创建新对象的方法创建了一个类,但在尝试创建新对象时遇到了一些问题,我不知道在这种情况下最好的比例是什么。

类.h

    #import <Foundation/Foundation.h>

@interface ObrasData : NSObject

@property (strong) NSNumber *ID;
@property (assign) char presupuesto;
@property (assign) char descripcion;
@property (assign) char aasm_state;
@property (assign) char clienteID;

- (id)initWithID:(NSNumber*)ID presupuesto:(char)presupuesto description:(char)description aasm_state:(char)aasm_state clienteID:(char)clienteID;

@end

类.m

@implementation ObrasData

@synthesize ID = _ID;
@synthesize presupuesto = _presupuesto;
@synthesize descripcion = _descripcion;
@synthesize aasm_state = _aasm_state;
@synthesize clienteID = _clienteID;

- (id)initWithID:(NSNumber *)ID presupuesto:(char)presupuesto description:(char)description aasm_state:(char)aasm_state clienteID:(char)clienteID{
    if ((self = [super init])) {
        self.ID = ID;
        self.presupuesto = presupuesto;
        self.descripcion = description;
        self.aasm_state = aasm_state;
        self.clienteID = clienteID;
    }
    return self;
}

这里我遇到了错误:“指向整数转换的指针不兼容”

 ObrasData *obra1 = [[ObrasData alloc] initWithID:[NSNumber numberWithInt:1] presupuesto:100 description:@"obra de prueba" aasm_state:@"En proceso" clienteID:@"dm2"];

我做错了什么?我想稍后在列表视图上显示该对象

【问题讨论】:

  • 用作“NSString”属性而不是“char”
  • 您将stringinteger 值作为参数传递,而您的函数只接受charNSString 类型值。

标签: objective-c uitableview


【解决方案1】:

类.h

    #import <Foundation/Foundation.h>

@interface ObrasData : NSObject

@property (strong) NSNumber *ID;
@property (strong) NSString *presupuesto;
@property (strong) NSString *descripcion;
@property (strong) NSString *aasm_state;
@property (strong) NSString *clienteID;

- (id)initWithID:(NSNumber*)ID presupuesto:(NSString *)presupuesto description:(NSString *)description aasm_state:(NSString *)aasm_state clienteID:(NSString *)clienteID;

@end

类.m

@implementation ObrasData

@synthesize ID = _ID;
@synthesize presupuesto = _presupuesto;
@synthesize descripcion = _descripcion;
@synthesize aasm_state = _aasm_state;
@synthesize clienteID = _clienteID;

- (id)initWithID:(NSNumber *)ID presupuesto:(NSString *)presupuesto description:(NSString *)description aasm_state:(NSString *)aasm_state clienteID:(NSString *)clienteID{
    if ((self = [super init])) {
        self.ID = ID;
        self.presupuesto = presupuesto;
        self.descripcion = description;
        self.aasm_state = aasm_state;
        self.clienteID = clienteID;
    }
    return self;
}

并像这样调用: ObrasData *obra1 = [[ObrasData alloc] initWithID:[NSNumber numberWithInt:1] presupuesto:@"100" description:@"obra de prueba" aasm_state:@"En proceso" clienteID:@"dm2"];

【讨论】:

  • 字符串属性可能应该声明为strong或更好的copy,而不是assign
  • 如果您需要在运行时添加,则使用 NSMutableArray 并使用 alloc 和 addObject 进行初始化,否则直接初始化,更多取决于内存优化等。
【解决方案2】:

在 initWithID 方法中,第二个参数输入类型是 char 但您传递的是整数 100。

[[ObrasData alloc] initWithID:[NSNumber numberWithInt:1] presupuesto:100 描述:@"obra de prueba" aasm_state:@"En proceso" clienteID:@"dm2"];

presupuesto 更改为 Char

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多