【问题标题】:Cannot create NSArray from object无法从对象创建 NSArray
【发布时间】:2011-11-13 04:39:53
【问题描述】:

XCode 错误:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 2530 thread 0x0]
2011-11-12 18:29:29.857 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x100539a00> of class Document
2011-11-12 18:29:29.879 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x10015dda0> of class Document
2011-11-12 18:30:10.362 RaiseMan[2530:707] Cannot create NSArray from object <Document: 0x1001b0a90> of class Document

person.h:

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *personName;
    float expectedRaise;
}

@property (readwrite, copy) NSString *personName;
@property (readwrite) float expectedRaise;

@end

person.m:

#import "Person.h"

@implementation Person

- (id) init
{
    self = [super init];
    expectedRaise = 5.0;
    personName = @"New Person";
    return self;    
}

- (void)dealloc
{
    [personName release];
    [super dealloc];
}

@synthesize personName;
@synthesize expectedRaise;

@end

文档.h:

#import <Cocoa/Cocoa.h>

@interface Document : NSDocument
{
    NSMutableArray *employees;
}

- (void)setEmployees:(NSMutableArray *)a;

@end

文档.m

#import "Document.h"

@implementation Document

- (id)init
{
    self = [super init];
    //if (self) {
        employees = [[NSMutableArray alloc] init];
    //}
    return self;
}

- (void)dealloc
{
    [self setEmployees:nil];
    [super dealloc];
}


-(void)setEmployees:(NSMutableArray *)a
{
    //this is an unusual setter method we are goign to ad a lot of smarts in the next chapter
    if (a == employees)
        return;
    [a retain];
    [employees release];
    employees = a;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    /*
     Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    /*
    Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    */
    NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
}

+ (BOOL)autosavesInPlace
{
    return YES;
}

@end

完整项目代码:http://sharesend.com/zyca7

【问题讨论】:

  • 有什么原因你没有在错误消息中包含模块(或相关行),而不是期望人们下载 zip 文件?
  • 不确定您的意思?我在顶部包含了完整的错误,在帖子中包含了完整的代码?我包括了完整的项目,因为有很多绑定等,因为这是我正在学习可可的 tut 我不知道我可能做错了什么。

标签: cocoa xcode4.2


【解决方案1】:

我今天遇到了同样的问题,我已经解决了。 您应该将列而不是单元格绑定到数组控制器。

【讨论】:

    【解决方案2】:

    如果不从 ShareSend 下载 zip 文件(请使用更好的主机,例如 Dropbox,或者更好的是,某种形式的版本控制),我只能猜测您已将数组控制器的 contentArray 绑定到您的文档,但是未能指定该文档的数组属性。

    告诉数组控制器使用文档本身作为其内容数组是行不通的,因为文档不是数组。进入您的 nib,选择数组控制器,编辑 contentArray 绑定,并指定要绑定到的数组属性的模型键路径。

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 2012-10-21
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多