2013-05-02 27 views
1

ARC禁止Objective-Cstruct s或union s中的對象s。使用ARC時用於Objective-C對象而不是結構的是什麼?

除非你添加__unsafe_unretained這意味着它沒有管理。

我想知道現在人們用什麼來代替struct

或者您是否手動保留所有內容?

+0

請添加一些示例代碼,你的問題 – 2013-05-02 08:10:59

+0

的可能重複[typedef結構主場迎戰對象 - 效益(http://stackoverflow.com/questions/15358771/typedef-struct-vs-object-benefits) – Sulthan 2013-05-02 08:12:02

+0

的結構vs對象問題是關於在哪種情況下使用的抽象討論。這是一個關於你將使用什麼來代替stu的問題,因爲arc不允許工程包含Objective C對象。 PPl建議不要使用Objective C Objects並仍然使用結構。做ppl只是使用對象?或者還有其他一些我不知道的方法。 – abe 2013-05-02 08:19:49

回答

4

我管理一個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 
+0

所以它似乎每個人都與一個obj-c對象 – abe 2013-05-07 10:25:13

+0

你會習慣它,不要擔心:)因爲這是一種常見的做法,並期待對我來說「自然」 – 2013-05-07 11:44:12

+0

其即時使用,但它感覺很浪費一些如何...我知道它並不是真的那麼多的內存,對於大多數事情......它只是呃我 – abe 2013-05-07 15:43:58

5

這很簡單 - 如果你想添加一個對象結構內,你做錯了。無論何時您需要一個結構來存放一個obj-c對象,都可以將該結構轉換爲一個obj-c對象。

+0

旁邊,當你正在包裝一個objC庫到C ... – 2015-03-04 01:03:41

1

實現一個靜態類並僞造它的屬性會不會容易很多,如here

相關問題