我需要一些关于他可能导致我的问题的一些想法。我有一个源代码,我想对它进行一些调试,但我看不到变量。
例如:
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
NSLog(@"The content of oldSavedArray is%@",oldSavedArray);
我明白了:
2015-05-04 11:06:28.275 MobilePocket[59803:12619613] The content of oldSavedArray is(
"<MPCheckedCurrency: 0x7f9903b21090>",
"<MPCheckedCurrency: 0x7f9903b20fc0>",
"<MPCheckedCurrency: 0x7f9903b21130>",
"<MPCheckedCurrency: 0x7f9903b21170>",
"<MPCheckedCurrency: 0x7f9903b21510>",
"<MPCheckedCurrency: 0x7f9903b21190>"
)
或为此:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:currencyCheckedArrayKey];
我是这样的:
(lldb) po dataRepresentingSavedArray
<62706c69 73743030 d4010203 0405064d 4e582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 ...etc
可能是这个问题:
@implementation MPCheckedCurrency
@synthesize currencyCode;
@synthesize currencyValue;
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:currencyCode forKey:currencyCheckedCodeKey];
[coder encodeObject:currencyValue forKey:currencyCheckedValueKey];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [super init];
if (self != nil)
{
self.currencyCode = [coder decodeObjectForKey:currencyCheckedCodeKey];
self.currencyValue = [coder decodeObjectForKey:currencyCheckedValueKey];
}
return self;
}
我怎样才能看到数组的内容? 任何帮助都是合情合理的!
答案 0 :(得分:1)
您正在为每个对象使用[NSObject description]
方法,该方法返回在此上下文中使用时感觉可能有用的任何字符串。有时根本没用。
要在description
对象中修复此问题,请执行MPCheckedCurrency
:
- (NSString *)description
{
return [NSString stringWithFormat:@"[currenyCode=%@, currencyValue=%@]", currencyCode, currencyValue];
}
(您可能还需要在description
和currencyCode
对象中实施currentValue
。
答案 1 :(得分:1)
当您使用NSLog时,它会调用description方法。
所以不是确切的代码,但你需要做类似下面的事情;
@implementation MPCheckedCurrency
@synthesize currencyCode;
@synthesize currencyValue;
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:currencyCode forKey:currencyCheckedCodeKey];
[coder encodeObject:currencyValue forKey:currencyCheckedValueKey];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [super init];
if (self != nil)
{
self.currencyCode = [coder decodeObjectForKey:currencyCheckedCodeKey];
self.currencyValue = [coder decodeObjectForKey:currencyCheckedValueKey];
}
return self;
}
-(NSString*)description{
return [NSString stringWithFormat:@"code: %d, value : %f",currencyCode,currencyValue];
}
答案 2 :(得分:-1)
@Adina - 你的问题是你在NSLog上打印完整的对象(数组)。相反,你需要在数组中循环并打印像这样的实际对象值:
for(objmpcheckcurrency in oldsavedarray)
{
NSLog(@"The content of oldSavedArray is %@ %@",objmpcheckcurrency.currencyCode, objmpcheckcurrency.currencyValue);
}