での使用方法を理解しようとする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
そして2つ目は本
これがインターフェースです
#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: 2 番目の本を追加してから印刷すると、表示されるすべての本が同じ本である理由
たとえば、この入力の出力は次のようになります
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 メソッドが機能しない理由がわかりません。NSDictionary
3:著者や idNumber のようなキー (私の例では、キーはタイトルです) を使用せずに本を見つけることができるかどうかを知りたいですか?