我对类和对象都很陌生,我有一个问题:
我要做的是在NSMutableArray
中收集书籍的所有对象:Collection。
(目前它只有一本书(objectAtIndex:0
)
目前正在工作,但当我试图将它们吐出来时,我只得到了这本书的描述。我很想得到所有的项目(标题,作者,描述)。
我一直在想的是:我应该创建一个名为BookCollection的新(集合)类并在那里创建一个数组吗?但我该怎么做呢?
代码如下,欢迎提供帮助和提示! (约一个月前开始)
Book.h
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property(nonatomic,strong)NSString* title;
@property(nonatomic,strong)NSString* author;
@property(nonatomic,strong)NSString* description;
-(id)initWithTitle:(NSString*)newTitle withAuthor:(NSString*)newAuthor andDescription:(NSString*)newDesription;
Book.m
#import "Book.h"
@implementation Book
@synthesize title,author,description;
-(id)initWithTitle:(NSString*)newTitle withAuthor:(NSString*)newAuthor andDescription:(NSString*)newDesription{
self = [super init];
if (self) {
title = newTitle;
author = newAuthor;
description = newDesription;
}
return self;
}
@end
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize lblTitle,lblAuthor,lblDescription;
@synthesize collection;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)buttonClick:(id)sender {
//alloc the array that will hold the books
collection = [[NSMutableArray alloc]init];
//create a new book
Book *newBook = [[Book alloc]initWithTitle:[lblTitle stringValue] withAuthor:[lblAuthor stringValue] andDescription:[lblDescription stringValue]];
//logging the items of the book
NSLog(@"%@",newBook.description);
NSLog(@"%@",newBook.title);
NSLog(@"%@",newBook.author);
//adding the book to the collection
[collection addObject:newBook];
//logging the book items from the collection
NSLog(@"%@",[collection objectAtIndex:0]);
//problem... only logs 1 item from the object...
}
@end
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "Book.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property(nonatomic,strong)NSMutableArray *collection;
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *lblTitle;
@property (weak) IBOutlet NSTextField *lblAuthor;
@property (weak) IBOutlet NSTextField *lblDescription;
- (IBAction)buttonClick:(id)sender;
@end
答案 0 :(得分:0)
您需要定义Book类的-description
方法。
当您致电NSLog(@"%@", someObject)
时,会调用对象的-description
方法并将其放在%@
格式说明符中。您需要覆盖Book类的-description
方法以打印出所有对象的字段。
看看here是一个很好的例子。
澄清一下,当你打电话:
NSLog(@"%@",newBook.description);
NSLog(@"%@",newBook.title);
NSLog(@"%@",newBook.author);
您(非常正确)记录每个字段。但是,当您致电:
NSLog(@"%@",[collection objectAtIndex:0]);
你基本上是在写:
NSLog(@"%@",newBook); // Gets an NSString from [newBook description];
因此,您需要为- (NSString *)desctiprion
类实现Book
以获取您所追踪的日志记录行为。
答案 1 :(得分:0)
Step 1
:从collection = [[NSMutableArray alloc]init];
移除- (IBAction)buttonClick:(id)sender
并将其放入applicationDidFinishLaunching
方法。问题是每次将New Book添加到Collection Array时,都在初始化数组。
To Iterate
:Array的所有对象都使用以下代码段
[collection enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
Book *objBook = (Book *)obj;
NSLog(@"%@",objBook.description);
NSLog(@"%@",objBook.title);
NSLog(@"%@",objBook.author);;
}];
答案 2 :(得分:0)
这种描述方法太棒了!我不知道甚至可能是@PLPiper!谢谢
虽然它变得越来越难......在这个时刻我只是记录下来(对我来说是开发人员)。但是例如......如果我想要标签中的所有这些属性(字符串值)。所以,如果我想通过我的阵列将它们全部吐出来,那该怎么办?
我见过的代码使用了我喜欢的以下示例(类型),并且它很容易阅读。
for (int i = 0; (i<=[collection.count]); i++) {
[titleLabel setStringValue:[dateCollection objectAtIndex:i].title]
[authorLabel setStringValue:[dateCollection objectAtIndex:i].author]
[descriptionLabel setStringValue:[dateCollection objectAtIndex:i].description]
}
理论上这应该有用,但实际上我在这里遗漏了一些东西......