将对象添加到NSMUtableArray Singleton

时间:2014-02-23 16:00:01

标签: ios objective-c cocoa-touch

我有一个共享的单件类NSMutableArray [ICGlobals sharedApplianceCount](第一次使用这种模式,所以如果我在这里做了一些非常愚蠢的话,请耐心等待我)

·H

#import <Foundation/Foundation.h>

@interface ICGlobals : NSObject

{

NSMutableArray* applianceCount;
}


@property (nonatomic, retain) NSString *applianceCount;

+ (ICGlobals *)sharedApplianceCount;

@end

的.m

#import "ICGlobals.h"

@implementation ICGlobals

static ICGlobals *sharedApplianceCount = nil;


+ (ICGlobals *)sharedUser {
if(sharedApplianceCount == nil){
    sharedApplianceCount = [[super allocWithZone:NULL] init];
  }
return sharedApplianceCount;
}

+ (id)allocWithZone:(NSZone *)zone {
return [self sharedApplianceCount];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end

在“其他视图控制器”中,我尝试添加row count table view(可更改的行数)= self.circuits.count

试过这个

[[ICGlobals sharedApplianceCount] addObject: self.circuits.count,nil]];

[[ICGlobals sharedApplianceCount]   = [[NSMutableArray alloc] init];

[[ICGlobals sharedApplianceCount] addObject: self.circuits.count,Nil]];

我没有看到@interface错误,说我的单例类声明了选择器

相同
NSNumber* numberOfRows = [NSNumber numberWithInteger:self.circuits.count];

[[ICGlobals sharedApplianceCount]addObject:[NSMutableArray arrayWithObjects:numberOfRows, nil]];

[ICGlobals sharedApplianceCount] = self.circuits.count;

我得到表达式可分配。单例类已导入。

2 个答案:

答案 0 :(得分:1)

您的界面声明中存在不一致之处。您声明类型为NSMutableArray的ivar,然后声明NSString属性。 Firstable,你不需要申报ivar,声明一个属性为你做。所以你的界面应该是这样的:

@interface ICGlobals : NSObject

@property (nonatomic, retain) NSMutableArray *applianceCount;

+ (ICGlobals *)sharedApplianceCount;

@end

此外,您还有一个命名故障。您不应该将名称applianceCount用于数组。通常,Cocoa的命名惯例表明count应为数字(intNSUInteger)。我会将此属性名称更改为applianceCounts

然后,当您初始化单线时,您还可以初始化阵列:

+ (ICGlobals *)sharedUser 
{
    if(sharedApplianceCount == nil)
    {
        sharedApplianceCount = [[super allocWithZone:NULL] init];
        sharedApplianceCount.applianceCounts = [[NSMutableArray alloc] init];
    }

    return sharedApplianceCount;
}

最后,以下是如何从视图控制器向单线程序的applianceCounts数组添加数据。

NSNumber* numberOfRows = [NSNumber numberWithInteger:self.circuits.count];

[[ICGlobals sharedApplianceCount].applianceCounts addObject:numberOfRows];

这应该指向正确的方向。 我没有完全明白你想要实现的目标,就像我不明白为什么你想在那里有阵列一样,所以如果你需要进一步的帮助,请在评论中告诉我。

我完全建议您阅读有关命名约定的内容。这篇文章是一个好的开始:

Introduction to Coding Guidelines for Cocoa.

答案 1 :(得分:1)

我会向你的班级推荐一些重构。 首先你做这样的界面:

@interface ICGlobals : NSObject
// add the app count but make it private, because you will provide methods to access it
@property (nonatomic, readonly) NSString *applianceCount;

// return ICGlobals instance 
+ (ICGlobals)sharedCounter;

- (NSInteger)count;
- (void)addObject:(id)object;

现在在.m文件中

@implementation ICGlobals

static ICGlobals *sharedApplianceCount = nil;
// this is your method, just changed the name
+ (ICGlobals *)sharedCounter {
if(sharedApplianceCount == nil){
    sharedApplianceCount = [[super allocWithZone:NULL] init];
  }
return sharedApplianceCount;
}

// instance methods goes here 
- (NSInteger)count
{
    return _applicationCount.count;
}

- (void)addObject:(id)object
{
    [_applicationCount addObject:object];
}

现在从任何viewController

调用[[ICGlobals sharedCount]addObject:object]