NSlog显示奇数输出

时间:2013-03-26 16:53:41

标签: objective-c arrays nslog

我有一个NSlog,它在调试器中提供奇数输出。如何让它显示正确的值?

 NSError *error = nil;
NSArray *data;

[self setStatus:@"Syncing data..."];
self.userInfo = [self.cloud Authenticate:[self serialNumber]];

if ( self.deviceInfo )
{
    data = [self.device GetData:&error];
    if ( !data )
    {
        [self displayErrorMessage:error];
        data = [NSMutableArray array];
    }

    //data received from device: Log point
    NSLog(@"data received from device: %@",data);

输出输出

   "<DataPoint: 0x1001f81b0>",
"<DataPoint: 0x10012f5f0>",
"<DataPoint: 0x1001f7780>",
"<DataPoint: 0x1001f8780>",

3 个答案:

答案 0 :(得分:3)

这是NSObject的description方法返回的默认字符串,它只打印指针值。如果要查看打印的正确数据,请覆盖DataPoint类中的描述:

- (NSString*) description
{
    // Example:
    return [NSString stringWithFormat: @"ivar1=%@ , ivar2=%@",ivar1, ivar2];
}

答案 1 :(得分:0)

在数组中存储DataPoint的对象时,会显示正确的输出。

如果你想看到每个人的全部价值,那么你需要使用

for(DataPoint *dp in data){
   NSLog(@"%@",dp.property);//property should be your property name of DataPoint class.
}

答案 2 :(得分:0)

+1 @ Ramy覆盖description的好建议,我建议扩展,而不是替换,如下:

- (NSString *)description {

    return [NSString stringWithFormat: @"%@: ivar1=%@, ivar2=%@",
        [super description], self.ivar1, self.ivar2];
}

回答类和%p指针的继承行为也非常有用。