如何在单例NSMutableArray中添加对象

时间:2016-02-15 09:49:57

标签: ios objective-c singleton nsmutablearray

我曾经存储过从服务器下载的数组数据。 但是我无法将它们保存在单例数组中。

似乎无法访问该对象。

为什么ulatitude,ulongitude,uaccuracy,uplacename是零?...

<。>文件中的

#import <Foundation/Foundation.h>
@interface LocationData : NSObject
{
    NSMutableArray *ulatitude;
    NSMutableArray *ulongitude;
    NSMutableArray *uaccuracy;
    NSMutableArray *uplacename;
}
@property (nonatomic, retain) NSMutableArray *ulatitude;
@property (nonatomic, retain) NSMutableArray *ulongitude;
@property (nonatomic, retain) NSMutableArray *uaccuracy;
@property (nonatomic, retain) NSMutableArray *uplacename;
+ (LocationData*) sharedStateInstance;
@end
<。>文件中的

#import "LocationData.h"

@implementation LocationData

@synthesize uaccuracy;
@synthesize ulatitude;
@synthesize ulongitude;

+ (LocationData*) sharedStateInstance {
        static LocationData *sharedStateInstance;

        @synchronized(self) {
            if(!sharedStateInstance) {
                sharedStateInstance = [[LocationData alloc] init];
            }
        }
        return sharedStateInstance;
}
@end

使用

[manager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
    {

                 NSLog(@"%@",responseObject);

                 // json response array
                 if ([responseObject isKindOfClass:[NSArray class]]) {

                     NSArray *responseArray = responseObject;
                     NSDictionary *responseDict = [[NSDictionary alloc] init];

                     LocationData* sharedState = [LocationData sharedStateInstance];

                     for(NSUInteger i=0; i < responseArray.count; i++)
                     {
                         responseDict = [responseArray objectAtIndex:i];

                         double dlat = [[responseDict objectForKey:@"lat"] doubleValue];
                         double dlng = [[responseDict objectForKey:@"lng"] doubleValue];

                          [[sharedState ulatitude] addObject:[NSString stringWithFormat:@"%f",dlat]];
                          [[sharedState ulongitude] addObject:[NSString stringWithFormat:@"%f",dlng]];
                          [[sharedState uaccuracy] addObject:[responseDict objectForKey:@"rad"]];
                          [[sharedState uplacename] addObject:[responseDict objectForKey:@"place_name"]];
}

5 个答案:

答案 0 :(得分:7)

您始终需要初始化阵列。在尝试向他们添加内容之前,您应该在某处执行操作:

arrayName = [[NSMutableArray alloc] init];

否则您将始终收到错误,因为它们尚未初始化。

在您的情况下,您应该覆盖LocationData init这样的功能:

- (instancetype)init {
    self = [super init];
    if (self) {
        self.yourArrayName = [[NSMutableArray alloc] init];
        // And so on....
    }
    return self;
}

答案 1 :(得分:1)

您需要正确初始化对象。基本上你的成员变量(&#34; ivars&#34;)没有任何指示(&#34; nil&#34;)。

添加到.m文件代码中的此初始化程序可以完成这项工作。

-(instancetype)init {
    if ((self = [super init])) {
                    self.accuracy = [NSMutableArray array];
                    self.latitude = [NSMutableArray array];
                    self.longitude = [NSMutableArray array];
                    self.uplacename = [NSMutableArray array];
    }
    return self;
}

作为单身人士模式,我更喜欢以下内容:

+ (LocationData*) sharedStateInstance {
    static LocationData *sharedStateInstance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        sharedStateInstance = [[LocationData alloc] init];
    });
    return sharedStateInstance;
}

虽然单身人士可能不会那么糟糕,但我不知道这对他们来说是一个很好的用法。但是,您的具体问题与该设计选择无关。

答案 2 :(得分:0)

试试这段代码。为NSMutableArrays编写getter。

#import <Foundation/Foundation.h>
@interface LocationData : NSObject

@property (nonatomic, retain) NSMutableArray *ulatitude;
@property (nonatomic, retain) NSMutableArray *ulongitude;
@property (nonatomic, retain) NSMutableArray *uaccuracy;
@property (nonatomic, retain) NSMutableArray *uplacename;
+ (LocationData*) sharedStateInstance;
@end


#import "LocationData.h"

@implementation LocationData

@synthesize uaccuracy = _uaccuracy;
@synthesize ulatitude = _ulatitude;
@synthesize ulongitude = _ulongitude;

+ (LocationData*) sharedStateInstance {
        static LocationData *sharedStateInstance;

        @synchronized(self) {
            if(!sharedStateInstance) {
                sharedStateInstance = [[LocationData alloc] init];
            }
        }
        return sharedStateInstance;
}


-(NSMutableArray*)uaccuracy
{
    if(_uaccuracy == nil)
    {
        _uaccuracy = [[NSMutableArray alloc]init];
    }
    return uaccuracy;
}

-(NSMutableArray*)ulongitude
{
    if(_ulongitude == nil)
    {
        _ulongitude = [[NSMutableArray alloc]init];
    }
    return ulongitude;
}

-(NSMutableArray*)ulatitude
{
    if(_ulatitude == nil)
    {
        _ulatitude = [[NSMutableArray alloc]init];
    }
    return ulatitude;
}

-(NSMutableArray*)uplacename
{
    if(_uplacename == nil)
    {
        _uplacename = [[NSMutableArray alloc]init];
    }
    return uplacename;
}
@end

答案 3 :(得分:-1)

你没有分配/ init任何数组...

您可以在单身创建方法中创建它们

+ (LocationData*) sharedStateInstance {
        static LocationData *sharedStateInstance;

        @synchronized(self) {
            if(!sharedStateInstance) {
                sharedStateInstance = [[LocationData alloc] init];
                sharedStateInstance.ulatitude = [[NSMutableArray alloc] init];
                // (add others...)
            }
        }
        return sharedStateInstance;
}

答案 4 :(得分:-1)

使用以下代码替换您的LocationData.m文件,这将有效。由于必须分配和初始化数组,因此只能在数组中添加对象

+ (LocationData*) sharedStateInstance {
            static LocationData *sharedStateInstance;
                    @synchronized(self) {
                    if(!sharedStateInstance) {
                        sharedStateInstance = [[LocationData alloc] init];
                        uaccuracy = [[NSMutableArray alloc]init];
                        ulatitude = [[NSMutableArray alloc]init];
                        ulongitude = [[NSMutableArray alloc]init];
                        uplacename = [[NSMutableArray alloc]init];
                    }
                }

            return sharedStateInstance;
    }