【问题标题】:Spitting out objects from array in Objective-C在Objective-C中从数组中吐出对象
【发布时间】:2013-09-28 05:41:45
【问题描述】:

我对类和对象很陌生,我有一个问题:

  • 我正在跟踪可以通过 textFields 输入的书籍。
  • 每本书 3 个属性:标题、作者和说明。

我正在尝试做的是在 NSMutableArray 中获取书籍的所有对象,称为:集合。 (目前只有 1 本书 (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

【问题讨论】:

  • 它非常简单 @Ben Vertonghen 只需从您的 Click 方法中删除 collection = [[NSMutableArray alloc]init]; 行就可以了。在applicationDidFinishLaunching 方法中初始化您的数组。进行此更改,您就完成了。
  • 您不应该使用“描述”作为属性名称,因为至少会与在 NSObject 中定义并在许多方法中被覆盖的标准 description 方法混淆。您希望 description 以“正常”方式工作,并转储对象的表示。
  • 请记住,@HotLicks!谢谢!

标签: objective-c class object nsmutablearray


【解决方案1】:

您需要定义 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];

因此您需要为 Book 类实现 - (NSString *)desctiprion 以获得您所追求的日志记录行为。

【讨论】:

    【解决方案2】:

    Step 1 :从- (IBAction)buttonClick:(id)sender 中删除collection = [[NSMutableArray alloc]init]; 并将其放入applicationDidFinishLaunching 方法中。问题是每次将新书添加到集合数组时,您都在初始化数组。

    To Iterate : Array 的所有对象都使用以下 sn-p

    [collection enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
        Book *objBook = (Book *)obj;
    
        NSLog(@"%@",objBook.description);
        NSLog(@"%@",objBook.title);
        NSLog(@"%@",objBook.author);;
    }];
    

    【讨论】:

      【解决方案3】:

      这个描述方法太棒了!我不知道这甚至有可能@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]
      
      }
      

      理论上这应该可行,但实际上我在这里遗漏了一些东西......

      【讨论】:

      • description 属性重命名为其他名称,然后编写一个新的description 方法,该方法返回一个包含所有字段的格式化字符串。
      • 一种懒惰的方式来做描述方法:return @{@"title"=self.title, @"author"=self.author, @"Description"=self.Description}.description;。 (我认为我的语法是正确的。)
      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2012-08-27
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多