我试图了解如何使用 in NSDictionary
,所以我创建了 2 个文件,第一个是 bookStore
这是界面:
#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
这是实现
#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
第二个是书
这是界面
#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
这就是实现
#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
这是主文件
#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;
}
我有3个问题:
1:为什么当我添加第二本书然后我打印所有我看到的书是同一本书
例如,在这个输入中,输出将是
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:我不明白为什么 findBook 方法不起作用?3:我想知道我是否可以在NSDictionary
不使用密钥的情况下找到书籍,(在我的例子中,密钥是标题)比如作者或idNumber?