-2

I am trying to declare a NSMutableArray to use to hold some Contact objects. I am following the instructions given in this question, how and where do I initialize an global NSMutableArray in Xcode 5. But have left off the @synthesize because according to this wikipedia page it says that it is implicit in newer versions of xcode. I have also tried including it and get the same errors so I don't think it is the problem but I just thought I would mention it.

I am getting these errorserrors but can't figure out what exactly they mean because according to these questions: Type of class in a NSMutableArray Can I restrict the NSMutableArray object type? Is there any way to enforce typing on NSArray, NSMutableArray, etc.?

NSMutableArrays do not enforce the types of objects that are placed into them.

What am I doing wrong and what do these errors mean? This is my first Objective C project and I believe this is a simple problem but nevertheless it is one that I have not been able to figure out and has been frustrating me for hours.

Here is my header file:

#import <Foundation/Foundation.h>
#import "Contact.h"

@interface ContactList : NSObject

@property (strong, readwrite, nonatomic) NSMutableArray *contactList;

+ (BOOL) addContact: (Contact *) contact;
- (BOOL) emptyContactList;
//- (BOOL) buildContactList

@end

and here is my implementation file:

#import "ContactList.h"

@implementation ContactList

contactList = [[NSMutableArray alloc] init];

+ (BOOL) addContact: (Contact *) contact
{
    return YES;
}
- (BOOL) emptyContactList
{
    return YES;
}

@end
Community
  • 1
  • 1
user3282276
  • 3,674
  • 8
  • 32
  • 48

4 Answers4

1

When referecing your property anyway I believe self needs to be in front, as in self.contactList

However to initialize you should do lazy instantiation by overwriting the "getter" for your property in your implementation file.

I believe it will be

// The getter method for property "contactList"
-(NSMutableArray *)contactList {
    // If no array exists, create one.
    if (!_contactList) {
        _contactList = [[NSMutableArray alloc]init]
    }
return _contactList;
}
Alex Turner
  • 247
  • 1
  • 2
  • 12
0

To add onto the already correct answers, if the array needs to exist as part of the initialization of the class, you can include it's setup in an init method as:

-(instancetype) init{
     self = [super init];
     if(self){
         _contactList = [NSMutableArray alloc] init];
     }
     return self;
 }

Just be aware that you can't set up instance variables in a class method. Meaning, you would only be able to call your +addContact: method after you created an instance of your ContactList class. Otherwise the array would always be nil, regardlesss of how many Contacts you added

This resource may provide you with some additional insight on class constructors

Louis Tur
  • 1,303
  • 10
  • 16
-1

Ok. It looks like you have a class ContactList that contains a property contactList, where contactList is a mutable array.

I never liked using the same words with different case to mean different things, so I would change the name of the property.

Using a class name with a capital letter (e.g. ContactList) and an instance variable in lower case (contactList) is something I've seen a fair amount. (But I don't like that either. It leads to confusion later.)

That's just my opinion though.

You have a line of executable code in your @implementation... @end block that is not inside any method or function:

contactList = [[NSMutableArray alloc] init];

You can't do that. All executable code must be inside a method or function somewhere. That's what the compiler is telling you.

You should probably define a custom init method for your class, as described by @LouisTur in his answer.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-1

It looks like this file was trying to declare a global variable named "contactList" and set it.

However, when declared outside a method or @interface, a variable must by typed, much like a C global. If you want to create such a global, try the following. (Note that this will usually cause a memory leak message at runtime in the console due to alloc/init being called before Objective-C is fully set up.)

static NSMutableArray* contactList = [[NSMutableArray alloc]init];

To use a more Objective-C solution, declare the static and set it to nil, (gives no message) then set it in the +initialize method. (Google for +initialize to see many good explanations of how it is guaranteed by Objective-C to be called only once.)

static NSMutableArray* contactList = nil;

+ (void) initialize {
    contactList = [[NSMutableArray alloc]init];
}

(Naming conventions for static global variables I'll leave for another conversation.)

Walt Sellers
  • 3,806
  • 30
  • 35
  • Fails if there is ever more than one ContactList object. – gnasher729 Dec 31 '14 at 10:28
  • @gnasher729 Yes, it would fail if ContactList was a normal instance object, but the question shows a global variable and a class method to add contacts, so it is easy to conclude the asker is using a singleton pattern. So, there is never more than one ContactList object. – Walt Sellers Dec 31 '14 at 17:06