1

我有一个 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>",
4

3 回答 3

3

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

- (NSString*) description
{
    // Example:
    return [NSString stringWithFormat: @"ivar1=%@ , ivar2=%@",ivar1, ivar2];
}
于 2013-03-26T17:01:15.290 回答
0

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

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

for(DataPoint *dp in data){
   NSLog(@"%@",dp.property);//property should be your property name of DataPoint class.
}
于 2013-03-26T16:59:31.813 回答
0

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

- (NSString *)description {

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

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

于 2013-03-26T17:14:47.003 回答