【问题标题】:Cant set values of custom object无法设置自定义对象的值
【发布时间】:2015-05-24 11:01:49
【问题描述】:

我有一个自定义数据对象,用于存储从 JSON 请求传递的信息,但是当我尝试设置这些值时,一切仍然为空。

我对 iOS 开发还是很陌生,并且有更多的 Java 背景,所以我确定我只是错过了一些东西或做错了一些事情。

我的班级:

建筑位置.h

#import <Foundation/Foundation.h>

@interface BuildingLocation : NSObject {
    NSString *_name;
    NSString *_description;
    NSString *_URL;
    float  _Long;
    float  _Lat;
    NSString *_Town;
    NSString *_Type;
    NSString *_Premium;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *URL;
@property (nonatomic) float Long;
@property (nonatomic) float Lat;
@property (nonatomic, copy) NSString *Town;
@property (nonatomic, copy) NSString *Type;
@property (nonatomic, copy) NSString *Premium;

- (id)init:(NSString *)name description:(NSString *)description URL:(NSString *)URL Long:(float)Long Lat:(float)Lat Town:(NSString *)Town Type:(NSString *)Type Premium:(NSString *)Premium;

@end

建筑位置.m

#import "BuildingLocation.h"

@implementation BuildingLocation

@synthesize name = _name;
@synthesize description = _description;
@synthesize URL = _URL;
@synthesize Long = _Long;
@synthesize Lat =_Lat;
@synthesize Town = _Town;
@synthesize Type = _Type;
@synthesize Premium = _Premium;

-(id)init:(NSString *)name description:(NSString *)description URL:(NSString *)URL Long:(float)Long Lat:(float)Lat Town:(NSString *)Town Type:(NSString *)Type Premium:(NSString *)Premium {
    if ((self = [super init])) {
        self.name = name;
        self.description = description;
        self.URL = URL;
        self.Long = Long;
        self.Lat = Lat;
        self.Town = Town;
        self.Type = Type;
        self.Premium = Premium;
    }
    return self;
}

@end

我如何尝试在我的代码中设置值:

BuildingLocation *locationObject;
                locationObject.name = [location objectForKey:@"Name"];
                locationObject.description = [location objectForKey:@"Decription"];
                locationObject.URL = [location objectForKey:@"URL"];
                locationObject.Long = [[location objectForKey:@"Longitude"] floatValue];
                locationObject.Lat = [[location objectForKey:@"Lat"] floatValue];
                locationObject.Town = [location objectForKey:@"Town"];
                locationObject.Type = [location objectForKey:@"Type"];
                locationObject.Premium = [location objectForKey:@"Premium"];

                NSLog(@"Name before: %@", locationObject.name);

NSLog 将 locationObject.name 的值设为 (null)

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    locationObject 对象永远不会被分配和初始化。

    试试这个:

    BuildingLocation *locationObject = [[BuildingLocation alloc] init]; // allocate and initialize this object.
    //  ...
    NSLog(@"Name before: %@", locationObject.name);
    

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多