How to use NSDictionary
I am trying to figure out how to use in NSDictionary
, so I create 2 files first - bookStore
this is the interface:
#import <Foundation/Foundation.h>
#import "Book.h"
@interface Bookstore : NSObject {
NSDictionary* myBookStore;
}
@property(retain) NSDictionary* myBookStore;
-(id)init;
-(void)printInvetory;
-(BOOL)addBook:(Book*)newBook;
-(BOOL)removeBookWithTitle:(NSString*)whichTitle;
-(BOOL) findBook:(NSString*)whatLook;
-(void) dealloc;
@end
this is the implementation
#import "Bookstore.h"
#import "Book.h"
@implementation Bookstore
@synthesize myBookStore;
-(id)init {
self=[super init];
if (self!=nil) {
myBookStore=[[NSMutableDictionary alloc] init];
}
return self;
}
-(BOOL)addBook:(Book*)newBook {
[myBookStore setObject:newBook forKey:newBook.title];
return YES;
}
-(BOOL) removeBookWithTitle:(NSString *)whichTitle {
// if ([myBookStore ObjectForKey:whichTitle]!=nil) {
[myBookStore removeObjectForKey:whichTitle];
return YES;
// }
return NO;
}
-(BOOL) findBook:(NSString *)whatLook {
Book *book;
for (NSString* key in myBookStore) {
if ([myBookStore objectForKey:key]==whatLook ){
book=[myBookStore objectForKey:key];
NSLog(@" Title: %@ ",book.title);
NSLog(@" Author: %@ ",book.author);
NSLog(@"Description: %@ ",book.description);
NSLog(@"Id number: %d " , book.idNumber);
}
}
}
-(void) printInvetory {
Book *book;
for (NSString* key in myBookStore) {
book= [ myBookStore objectForKey:key];
NSLog(@" Title: %@ ",book.title);
NSLog(@" Author: %@ ",book.author);
NSLog(@"Description: %@ ",book.description);
NSLog(@"Id number: %d " , book.idNumber);
}
}
-(void) dealloc {
self.myBookStore = nil;
[super dealloc];
}
@end
and the second is a book
this is an interface
#import <Foundation/Foundation.h>
@interface Book : NSObject {
NSString *title;
NSString *author;
NSString *description;
int idNumber;
}
@property (nonatomic,retain) NSString* title;
@property (nonatomic,retain) NSString* author;
@property (nonatomic,retain) NSString* description;
@property (nonatomic) int idNumber;
-(id) initWithTitle:(NSString*)newTitle author:(NSString*)newAuthor description:(NSString*)newDescription idNumber:(int) newIdNumber;
@end
and this is the implementation
#import "Book.h"
@implementation Book
@synthesize title,author,description,idNumber;
-(void) initWithTitle:(NSString*)newTitle author:(NSString*)newAuthor description:(NSString*)newDescription idNumber:(int)newIdNumber {
title=newTitle;
author=newAuthor;
description=newDescription;
idNumber=newIdNumber;
}
@end
and this is the main file
#import <Foundation/Foundation.h>
#import "Book.h"
#import "Bookstore.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString* find=@"gingi";
// insert code here...
Bookstore* theBookNook = [[Bookstore alloc] init];
NSString* newTitle = @"A Farwell To Arms";
Book* newBook = [[Book alloc] initWithTitle:newTitle author:@"Ernest Hemingway" description:@"The story of an " "affair between an English nurse and an American soldier on the Italian front during World War I." idNumber:3];
[theBookNook addBook:newBook];
newTitle =@"gingi";
[newBook initWithTitle:newTitle author:@"bla bla" description:@"bla bla bla" idNumber:98];
[newBook release];
[theBookNook addBook:newBook];
[theBookNook findBook:find];
[theBookNook release];
}
return 0;
}
I have 3 questions:
1: why when I add a second book and then I print the whole book I see it is the same book.
for example in this input the output will be
2012-04-10 15:26:45.476 MyBookstore[43574:403] Title: gingi
2012-04-10 15:26:45.478 MyBookstore[43574:403] Author: bla bla
2012-04-10 15:26:45.478 MyBookstore[43574:403] Description: bla bla bla
2012-04-10 15:26:45.479 MyBookstore[43574:403] Id number: 98
2012-04-10 15:26:45.479 MyBookstore[43574:403] Title: gingi
2012-04-10 15:26:45.480 MyBookstore[43574:403] Author: bla bla
2012-04-10 15:26:45.480 MyBookstore[43574:403] Description: bla bla bla
2012-04-10 15:26:45.481 MyBookstore[43574:403] Id number: 98
2: I don't understand why the findBook method is not working? 3: I want to know if I can find books in NSDictionary
without using a key (in my example it is a key), like author or idNumber?
source to share
-
When adding book objects, follow these steps:
Book* newBook = [[Book alloc] initWithTitle:newTitle author:@"Ernest Hemingway" description:@"The story of an " "affair between an English nurse and an American soldier on the Italian front during World War I." idNumber:3]; [theBookNook addBook:newBook]; [newBook release]; newTitle =@"gingi"; newBook = [[Book alloc] initWithTitle:newTitle author:@"bla bla" description:@"bla bla bla" idNumber:98]; [theBookNook addBook:newBook]; [newBook release];
-
Change the method
findBook:
as follows:-(BOOL)findBook:(NSString *)whatLook{
Book *book; for (NSString* key in [myBookStore allKeys]) { if ([[myBookStore objectForKey:key] isEqualToString:whatLook] ) { book=[myBookStore objectForKey:key]; NSLog(@" Title: %@ ",book.title); NSLog(@" Author: %@ ",book.author); NSLog(@"Description: %@ ",book.description); NSLog(@"Id number: %d " , book.idNumber); } }
}
source to share