0

I'm trying to write an app that has two scenes in it. The first page is a UITableView that will contain a list of note entries. The second scene has 2 text fields (note summary and note description). I'm entering details on the second scene and then clicking a "Save" button which saves the data:

NoteListViewController.m

- (IBAction)saveAndGoBack:(id)sender {
    NSLog(@"NoteDetailViewController.m: %@", NSStringFromSelector(_cmd));    

    NSString * desc = [[NSString alloc]init];
    NSString * detail = [[NSString alloc]init];
    desc = _noteTitle.text;
    detail = _noteDesc.text;

    [[NoteStore sharedStore]createNoteWithDesc:desc AndDetail:detail];
    [self.navigationController popViewControllerAnimated:YES];
}

NoteStore is a static sharedStore that I am saving the data into.

NoteStore.m

-(Notes *)createNoteWithDesc:(NSString *)desc AndDetail:(NSString *)detail {
    NSLog(@"NoteStore.m: %@", NSStringFromSelector(_cmd));
    Notes * newNote = [[Notes alloc]initNoteWithDesc:desc AndDetail:detail];
    [self.privateItems addObject:newNote];
    return newNote;
}

So, the note is added to an NSMutableArray called "privateItems". I confirmed that the Note object gets added properly.

*****The problem happens when I try to retrieve the Note object (desc and detail) from the privateItems array later on using an accessor method which has a public property in the NoteStore.h file called allItems (it's an NSArray readonly, nonatomic and a copy):

NoteStore.m

-(NSArray *)allItems{
    NSLog(@"NoteStore.m: %@", NSStringFromSelector(_cmd));
    return [self.privateItems copy];
}

Everytime I try to retrieve it, the first property (desc) comes up as nil while the second property (detail) has the data I saved in the second text field of the second scene. Why is the first field constantly coming up as nil???

Just for clarity, a Note object is declared as follows

@interface Notes : NSObject

// What are the properties of a note?
@property (nonatomic, weak) NSString * noteDesc;
@property (nonatomic, weak) NSString * noteDetail;
@property (nonatomic, weak) NSString * test;

// Designated Initializer
-(instancetype)initNoteWithDesc:(NSString *)desc AndDetail:(NSString *)detail;

@end 

Any help would be greatly appreciated.

7
  • 1
    Could it be that your Notes object has only weak references to it's properties? Maybe make them a strong reference so that they keep ownership. Commented Dec 16, 2014 at 0:16
  • 1
    where is privateItems defined then? Commented Dec 16, 2014 at 0:23
  • is privateItems declared as weak or strong property / or ivar? how are you managing privateItems accessors? Commented Dec 16, 2014 at 0:26
  • Hey Bamsworld and Dij-Djan, thanks for answering. privateItems is defined as an NSMutableArray (nonatomic) in NoteStore.m's class extension Commented Dec 16, 2014 at 0:31
  • Then, allItems is an NSArray declared in NoteStore.h. It's read-only, nonatomic and a copy. I don't want anyone to edit privateItems when I need to return what's in it, I return it by using the accessor on allItems Commented Dec 16, 2014 at 0:34

1 Answer 1

1

When you call the designated initialiser you pass in two NSString objects. At this point they are owned by the method where they are created.

When they are assigned to the properties they only have a weak reference and therefor the retain count is not bumped up. Weak references are good for things like delegate objects. In this case you want your objects to stick around, so by declaring them as strong you're saying I want these properties to stick around in memory and take ownership of them.

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

1 Comment

Thank you so much Bamsworld! I spent hours trying to figure this out and the solution was so simple. Thank you again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.