简体   繁体   English

Objective-C - 接收器类型'NSInteger'(又名'long')不是Objective-C类和接收器类型'NSDecimal'不是Objective-C类

[英]Objective-C - Receiver type 'NSInteger' (aka 'long') is not an Objective-C class & Receiver type 'NSDecimal' is not an Objective-C class

I am getting the errors in title and I have no idea why.... 我收到标题中的错误,我不知道为什么....

.h 。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 .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

I dont understand what I am doing wrong...can someone help me out? 我不明白我做错了什么...有人可以帮帮我吗?

NSInteger and NSDecimal are no classes, but the type of scalars. NSIntegerNSDecimal都不是类,而是标量的类型。 You cannot sent messages to "objects" (in the sense of C, not in the sense of OOP) of this type. 您不能将消息发送到此类型的“对象”(在C的意义上,而不是OOP意义上)。 Therefore you do not construct them with +alloc and -init… . 因此,不要使用+alloc-init…构造它们。

You have two possibilities: 你有两种可能性:

A. Use (OOP) objects instead of scalars A.使用(OOP)对象而不是标量

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

Then you can assign references to objects as usual: 然后您可以像往常一样为对象分配引用:

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

B. Simply assign a value B.只需指定一个值

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

This works good for integers, but bad for decimals, since they are a struct with private components and there is no function to create them. 这适用于整数,但对于小数则不好,因为它们是具有私有组件的struct ,并且没有创建它们的函数。 However, you can create an NSDecimalNumber instance object (as in A.) and get the decimal value with -decimalValue . 但是,您可以创建NSDecimalNumber实例对象(如A中所示)并使用-decimalValue获取小数值。

_latitude = [[NSDecimalNumber zero] decimalValue];

The usage of NSDecimal seems to be erroneous for me. NSDecimal的使用似乎对我来说是错误的。 Why do you want to use it? 你为什么要用它? Usually longitude and latitude are double , another scalar type, which can be treated as integers with values. 通常经度和纬度是double ,另一个是标量类型,可以将其视为具有值的整数。 Decimal floating point objects are for use in finance software. 十进制浮点对象用于财务软件。

NSInteger is NSInteger是

typedef long NSInteger;

That should explain "NSInteger (aka 'long')" and it is equivalent 64-bit long (long int) C data type. 这应该解释“NSInteger(又名'long')”并且它等效于64位长(long int)C数据类型。 It is not an OC class, that's why you can't do (+)alloc and (-)init. 它不是OC类,这就是为什么你不能做(+)alloc和( - )init。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM