I have started a Master Detail application and left the generated code untouched. I created and added two additional classes: a book class(contains an NSString for a title, author, and summary) and also a data controller class(contains a mutable array to store the books).
My understanding of @property attributes after reading Apple doc and others is this:
- strong - default, creates ownership of an object
- weak - alternative to strong, used to avoid retain cycles
- copy - creates a copy of the existing object and takes ownership of that
- nonatomic - disregards any sort of thread safety
This code throws a segmentation fault in addBookToList when the @property AJKBook is declared with the copy attribute and I don't understand why.
@interface AJKBookDataController ()
// when current book uses the copy attribute code seg faults in addBookToList
@property (nonatomic) AJKBook *currentBook;
@property (nonatomic, copy) NSString *currentValue;
- (void)populateBookList;
- (void)addBookToBookList;
@end
@implementation AJKBookDataController
- (id)init
{
self = [super init];
if (self) {
_bookList = [[NSMutableArray alloc] init];
_currentBook = [[AJKBook alloc] init];
_currentValue = [[NSString alloc] init];
[self populateBookList];
return self;
}
return nil;
}
- (void)setBookList:(NSMutableArray *)bookList
{
// this bit of code ensures bookList stays mutable
if (_bookList != bookList) {
_bookList = [bookList mutableCopy];
}
}
- (void)populateBookList
{
NSURL *url = [NSURL URLWithString:@"https://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
NSLog(@"%@", [self.bookList description]);
}
- (void)addBookToBookList
{
[self.bookList addObject:self.currentBook];
self.currentBook = [[AJKBook alloc] init];
}
...
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"title"]) {
// [self.currentBook title:self.currentValue];
self.currentBook.title = self.currentValue;
} else if ([elementName isEqualToString:@"author"]) {
self.currentBook.author = self.currentValue;
} else if ([elementName isEqualToString:@"summary"]) {
self.currentBook.summary = self.currentValue;
} else if ([elementName isEqualToString:@"Book"]) {
[self addBookToBookList];
}
self.currentValue = [NSString stringWithFormat:@""];
}
@end