簡體   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

我收到標題中的錯誤,我不知道為什么....

。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

我不明白我做錯了什么...有人可以幫幫我嗎?

NSIntegerNSDecimal都不是類,而是標量的類型。 您不能將消息發送到此類型的“對象”(在C的意義上,而不是OOP意義上)。 因此,不要使用+alloc-init…構造它們。

你有兩種可能性:

A.使用(OOP)對象而不是標量

@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 ,另一個是標量類型,可以將其視為具有值的整數。 十進制浮點對象用於財務軟件。

NSInteger是

typedef long NSInteger;

這應該解釋“NSInteger(又名'long')”並且它等效於64位長(long int)C數據類型。 它不是OC類,這就是為什么你不能做(+)alloc和( - )init。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM