3

ARC forbids Objective-C objects in structs or unions.

Unless you add __unsafe_unretained which means its not managed.

I was wonder what people are using in place of structs now if anything?

Or are you retaining everything manually?

Regexident
  • 29,441
  • 10
  • 93
  • 100
abe
  • 4,046
  • 6
  • 29
  • 33
  • please add some example code to your question – art-divin May 02 '13 at 08:10
  • possible duplicate of [typedef struct vs. Object - Benefits](http://stackoverflow.com/questions/15358771/typedef-struct-vs-object-benefits) – Sulthan May 02 '13 at 08:12
  • The struct vs object question is a abstract discussion about which to use in what scenario. This is a question about what you would use instead of stucts since arc does not allow stucts to contain Objective C objects. Do ppl recommend not use Objective c Objects and still use structs. do ppl just use objects? Or is there some other method i am not aware of. – abe May 02 '13 at 08:19

4 Answers4

5

It's very simple - if you want to add an object inside a struct, you are doing it wrong. Whenever you need a struct to hold an obj-c object, convert the struct into an obj-c object.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
4

I would manage different objects in one objc-object like this:

@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
peterflynn
  • 4,667
  • 2
  • 27
  • 40
art-divin
  • 1,635
  • 1
  • 20
  • 28
  • so it seems every one is going with an obj-c object – abe May 07 '13 at 10:25
  • you'll get used to it, don't worry :) Since it's a common practice and looks to me "naturally" – art-divin May 07 '13 at 11:44
  • 1
    its what im using but it feels wasteful some how...i know its not really that much memory for most things...it just urks me – abe May 07 '13 at 15:43
  • @art-divin : Sorry to bump an old thread. I'm currently using your suggested class model in lieu of C-structures. However, if in addition to properties, the class also contains methods, would it mean that every instance of the class, that is passed as a parameter, also contains all those methods as well? If so, is such an operation safe, and are the overheads high? One more question, please _(if you ever read this)_. The instantiated class object is actually being sent as a parameter to a C-style function that resides within that same instantiated class - _is that safe?_ Thank you. – iSofia Jun 01 '15 at 11:58
  • Just noticed that art-divin's last post was dated over a year ago. If anyone else might read this, any help would be appreciated. Thank you. – iSofia Jun 01 '15 at 12:00
  • @iSofia `However, if in addition to properties, the class also contains methods, would it mean that every instance of the class, that is passed as a parameter, also contains all those methods as well` - I am sorry but I cannot understand your question well enough. I would say that "contains" term should be avoided, instead, ObjC classes do "respond" to messages (however, class runtime table map does contain IMP of all the methods). `If so, is such an operation safe, and are the overheads high` I would say that "safe" is wrong term here, really. ObjC objects work well with ObjC objects. – art-divin Jun 04 '15 at 09:01
  • @iSofia `The instantiated class object is actually being sent as a parameter to a C-style function that resides within that same instantiated class - is that safe? ` I would say that "safe" term is not the way to go. I think that what you asked is the same as OP asks - how to store ObjC objects inside of C-structs. The answer is simple: you should not do this. This is not "safe" in terms that pointer size may be different between different architectures and, for example, object marshalling will fail miserably – art-divin Jun 04 '15 at 09:02
  • @art-divin : Thank you for your answer. So, all methods are imported in a class object; got it. And I've done away with structs, and implemented properties classes instead _(as per you suggestion)_. It's just a class of various properties that would be instantiated, have their properties set, and then the entire object passed as a parameter to some function. My new issue is that the function _(C-style function)_ that is receiving this object as its parameter also resides in the properties class itself. I heard that this is not a problem, as C-style functions are not imported? Is this correct? – iSofia Jun 05 '15 at 04:51
  • 1
    Note: you also need to include an empty `@implementation MyContainer` block somewhere in the .m file or else you will get a "symbol(s) not found" linker error. I will edit the answer above to include this. – peterflynn Jan 28 '16 at 23:23
1

Wouldn't it be a lot easier to implement a static class and fake its properties, as shown here?

Community
  • 1
  • 1
shmim
  • 729
  • 9
  • 21
0

I answered to it here https://stackoverflow.com/a/28845377/1570826

maybe somebody with the right level could mark this or the other as a duplicate.

Community
  • 1
  • 1
lu_zero
  • 978
  • 10
  • 14