在 Objective-C 中创建一个整数属性数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/476843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:04:08  来源:igfitidea点击:

Create an array of integers property in Objective-C

objective-carraysproperties

提问by jebcrum

I'm having troubles creating a property of an array of integers in Objective-C. I'm not sure whether this is even possible to do in Obj-C so I'm hoping someone can help me in finding out either how to do it correctly or provide an alternative solution.

我在 Objective-C 中创建整数数组的属性时遇到了麻烦。我不确定这是否可以在 Obj-C 中做到,所以我希望有人能帮助我找出如何正确地做到这一点或提供替代解决方案。

myclass.h

我的类.h

@interface myClass : NSObject {

@private int doubleDigits[10];
}

@property int doubleDigits;
@end

myclass.m

我的班级

@implementation myClass

    @synthesize doubleDigits;
    -(id) init {

        self = [super init];

        int doubleDigits[10] = {1,2,3,4,5,6,7,8,9,10};

        return self;
    }

    @end

When I build and run, I get the following error:

当我构建并运行时,出现以下错误:

error: type of property 'doubleDigits' does not match type of ivar 'doubleDigits'

错误:属性“doubleDigits”的类型与 ivar“doubleDigits”的类型不匹配

回答by robottobor

This should work:

这应该有效:

@interface MyClass
{
    int _doubleDigits[10]; 
}

@property(readonly) int *doubleDigits;

@end

@implementation MyClass

- (int *)doubleDigits
{
    return _doubleDigits;
}

@end

回答by lucius

C arrays are not one of the supported data types for properties. See "The Objective-C Programming Language" in Xcode documentation, in the Declared Properties page:

C 数组不是属性支持的数据类型之一。请参阅 Xcode 文档中的“Objective-C 编程语言”,在“声明的属性”页面中:

Supported Types

You can declare a property for any Objective-C class, Core Foundation data type, or “plain old data” (POD) type (see C++ Language Note: POD Types). For constraints on using Core Foundation types, however, see “Core Foundation.”

支持的类型

您可以为任何 Objective-C 类、Core Foundation 数据类型或“普通旧数据”(POD) 类型声明属性(请参阅 C++ 语言注释:POD 类型)。但是,对于使用 Core Foundation 类型的限制,请参阅“Core Foundation”。

POD does not include C arrays. See http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

POD 不包括 C 数组。见 http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

If you need an array, you should use NSArray or NSData.

如果你需要一个数组,你应该使用 NSArray 或 NSData。

The workarounds, as I see it, are like using (void *) to circumvent type checking. You can do it, but it makes your code less maintainable.

在我看来,解决方法就像使用 (void *) 来规避类型检查。您可以这样做,但它会使您的代码不易维护。

回答by Tim Vermeulen

Like lucius said, it's not possible to have a C array property. Using an NSArrayis the way to go. An array only stores objects, so you'd have to use NSNumbers to store your ints. With the new literal syntax, initialising it is very easy and straight-forward:

就像 lucius 所说,不可能有 C 数组属性。使用 anNSArray是要走的路。数组仅存储对象,因此您必须使用NSNumbers 来存储整数。使用新的文字语法,初始化非常简单直接:

NSArray *doubleDigits = @[ @1, @2, @3, @4, @5, @6, @7, @8, @9, @10 ];

Or:

或者:

NSMutableArray *doubleDigits = [NSMutableArray array];

for (int n = 1; n <= 10; n++)
    [doubleDigits addObject:@(n)];

For more information: NSArray Class Reference, NSNumber Class Reference, Literal Syntax

更多信息:NSArray 类参考NSNumber 类参考文字语法

回答by Georg Sch?lly

I'm just speculating:

我只是猜测:

I think that the variable defined in the ivars allocates the space right in the object. This prevents you from creating accessors because you can't give an array by value to a function but only through a pointer. Therefore you have to use a pointer in the ivars:

我认为 ivars 中定义的变量在对象中分配了空间。这会阻止您创建访问器,因为您不能按值将数组提供给函数,而只能通过指针。因此,您必须在 ivars 中使用指针:

int *doubleDigits;

And then allocate the space for it in the init-method:

然后在 init-method 中为它分配空间:

@synthesize doubleDigits;

- (id)init {
    if (self = [super init]) {
        doubleDigits = malloc(sizeof(int) * 10);
        /*
         * This works, but is dangerous (forbidden) because bufferDoubleDigits
         * gets deleted at the end of -(id)init because it's on the stack:
         * int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
         * [self setDoubleDigits:bufferDoubleDigits];
         *
         * If you want to be on the safe side use memcpy() (needs #include <string.h>)
         * doubleDigits = malloc(sizeof(int) * 10);
         * int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
         * memcpy(doubleDigits, bufferDoubleDigits, sizeof(int) * 10);
         */
    }
    return self;
}

- (void)dealloc {
    free(doubleDigits);
    [super dealloc];
}

In this case the interface looks like this:

在这种情况下,界面如下所示:

@interface MyClass : NSObject {
    int *doubleDigits;
}
@property int *doubleDigits;

Edit:

编辑:

I'm really unsure wether it's allowed to do this, are those values really on the stack or are they stored somewhere else? They are probably stored on the stack and therefore not safe to use in this context. (See the question on initializer lists)

我真的不确定是否允许这样做,这些值真的在堆栈中还是存储在其他地方?它们可能存储在堆栈中,因此在此上下文中使用不安全。(请参阅初始化列表上问题

int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
[self setDoubleDigits:bufferDoubleDigits];

回答by Martin West

This works

这有效

@interface RGBComponents : NSObject {

    float components[8];

}

@property(readonly) float * components;


- (float *) components {
    return components;
}

回答by Vladimir Despotovic

You can put this in your .h file for your class and define it as property, in XCode 7:

您可以将它放在您的类的 .h 文件中,并将其定义为属性,在 XCode 7 中:

@property int  (*stuffILike) [10];

回答by gog

I found all the previous answers too much complicated. I had the need to store an array of some ints as a property, and found the ObjC requirement of using a NSArray an unneeded complication of my software.

我发现以前的所有答案都太复杂了。我需要将一些整数数组存储为属性,并且发现使用 NSArray 的 ObjC 要求是我的软件不必要的复杂性。

So I used this:

所以我用了这个:

typedef struct my10ints {
    int arr[10];
} my10ints;

@interface myClasss : NSObject

@property my10ints doubleDigits;

@end

This compiles cleanly using Xcode 6.2.

这可以使用 Xcode 6.2 进行干净的编译。

My intention wasto use it like this:

我的意图像这样使用它:

myClass obj;
obj.doubleDigits.arr[0] = 4;

HOWEVER, this does not work. This is what it produces:

但是,这不起作用。这是它产生的:

int i = 4;
myClass obj;
obj.doubleDigits.arr[0] = i;
i = obj.doubleDigits.arr[0];
// i is now 0 !!!

The only way to use this correctly is:

正确使用它的唯一方法是:

int i = 4;
myClass obj;
my10ints ints;
ints = obj.doubleDigits;
ints.arr[0] = i;
obj.doubleDigits = ints;
i = obj.doubleDigits.arr[0];
// i is now 4

and so, defeats completely my point (avoiding the complication of using a NSArray).

因此,完全违背了我的观点(避免使用 NSArray 的复杂性)。