how to use NSDictionary

六月ゝ 毕业季﹏ 提交于 2019-12-23 18:22:45

问题


i try to understand how to use in NSDictionary, so i create 2 files the first one is 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 book

this is the 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 the second book and then i print all the book i see is the same book

for exemple 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 dont understand why the findBook method doesn't worked? 3: i want to know if i can find books in the NSDictionary without use the key, ( in my exemple the key is the title) like the author or the idNumber?


回答1:


Inside findBook().. you are doing a string match so use

if ([[myBookStore objectForKey:key] isEqualToString:whatLook] ){

instead of

 if ([myBookStore objectForKey:key]==whatLook ){



回答2:


  1. While adding book objects, do as following:

    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];
    
  2. Change your findBook: method 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);
        }
    }
    

    }



来源:https://stackoverflow.com/questions/10088872/how-to-use-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!