使用ARC时,Objective-C对象使用什么而不是结构?

ARC禁止结构或联合中的Objective-C对象.

除非你添加__unsafe_unretained,这意味着它没有被管理.

我想知道人们现在用什么代替结构?

或者你手动保留一切?

最佳答案
我会在一个objc对象中管理不同的对象,如下所示:

@class MyFirst, MySecond;

@interface MyContainer : NSObject

@property (nonatomic, strong, readonly) MyFirst *firstInst;
@property (nonatomic, strong, readonly) MySecond *secondInst;

// optional: convenience initializer
+ (instancetype)containerWithFirstInst:(MyFirst *)firstInst secondInst:(MySecond *)secondInst;

@end

// required by linker: stub definition for the class declared above
@implementation MyContainer
@end


@interface SomeController : NSObject

- (void)doSomething;

@end

@implementation SomeController

- (void)doSomething {
    MyFirst *firstInstance = [[MyFirst alloc] initWithSomeParameters:...];
    MySecond *secondInstance = [[MySecond alloc] initWithSomeParameters:...];
    MyContainer *container = [MyContainer containerWithFirstInst:firstInstance secondInst:secondInstance];
    // use container as a struct (but it's definitely an object that is managed by ARC)
}

@end

转载注明原文:使用ARC时,Objective-C对象使用什么而不是结构? - 代码日志