【问题标题】:How to write a NSMutableArray of a struct type that also includes NSMutableArray inside of it?如何编写其中还包含 NSMutableArray 的结构类型的 NSMutableArray?
【发布时间】:2009-07-16 05:02:37
【问题描述】:

我想知道是否有任何用于objective-C 的示例代码来实现结构类型的NSMutableArray。在内部,我需要在结构中声明 2 个可变数组(也通过 NSMutableArray)。我书中的所有代码示例都向我展示了如何通过 C 数组语法(使用括号)创建一个定义大小的数组,但我不知道如何使用 NSMutableArray。以前有没有其他人这样做过?到目前为止,这是我的代码......它编译得很好我已经定义了数组的大小(下面的代码中使用了 2 和 5 作为示例,但我需要设置它以便让它们可变。我可以使用简单的结构,当它们只有一些“更易于理解”的数据类型,如 int、double、short、long、BOOL(你明白了)。当它进入指针时,这就是我迷失的地方(我可以很好地使用指针,但知道如何将它们放入结构中是困难的部分)。一旦代码与 NSMutableArray 一起工作,我会将“network”作为指针放入接口中以键入“Network”吗?我之前试过这个,但是我报错了,最后我基本上是希望能写出来

network.input[2].list[1].value = 5.0;

在“网络”类型的任意定义数组上。任何人都可以提供有关制作“网络”类型的 NSMutableArray 的信息的建议或链接,其中包括两个 NSMutableArray 的结构?感谢您的帮助!

SomeFile.h

#import <Foundation/Foundation.h>

struct lists{
    double value;
};

// supporting structs
struct inputs{
    struct lists list[2];
};

struct Network {
    struct inputs input[5];
    struct lists output[5];
}

@interface SomeFile : NSObject {

}

@end

SomeFile.m

#import "SomeFile.h"
@implementation SomeFile
@end

【问题讨论】:

    标签: objective-c arrays pointers struct nsmutablearray


    【解决方案1】:

    NSArray 和 NSMutableArray 只能包含 Objective-C 对象,因此不能在其中存储结构。如果内容必须是结构并且您想要类似于 NSArray 的内容,请使用 NSPointerArray,在 10.5 及更高版本中可用。

    您可以像任何其他指针类型一样在结构中存储指向 Objective-C 对象(如 NSPointerArray*id)的指针。例如,您可以为双向链表节点声明一个结构,该结构存储一个 Objective-C 对象,如下所示:

    typedef struct DoublyLinkedListNode {
        id object;
        __strong struct DoublyLinkedListNode *next;
        __strong struct DoublyLinkedListNode *prev;
    } DoublyLinkedListNode;
    

    __strong 属性与 Objective-C 2.0 中的垃圾回收一起使用,因为指向 Objective-C 对象的指针充当强引用,但 C 指针类型默认情况下不这样做。这样,只要列表中的一个节点被 __strong 引用引用,列表就不会消失。 (阅读Garbage Collection Programming Guide 了解详细信息,尤其是Using Core Foundation with Garbage Collection的后半部分。)您可能需要考虑为您的结构这样做。

    就您所需的语法而言,我可能没有完全理解您的问题,但您将无法使用方括号语法访问 Cocoa 集合中的对象,例如 NSPointerArray。 (此外,您可能必须对结构使用“-&gt;”运算符而不是“.”,因为它们很可能是在堆上分配的。所有 Objective-C 对象都必须是,并且我假设您希望将这些结构存储在方法的本地范围之外。)

    由于 Objective-C 没有泛型,你也不能“实现 [an] NSMutableArray of type struct”。事实上,one of your previous SO questions 有更多关于这个主题的细节。如果这不是您的意思,请随时澄清。

    【讨论】:

      【解决方案2】:

      这不是一个完整的答案;我不确定你不知道如何将指针放入结构中是什么意思。我将假设您要对具有动态数量的多个输入和输出的网络进行建模。

      你有几个选择:

      1. 使用值对象而不是结构来存储您的值:

        [[[network inputs] objectAtIndex:2] replaceObjectAtIndex:1 withObject:[NSNumber numberWithDouble:5.0]];
        
      2. 使用对象为您的网络建模:

        @interface Network : NSObject {
            // ivars
        }
        
        - (void) setInput:(double)value atIndex:(NSInteger)valueIndex ofInputAtIndex:(NSInteger)inputIndex;
        - (double) outputAtIndex:(NSInteger)index;
        @end
        
      3. 就像你已经在做的那样使用结构;如果您需要预先更改大小,请使用您的朋友 malloc:

        struct Network_value {
          double value;
        }
        
        struct Network {
            struct Network_value **inputs;
            struct Network_value *outputs;
        };
        
        void Network_alloc(Network *n, unsigned inputs, unsigned input_values, unsigned outputs) {
            n->outputs = calloc(sizeof(Network_value), outputs);
            n->inputs = calloc(sizeof(Network_value *), inputs);
            while (inputs --> 0) {
                n->inputs[inputs] = calloc(sizeof(Network_value), input_values);
            }
        }
        
        void Network_free(Network *n, unsigned inputs) {
            free(n->outputs);
            while (inputs --> 0) {
                free(n->inputs[inputs]);
            }
            free(n->inputs);
        }
        
        Network network;
        Network_alloc(&network, 5, 2, 5);
        
        network.inputs[2][1].value = 5.0;
        
        Network_free(&network, 2);
        
      4. 通过呈现一个 Network 对象来结合想法 2 和 3,但在内部使用结构存储值。如果输入和输出的数量非常大,这可能是个好主意。

      【讨论】:

      • 我强烈建议也设计垃圾收集。代替 malloc 和 calloc,最好研究 NSAllocateCollectable 和 NSMakeCollectable。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2015-12-20
      相关资源
      最近更新 更多