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?

3
  • please add some example code to your question Commented May 2, 2013 at 8:10
  • possible duplicate of typedef struct vs. Object - Benefits Commented May 2, 2013 at 8: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. Commented May 2, 2013 at 8:19

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

9 Comments

so it seems every one is going with an obj-c object
you'll get used to it, don't worry :) Since it's a common practice and looks to me "naturally"
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
@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.
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.
|
1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.