NSMutableArray显示一些十六进制内容而不是实际值

时间:2012-09-19 11:47:54

标签: objective-c xcode nsmutablearray nsdata nslog

  

可能重复:
  NSMutableArray Not showing actual values when NSLog in iphone application

我的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行的表:

Yerevan 2
伦敦-1
洛杉矶-9

我做错了什么?

由于

2 个答案:

答案 0 :(得分: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>"
)

答案 1 :(得分:2)

在Spot类中添加此方法

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

   return str;
}

将Var1,Var2和Var3替换为原始变量名。