Error: Assigning array literal to a weak property


#1

so here is my code that is throwing two errors:

if (self) {
        self.questions = @[@"From what is cognac made",
                           @"What is 7 + 7?",
                           @"What is the capital of Vermont?"];
        
        self.answers = @[@"Grapes",
                         @"14",
                         @"Montpelier"];
        
}

So the self.question = … line and the self.answers = … line are highlighted yellow and are giving this error:
"Assigning array literal to a weak property; object will be released after assignment"
So when I launch my app at the end of the chapter the app does not fetch questions or answers when I press the buttons.

Any help is greatly appreciated! :smiley:


#2

How did you declare those properties, questions and answers?

Did you declare them like this, with the weak attribute?

//  main.m
@interface Foo : NSObject

@property (weak) NSArray *bars;

@end

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    return 0;
}

@implementation Foo

- (instancetype)init
{
    self = [super init];
    self.bars = @[@"From what is cognac made",
                  @"What is 7 + 7?",
                  @"What is the capital of Vermont?"];
    return self;
}

@end

If you post your code, you will make it easier for others to diagnose what the problem is.


#3

[code]#import “BNRQuizViewController.h”

@interface BNRQuizViewController ()

@property (nonatomic) int currentQuestionIndex;

@property (nonatomic, weak) NSArray *questions;
@property (nonatomic,weak) NSArray *answers;

@property (nonatomic, weak) IBOutlet UILabel *questionLabel;
@property (nonatomic, weak) IBOutlet UILabel *answerLabel;

@end

@implementation BNRQuizViewController

  • (instancetype)initWithNibName:(NSString *)nibNameOrNil
    bundle:(NSBundle *)nibBundleOrNil
    {
    // Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

      self.questions = @[@"From what is cognac made",
                         @"What is 7 + 7?",
                         @"What is the capital of Vermont?"];
      
      self.answers = @[@"Grapes",
                       @"14",
                       @"Montpelier"];
    

    }
    // Return the address of the new object
    return self;
    }

  • (IBAction)showQuestion:(id)sender
    {
    self.currentQuestionIndex++;

    if (self.currentQuestionIndex == [self.questions count]) {
    // Reset if at end of the question array
    self.currentQuestionIndex = 0;
    }

    NSString *question = self.questions[self.currentQuestionIndex];

    self.questionLabel.text = question;

    self.answerLabel.text = @"???";

}

  • (IBAction)showAnswer:(id)sender
    {
    NSString *answer = self.answers[self.currentQuestionIndex];

    self.answerLabel.text = answer;
    }

@end[/code]


#4

@property (nonatomic, copy) NSArray *questions; @property (nonatomic, copy) NSArray *answers;

These 2 properties should be copy rather than weak. The default is strong so, in effect, this is same as writing:

@property (nonatomic, copy, strong) NSArray *questions; @property (nonatomic, copy, strong) NSArray *answers;


#5

[quote=“codeartisan”]@property (nonatomic, copy) NSArray *questions; @property (nonatomic, copy) NSArray *answers;

These 2 properties should be copy rather than weak. The default is strong so, in effect, this is same as writing:

@property (nonatomic, copy, strong) NSArray *questions; @property (nonatomic, copy, strong) NSArray *answers;[/quote]

Copy and strong are mutually exclusive, so you cannot have both. Otherwise, you are correct.


#6

This is what you are looking for:

@property (nonatomic, copy) NSArray *questions; @property (nonatomic, copy) NSArray *answers;