如何通过静态(+)方法将数组添加到数组?

时间:2014-06-23 19:15:09

标签: objective-c arrays static-methods

// Marketplace.h
#import <Foundation/Foundation.h>
#import "Item.h"

@interface Marketplace : NSObject

+ (void)addItemToMarketplace:(Item *)newItem; // METHOD IN QUESTION

@end

// Marketplace.m
#import "Marketplace.h"

@interface Marketplace()
@property (strong, nonatomic) NSMutableArray *listOfItems;
@end

@implementation Marketplace


+ (void)addItemToMarketplace:(Item *)newItem // METHOD IN QUESTION
{
    [self.listOfItems addObject:newItem]; // Raises 3 errors
}

@end

我已经声明了一个类方法addItemToMarketplace,它接受Item类型的对象,并将此Item添加到我在实现文件的接口中声明的listOfItems属性中(我不确定我是否希望其他类可以使用这个性质)。我已经在另一个类中使用了这个方法[Marketplace addItemToMarketplace:newItem]。我不知道如何处理我写[self.listOfItems addObject:newItem]时引发的三个错误。

The 3 errors are as follows:
    1. Member reference type 'struct objc_class *' is a pointer; maybe you meant to use '->'?
    2. Definition of 'struct objc_class' must be imported from module 'ObjectiveC.runtime' before it is required
    3. No member named 'listOfItems' in 'struct objc_class'

Making the change proposed in #1, which changes self.listOfItems to self->listOfItems, raises the error "Member reference base type 'Class' is not a structure or union"

任何帮助都将不胜感激。

// NEW CHANGES!

在做了一些更改并遵循我在其他网站上找到的一些建议后,到目前为止我的内容是:

// Marketplace.h

#import <Foundation/Foundation.h>
#import "Item.h"

@interface Marketplace : NSObject {}

+ (Marketplace *)sharedMarket;
- (void)addItemToMarketplace:(Item *)newItem;

@end

// Marketplace.m

#import "Marketplace.h"

@interface Marketplace()
@property (strong, nonatomic)NSMutableArray *listOfItems;
@end

static Marketplace *sharedMarketplace = nil;

@implementation Marketplace

+ (Marketplace *)sharedMarket
{
    if (sharedMarketplace == nil) {
        sharedMarketplace = [[super allocWithZone:NULL] init];
    }
return sharedMarketplace;
}


- (void)addItemToMarketplace:(Item *)newItem
{
    [self.listOfItems addObject:newItem];
}

- (id)init
{
    if ( (self = [super init]) ) {
    }
    return self;
}

+ (id)allocWithZone:(NSZone *)zone {
    return [self sharedMarket];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

@end

问题,评论和疑虑?

3 个答案:

答案 0 :(得分:1)

只有实例方法可以对实例数据进行操作。因此,您需要一个实例或将数据更改为静态:

@interface Marketplace()
@end

static NSMutableArray *listOfItems;  // static is implied, so not really necessary

@implementation Marketplace

+ (void)addItemToMarketplace:(Item *)newItem {

    if (!listOfItems) listOfItems = [NSMutableArray array];
    [listOfItems addObject:newItem];
}

@end

请注意,此处没有线程安全性。

答案 1 :(得分:0)

+前缀定义了一个类方法。在该方法中self指的是类本身,几乎肯定不是你想要的。该类没有listOfItems,因为这是该类的每个实例上可用的属性。

目前尚不清楚你在这里尝试做什么。如果您来自C / C ++背景,也许您已经为此方法假设了不同的行为。您是否尝试将项目添加到由此类的特定实例管理的列表或添加到所有实例共享的列表中?

答案 2 :(得分:0)

如果您的应用中只有一个 marketplace 实例,请考虑使用单例..

然后您可以将项目添加到单个市场,如下所示:

[[Marketplace theMarketplace] addItem:<*item*>]

以下是设置此方法的一种方法:

@interface Marketplace : NSObject
@property ( nonatomic ) NSArray * items ;
@end

@implementation Marketplace

static Marketplace * __marketplace ;

+(void)load
{
    __marketplace = [ Marketplace new ] ;
}

+(instancetype)theMarketplace
{
    return __marketplace ;
}

-(void)addItem:(Item*)item
{
    self.items = [ ( self.items ?: @[] ) arrayByAddingObject:item ] ;
}

@end

(或在Swift中)

class Marketplace
{
    struct Static
    {
        static let marketplace = Marketplace()
    }

    var items:Array<Item> = [] ;

    class func get() -> Marketplace { return Static.marketplace }

    func addItem( item: Item ) { self.items += item }
}