如何使用 NSDictionary [英] how to use NSDictionary

查看:48
本文介绍了如何使用 NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在 NSDictionary 中使用,所以我创建了 2 个文件第一个是bookStore

i try to understand how to use in NSDictionary, so i create 2 files the first one is 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:为什么当我添加第二本书然后我打印我看到的所有书都是同一本书

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:我不明白为什么 findBook 方法不起作用?3:我想知道我是否可以在NSDictionary 中找到书籍而无需使用密钥(在我的示例中,密钥是标题)如作者或 idNumber?

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. 在添加书籍对象时,请执行以下操作:

  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];

  • 更改您的 findBook: 方法如下:

    -(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);
        }
    }
    

    }

    这篇关于如何使用 NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆