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

Objective c NSMutableArray显示一些十六进制内容,而不是实际值,objective-c,xcode,nsmutablearray,nsdata,nslog,Objective C,Xcode,Nsmutablearray,Nsdata,Nslog,可能重复: 我已将NSMutableArray初始化如下: - (id)init { self = [super init]; if (self) { mySpotsArray = [[NSMutableArray alloc]init]; } return self; } //----------------------------------------- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView

可能重复:

我已将NSMutableArray初始化如下:

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

return self;
}
//-----------------------------------------
- (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];
}
数组按如下方式存储NSTable的数据:

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

return self;
}
//-----------------------------------------
- (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;
}
NSLog(@"%@", mySpotsArray);
我正在保存和加载数组内容,如下所示:

//-----------------------------------------
- (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;
}
NSLog(@"%@", mySpotsArray);
对象的编码和解码如下所示:

//-----------------------------------------
- (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;
}
NSLog(@"%@", mySpotsArray);
每件事都有各自的定义

NSMutableArray *mySpotsArray;

NSUserDefaults *savedSpots;

NSData *encodedMySpotsArrayObject;
NSData *decodedMySpotsArrayObject;
所有工作在UI级别上都非常完美,即表格正确显示、添加、删除、保存和加载。但当我尝试这样记录时:

//-----------------------------------------
- (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;
}
NSLog(@"%@", mySpotsArray);
我明白了:

2012-09-19 13:41:25.372 Spot[1541:303] (
"<Spot: 0x100674ab0>",
"<Spot: 0x100674c20>",
"<Spot: 0x100675040>"
)
2012-09-19 13:41:25.371 Spot[1541:303] strData: (null)
我明白了:

2012-09-19 13:41:25.372 Spot[1541:303] (
"<Spot: 0x100674ab0>",
"<Spot: 0x100674c20>",
"<Spot: 0x100675040>"
)
2012-09-19 13:41:25.371 Spot[1541:303] strData: (null)
我只需要访问NSMutableArray内容,然后将其转换为字符串。我在UI上看到的实际内容是一个包含2列和3行的表:

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

我做错了什么


谢谢

我想这是意料之中的行为

调试器如何知道应该向控制台打印什么?如果要打印其他内容,例如
Spot
对象上的某些属性,则需要为
Spot
提供
说明
方法

例如:

#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
)
2012-09-19 07:01:12.542 Untitled 2[59853:707] Objs = (
    "<Foo: 0x7f9773c080c0>",
    "<Foo: 0x7f9773c0a9b0>"
)
2012-09-19 07:01:12.542无标题2[59853:707]Objs=(
"",
""
)

我认为这是预期的行为

调试器如何知道应该向控制台打印什么?如果要打印其他内容,例如
Spot
对象上的某些属性,则需要为
Spot
提供
说明
方法

例如:

#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
)
2012-09-19 07:01:12.542 Untitled 2[59853:707] Objs = (
    "<Foo: 0x7f9773c080c0>",
    "<Foo: 0x7f9773c0a9b0>"
)
2012-09-19 07:01:12.542无标题2[59853:707]Objs=(
"",
""
)

在Spot类中添加此方法

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

   return str;
}

用原始变量名替换Var1、Var2和Var3。

在Spot类中添加此方法

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

   return str;
}


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

一个最小的、自包含的示例如何?是的,我昨天试过了,人们抱怨如果看不到代码他们就无能为力。。。现在代码太多了…正确。生活就是要在太多和太少之间找到平衡。如果你在餐馆里要水,如果服务员把你扔到鱼塘里,你可能不会感激。@grep,与其使用表视图方法,不如查看Spot.h/.m文件。您是否实现了自定义的
描述
方法?让我告诉您我的理解。您希望以纯文本格式以长格式打印对象。我说的对吗?一个简单的、自成一体的例子怎么样?是的,我昨天试过了,人们抱怨如果他们看不到代码他们就无能为力。。。现在代码太多了…正确。生活就是要在太多和太少之间找到平衡。如果你在餐馆里要水,如果服务员把你扔到鱼塘里,你可能不会感激。@grep,与其使用表视图方法,不如查看Spot.h/.m文件。您是否实现了自定义的
描述
方法?让我告诉您我的理解。您希望以纯文本格式以长格式打印对象。我说得对吗?我如何添加描述方法?在您的
Spot
实现中。非常感谢,它对表的第一列起到了作用。如何对第二列执行相同操作,其中的值为int?。。。字符串格式,一般来说…如果我有NSString bar1和bar2,并且想要NSLog(@“Obj1=%@Obj2%@“,[objs objectForKey:@“bar1”],[objs objectForKey:@“bar2”]),该怎么办;如何添加描述方法?在您的
Spot
实现中。非常感谢,它对表的第一列有效。如何对第二列执行相同操作,其中的值为int?。。。字符串格式,一般来说…如果我有NSString bar1和bar2,并且想要NSLog(@“Obj1=%@Obj2%@“,[objs objectForKey:@“bar1”],[objs objectForKey:@“bar2”]),该怎么办;谢谢CRDave,我使用了Alan的方法,效果很好,但它只显示了第1列的内容。这个表有两列,一列是字符串,第二列是整数,我怎样才能在第二列中实现相同的结果呢?@CRDave给出的答案就是答案。我们不知道你的
Spot
类的结构,所以你必须从他上面写的内容推断到你的实现中。用NSString str追加这个数字。仍然需要学习很多,但无论如何谢谢你谢谢CRDave,我使用了Alan的方法,它很有效,但它只显示了第1列的内容。这个表有两列,一列是字符串,第二列是整数,我怎样才能在第二列中实现相同的结果呢?@CRDave给出的答案就是答案。我们不知道你的
Spot
类的结构,所以你必须从他上面写的内容推断到你的实现中。用NSString str追加这个数字。仍然需要学习很多,但无论如何,谢谢你