0

可能重复:
NSMutableArray 在 iPhone 应用程序中的 NSLog 时未显示实际值

我将 NSMutableArray 初始化如下:

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

return self;
}

数组存储 NSTable 的数据如下:

//-----------------------------------------
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [mySpotsArray count];

[self saveMySpots];
}


//-----------------------------------------
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn            *)tableColumn row:(NSInteger)row
{
Spot *sp = [mySpotsArray objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [sp valueForKey:identifier];

[self saveMySpots];
}



 //-----------------------------------------
 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Spot *sp = [mySpotsArray objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[sp setValue:object forKey:identifier];

[self saveMySpots];
}

我正在添加和删除这样的对象:

//-----------------------------------------
- (IBAction)addSpot:(id)sender
{
[mySpotsArray addObject:[[Spot alloc]init]];
[mySpotsTable reloadData];

[self saveMySpots];
}




//-----------------------------------------
- (IBAction)deleteSpot:(id)sender
{
NSInteger row = [mySpotsTable selectedRow];
[mySpotsTable abortEditing];
if (row !=-1)
{
    [mySpotsArray removeObjectAtIndex: row];
}

[mySpotsTable reloadData];

[self saveMySpots];
}

我正在保存和加载这样的数组内容:

//-----------------------------------------
- (void) saveMySpots
{
savedSpots = [NSUserDefaults standardUserDefaults];
encodedMySpotsArrayObject = [NSKeyedArchiver archivedDataWithRootObject: mySpotsArray];
[savedSpots setObject: encodedMySpotsArrayObject forKey:[NSString stringWithFormat:@"MySpotsArrayKey"]];
}




//-----------------------------------------
- (void) loadMySpots
{
savedSpots = [NSUserDefaults standardUserDefaults];
decodedMySpotsArrayObject = [savedSpots objectForKey: [NSString stringWithFormat:@"MySpotsArrayKey"]];

 mySpotsArray = [NSKeyedUnarchiver unarchiveObjectWithData: decodedMySpotsArrayObject];
}

对象的编码和解码如下:

//-----------------------------------------
- (id)init
{
self = [super init];
if (self)
{
    _mySpot = @"My Spot";
    _localOffset = 0;
}

return self;
}


//-----------------------------------------
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject: self.mySpot forKey:@"MySpotKey"];
[encoder encodeInt: self.localOffset forKey:@"LocalOffsetKey"];
}



//-----------------------------------------
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
    self.mySpot = [decoder decodeObjectForKey:@"MySpotKey"];
    self.localOffset = [decoder decodeIntForKey:@"LocalOffsetKey"];
}
return self;
}

一切都在各自的 .h 中正确定义,如下所示

NSMutableArray *mySpotsArray;

NSUserDefaults *savedSpots;

NSData *encodedMySpotsArrayObject;
NSData *decodedMySpotsArrayObject;

所有在 UI 级别上完美运行,即正确显示、添加、删除、保存和加载表格。但是当我尝试这样的 NSLog 时:

NSLog(@"%@", mySpotsArray);

我明白了:

2012-09-19 13:41:25.372 Spot[1541:303] (
"<Spot: 0x100674ab0>",
"<Spot: 0x100674c20>",
"<Spot: 0x100675040>"
)

我也试过这个:

 NSString *strData = [[NSString alloc]initWithData: decodedMySpotsArrayObject encoding:NSUTF8StringEncoding];

NSLog(@"strData: %@", strData);

我明白了:

2012-09-19 13:41:25.371 Spot[1541:303] strData: (null)

我只需要访问 NSMutableArray 内容,然后将其转换为字符串。我在 UI 上看到的实际内容是一个 2 列 3 行的表格:

埃里温 2
伦敦 -1
洛杉矶 -9

我究竟做错了什么?

谢谢

4

2 回答 2

3

我认为这是预期的行为。

调试器如何知道它应该在控制台打印什么?如果您希望它打印其他内容,例如对象上的某些属性Spot,您需要提供Spot一个description方法。

例如:

#import <Foundation/Foundation.h>

@interface Foo:NSObject {
    NSString *_bar;
}
@property (nonatomic, copy) NSString *bar;
@end

@implementation Foo
@synthesize bar = _bar;

- (NSString *)description {
    return self.bar;
}

@end

int main(int argc, char *argv[]) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

    NSMutableArray *objs = [[NSMutableArray alloc] init];
    Foo *myFoo = [Foo new];
    myFoo.bar = @"Mine";

    Foo *yourFoo = [Foo new];
    yourFoo.bar = @"Yours";

    [objs addObject:myFoo];
    [objs addObject:yourFoo];

    NSLog(@"Objs = %@",objs);

    [p release];
}

将其打印到控制台:

2012-09-19 06:57:10.375 Untitled 2[59494:707] Objs = (
    Mine,
    Yours
)

但如果没有该description方法,这就是打印到控制台的内容:

2012-09-19 07:01:12.542 Untitled 2[59853:707] Objs = (
    "<Foo: 0x7f9773c080c0>",
    "<Foo: 0x7f9773c0a9b0>"
)
于 2012-09-19T11:59:07.363 回答
2

在您的 Spot 类中添加此方法

- (NSString *)description
{
   NSString *str = [[NSString alloc] initWithFormat:@"%@ \n %@ \n%@",Var1,Var2,var3];

   return str;
}

用您原来的变量名替换 Var1、Var2 和 Var3。

于 2012-09-19T12:07:05.437 回答