Objective-C- 接收者类型 'NSInteger' (又名 'long') 不是一个Objective-C类 & 接收者类型 'NSDecimal' 不是一个Objective-C类。

5
我遇到了标题错误,但不知道为什么...
.h
@interface ItemData : NSObject

@property (nonatomic,assign) NSInteger tagId;
@property (nonatomic,strong) NSMutableString *deviceId;
@property (nonatomic,strong) NSDecimal *latitude;
@property (nonatomic,strong) NSDecimal *longitude;


@end

.m

#import <Foundation/Foundation.h>
#import "ItemData.h"

@implementation ItemData

-(id)init
{
    if(self = [super init])
    {
        //TODO: Fix Commented out items

        _tagId = [[NSInteger alloc] init];
        _deviceId = [[NSMutableString alloc] init];
        _latitude = [[NSDecimal alloc] init];
        _longitude = [[NSDecimal alloc] init];

    }
    return self;
}

@end

我不明白我做错了什么...有人能帮帮我吗?

NSInteger不是对象类型,它是类似于int或char的内在类型。你不需要alloc/init它。同样地,NSDecimal是一个结构体,而不是类,所以你也不需要alloc/init。 - Paulw11
2个回答

2

NSIntegerNSDecimal不是类,而是标量的类型。您不能向此类型的“对象”(在C的意义上,而不是面向对象编程的意义上)发送消息。因此,您不使用+alloc-init…来构造它们。

您有两种选择:

A. 使用(面向对象编程的)对象代替标量

@property (nonatomic,assign) NSNumber *tagId;
@property (nonatomic,strong) NSDecimalNumber *latitude;

然后,您可以像往常一样将引用分配给对象:

_tagId = @0; // Or whatever you want.
_latitude = [NSDecimalNumber zero];

B. 只需赋值

_tagId = 0; // Or whatever you want. There is no nil for integers

这对于整数很好用,但对于小数不太实用,因为它们是一个有私有部件的struct,没有可以创建它们的函数。但是,你可以创建一个NSDecimalNumber实例对象(如A.中所示),并使用-decimalValue获取十进制值。

_latitude = [[NSDecimalNumber zero] decimalValue];

NSDecimal 的使用对我来说似乎是错误的。为什么要使用它?通常经度和纬度是 double,这是另一种标量类型,可以视为具有值的整数。十进制浮点对象用于金融软件。


0

NSInteger 是一个

typedef long NSInteger;

这应该解释了 "NSInteger (又名 'long')",它相当于64位长整型(long int)C数据类型。它不是OC类,这就是为什么你不能使用 (+)alloc 和 (-)init 的原因。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接