I am getting the errors in title and I have no idea why....
.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
I dont understand what I am doing wrong...can someone help me out?
NSInteger
and NSDecimal
are no classes, but the type of scalars. You cannot sent messages to "objects" (in the sense of C, not in the sense of OOP) of this type. Therefore you do not construct them with +alloc
and -init…
.
You have two possibilities:
A. Use (OOP) objects instead of scalars
@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
_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. However, you can create an NSDecimalNumber
instance object (as in A.) and get the decimal value with -decimalValue
.
_latitude = [[NSDecimalNumber zero] decimalValue];
The usage of NSDecimal
seems to be erroneous for me. Why do you want to use it? Usually longitude and latitude are double
, another scalar type, which can be treated as integers with values. Decimal floating point objects are for use in finance software.
NSInteger is
typedef long NSInteger;
That should explain "NSInteger (aka 'long')" and it is equivalent 64-bit long (long int) C data type. It is not an OC class, that's why you can't do (+)alloc and (-)init.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.