Objective-C 是否禁止使用结构体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28471855/
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:32:18  来源:igfitidea点击:

Does Objective-C forbid use of structs?

objective-cstructautomatic-ref-counting

提问by user2490003

I'm new to Objective C

我是目标 C 的新手

I tried using a simple structand got

我尝试使用一个简单的struct并得到

arc forbids objective-c objects in struct

Looking up ARC, it looks like this is the specification that defines Objective C syntaxt - is that correct?

查找 ARC,看起来这是定义 Objective C 语法的规范 - 对吗?

Secondly, how do I go about using structif it's not allowed?

其次,struct如果不允许,我该如何使用?

Thank you!

谢谢!

Edit: Some code as a sample

编辑:一些代码作为示例

@implementation Cities {
    // The goal is to have a struct that holds information about a city,
    // like when a person started and ended living there.
    // I was trying to make this struct an instance variable of the Cities
    // class
    // XCode doesn't like the below struct definition

    struct City
    {
        NSString *name;
        int *_startYear;
        int *_endYear;
    };
}

回答by dandan78

arc forbids objective-c objectsin struct

arc 禁止结构中的objective-c 对象

Structs are a C construct. The compiler is telling you, in very unabiguous terms, that you can't have Objective-C objects inside a struct, not that structs are illegal.

结构是一个 C 结构。编译器非常明确地告诉您,结构中不能有 Objective-C 对象,而不是结构是非法的。

You can use regular C structs all you want.

您可以随心所欲地使用常规的 C 结构。

Your example tries to put references to an Objective-C object, NSString, into a struct, which is incompatible with ARC.

您的示例尝试将对 Objective-C 对象 , 的引用NSString放入struct与 ARC 不兼容的 。

Structs are typically used for simple data structures. Examples that you are likely to come across in Objective-C code are CGPointand CGRect.

结构通常用于简单的数据结构。您可能在 Objective-C 代码中遇到的示例是CGPointCGRect

CGPointlooks something like this

CGPoint看起来像这样

struct CGPoint 
{ 
   CGFloat x; 
   CGFloat y; 
};

A CGFloatis, I think, just a double, and the idea it to represent a point in 2D space. Structs can include pointers to other structs, C-arrays and standard C data types such as int, char, float... And Objective-C classes can contain structs, but the reverse does not work.

ACGFloat是,我认为,只是一个double,它代表二维空间中的一个点的想法。结构体可以包含指向其他结构体、C 数组和标准 C 数据类型的指针,例如int, char, float... 并且 Objective-C 类可以包含结构体,但反过来不起作用。

Structs can also get pretty complicated, but that is a very broad topic that is best researched using Google.

结构也可能变得非常复杂,但这是一个非常广泛的主题,最好使用 Google 进行研究。

回答by Kazuki Sakamoto

You can use structin Objective-C++with ARC in any case.

在任何情况下,您都可以struct在带有 ARC 的Objective-C++ 中使用。

#import <Foundation/Foundation.h>

@interface City : NSObject
struct Data {
    NSString *name;
};

@property struct Data data;
@end

@implementation City
@end

int main()
{
    City *city = [[City alloc] init];
    city.data = (struct Data){@"San Francisco"};
    NSLog(@"%@", city.data.name);
    return 0;
}

If you compile it as Objective-C, you failed as you said.

如果你把它编译成Objective-C,你说的就失败了。

$ clang -x objective-c -fobjc-arc a.m -framework Foundation 
a.m:5:15: error: ARC forbids Objective-C objects in struct
    NSString *name;
              ^
1 error generated.

Because C struct doesn't have the capability of management for variable life span.

因为C struct 没有管理可变生命周期的能力。

But in C++, struct does have destructor function. So C++ struct is compatible with ARC.

但是在C++中,struct确实有析构函数。所以 C++ struct 与 ARC 兼容。

$ clang++ -x objective-c++ -fobjc-arc a.m -framework Foundation
$ ./a.out
San Francisco

回答by Soumen

If you want to use struct in Objective C ( with ARC) use "__unsafe_unretained" attribute.

如果您想在 Objective C(带有 ARC)中使用 struct,请使用“__unsafe_unretained”属性。

struct Address {
   __unsafe_unretained NSString *city;
   __unsafe_unretained NSString *state;
   __unsafe_unretained NSString *locality;
};