Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C array of structs

I want to have a struct like the following:

struct foo {
NSString Title;
NSInteger numberOfBooks;
vector of NSStrings Books;
};

foo bar[50];

Now first, I'd like to know how to create an array of strings (Each row containing a book title).

Then I'd like to know is the code I've written correct.

And finally, can I access said element of the struct with bar[n].Title?


I've followed what you commented, and here's how it looks:

@interface Foo : NSObject {
NSString *name;
NSArray *books;
}

@end

A->name = @"Clancy, Thomas";
A->books = [[NSArray alloc] initWithObjects:@"Book 1"
            @"Book2",nil];
self.authors = [[NSArray alloc] initWithObjects:A, nil];

Now it gives me that instance variable 'name' is protected. I tried writing public: when defining Foo, but it doesn't accept that. (Yes, I'm new to Obj-C).

like image 688
Lord Zsolt Avatar asked Jun 26 '13 06:06

Lord Zsolt


1 Answers

Unfortunately, Objective C with Automatic Reference Counting (ARC) enabled does not allow for objects inside C structs. Objc also only allows objects inside NSArrays (the loose equivalent of vectors).

The first step of the solution is to create a class instead of a struct:

// Foo.h
#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *books;
// no need to keep the number of items in an NSArray as a separate variable,
// NSArray does that for you.

- (id)initWithTitle:(NSString *)title books:(NSArray *)books;

@end

// Foo.m
#import "Foo.h"

@implementation Foo
@synthesize title;
@synthesize books;

- (id)initWithTitle:(NSString *)title books:(NSArray *)books
{
  if (self = [super init]) {
    self.title = title;
    self.books = books;
  }
  return self;
}

@end

Note that all object types are pointers. This is because all object in objc are heap allocated, so you can't object types that aren't pointers. Also note that the type NSArray isn't parameterized. That's because objc doesn't have a concept of templates of generics, so the contents of the array could be any object. This usually isn't a problem, but occasionally a type error won't be caught at compile time.

To keep an array of these Foo objects is simple. You could just make a C array of them like Foo *bar[50];, but I don't think that works with ARC, and it definitely isn't the right way to do it in objective c. The right way is just to use another NSArray: NSArray *bar = [[NSArray alloc] init];, or with objc2.0 syntax: NSArray *bar = @[];

Some notes that might help you

@property and @synthesize are shortcuts for making an instance variable and accessor methods for if. You need accessor methods if you want to access an instance variable from outside the class (or rather, the implementation file). The strong flag tells the reference counter that this is a strong reference, like shared_ptr from the c++ boost libraries. I have no idea what nonatomic does, but I'm told you need it.

The method initWithTitle:books: is like a constructor, except that objc doesn't have a notion of constructors. The implementation calls the superclass's constructor with self = [super init] (pretend that self is a c++ reference to the object). Constructors can return nil, so that's why you need the if block.

The line self.title = title; is technically shorthand for [self setTitle:title];, which uses a method autogenerated by @property and @synthesize.

like image 87
adrusi Avatar answered Sep 28 '22 05:09

adrusi