0

List is defined:

@property (nonatomic, strong) NSArray *list;

@synthesize list = _list;

What is the difference between:

list = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

and

self.list = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

The first returns all the records in Core Data but the second returns nothing.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • 1
    Where is `list` definied? – iCode Oct 05 '13 at 08:35
  • I have edited the question to show how list is defined. – RGriffiths Oct 05 '13 at 08:45
  • 1
    You have a property `self.list`, which is synthesized as instance variable `_list`. But where does the `list` variable come from? - And the RHS of both assignments is identical. It cannot be that they return different results. – Martin R Oct 05 '13 at 08:58

1 Answers1

1

I think you MUST read the App doc about Encapsulating Data.

In particular, with the first code snippet you are saying to wrap an instance variable called _list through accessor methods.

In general in OOP they are also called setters and getters. A good discussion of pros of them can be found in Why use getters and setters?.

So, comments of other people are right. Where does the list variable come from?

An important thing you need to understand is that the dot syntax is a a concise way to access method calls. So, for example:

NSString *nickname = person.nickname;
person.nickname = @"This is my nickname";

is equal to

NSString *nickname = [person nickname];
[person setNickname:@"This is my nickname"];

A note. Starting form XCode 4.4, the new Apple LLVM compiler 4.0 allows you to skip the @synthesize directive. Under the hood the compiler generates an instance variable with a _ suffix. Further references at Automatic Property Synthesis With Xcode 4.4.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • I really appreciate the time that you have spent sending me this. I am just learning objective-c and working in a remote area in Ethiopia resources/internet is very difficult to access. Many thanks - I will look into this. – RGriffiths Oct 06 '13 at 08:50