我对Objective c很新。在我的应用程序中,我想在.plist文件中保存一个由NSString构成的对象和三个分段控件中的三个NSInteger令牌。它在32位模拟器和设备上工作正常,但是在64位中传递了一个空对象,因为我想是NSCoder。 在我的对象类中,我实现了这两个方法:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject: self.cardPath forKey:@"cardPath"];
[coder encodeInteger: (NSInteger) self.cardSign forKey:@"cardSign"];
[coder encodeInteger: (NSInteger) self.cardNumber forKey:@"cardNumber"];
[coder encodeInteger: (NSInteger) self.cardColor forKey:@"cardColor"];
}
- (id)initWithCoder:(NSCoder *)coder {
self.cardPath = [coder decodeObjectForKey:@"cardPath"];
self.cardSign = (NSInteger*) [coder decodeIntegerForKey: @"cardSign"];
self.cardNumber = (NSInteger*) [coder decodeIntegerForKey:@"cardNumber"];
self.cardColor = (NSInteger*) [coder decodeIntegerForKey:@"cardColor"];
return self;
}
我从分段控件中获取值:
cardSelected.cardSign = [NSNumber numberWithInteger:[sender selectedSegmentIndex]];
和
cardSelected.cardNumber = [NSNumber numberWithInt:[sender selectedSegmentIndex]];
和我比较:
cardSelected.cardColor = [NSNumber numberWithInt:[sender selectedSegmentIndex]];
if ([cardSelected.cardColor intValue] == 0)
{
[iBack setImage: [UIImage imageNamed: @"5_0.png"]];
NSLog(@"red");
}
我保存了对象:
'-(IBAction)game:(id)sender
{
NSMutableArray *cards = [[NSMutableArray alloc] initWithObjects: cardSelected, nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cards];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"cards"];
[data writeToFile:path atomically:YES];}'
我尝试以这种方式在viewDidLoad方法中检索保存的对象
- (void)viewDidLoad
{
[super viewDidLoad];
// testing for 64bit at runtime
if (sizeof(void*) == 4) {
NSLog(@"You're running in 32 bit");
} else if (sizeof(void*) == 8) {
NSLog(@"You're running in 64 bit");
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[NSString alloc]initWithString:[paths[0]stringByAppendingPathComponent:@"selectedCard.plist"]];
if([[NSFileManager defaultManager]fileExistsAtPath:path])
{
segSign.momentary = NO;
segNumber.momentary = NO;
segColor.momentary = NO;
[segSign addTarget:self action:@selector(segmentedSign:) forControlEvents: UIControlEventValueChanged];
[segNumber addTarget:self action:@selector(segmentedNumber:) forControlEvents: UIControlEventValueChanged];
[segColor addTarget:self action:@selector(segmentedColor:) forControlEvents: UIControlEventValueChanged];
NSData *cardsData = [[NSUserDefaults standardUserDefaults] objectForKey:@"cards"];
NSMutableArray *cards = [NSKeyedUnarchiver unarchiveObjectWithData:cardsData];
cardSelected = [cards objectAtIndex:0];
//NSLog(@"%li",cardSelected.cardNumber);
if ([cardSelected.cardColor intValue] == 0)
{
[iBack setImage: [UIImage imageNamed: @"5_0.png"]];
NSLog(@"red");
}
if ([cardSelected.cardColor intValue] == 1)
{
[iBack setImage: [UIImage imageNamed: @"5_1.png"]];
NSLog(@"blue");
}
[segSign setSelectedSegmentIndex: [cardSelected.cardSign intValue]];
NSLog(@"check sign...%i", [cardSelected.cardSign intValue]); //always display 1
NSLog(@"double-check...%@", cardSelected.cardSign); //always display 1
[segNumber setSelectedSegmentIndex: [cardSelected.cardNumber intValue]];
[segColor setSelectedSegmentIndex: [cardSelected.cardColor intValue]];
} else
{
NSLog(@"else");
Card *i = [[Card alloc]init];
i.cardSign = 0;
i.cardNumber = 0;
i.cardPath = @"0_0.png";
[iBack setImage: [UIImage imageNamed: @"5_0.png"]];
cardSelected = i;
}
[iImage setImage: [UIImage imageNamed : cardSelected.cardPath]];
// Do any additional setup after loading the view, typically from a nib.
}
cardSelected是一个带有.h文件的对象卡:
@interface Card : NSObject <NSCoding>
{
NSNumber *cardSign, *cardNumber, *cardColor;
NSString *cardPath;
}
@property (nonatomic) NSNumber *cardSign, *cardNumber, *cardColor;
@property (nonatomic, strong) NSString *cardPath;
@property (nonatomic, strong) NSCoder *coder, *encoder;
@end
并将.m文件实现为
'
@interface Card : NSObject { NSNumber *cardSign, *cardNumber, *cardColor; NSString *cardPath;
}
@property (nonatomic) NSNumber *cardSign, *cardNumber, *cardColor;
@property (nonatomic, strong) NSString *cardPath;
@property (nonatomic, strong) NSCoder *coder, *encoder;
@end
” 使用对象(卡)* cardSelected时,它没有预期的值。 我做错了什么?
答案 0 :(得分:1)
您使用NSInteger的方式很糟糕。
NSInteger是标量值(取决于平台,它可以是int或long)。
所以当你这样做时
cardSelected.cardSign = (NSInteger*) [sender selectedSegmentIndex];
它错了。
您应该在没有*。
的情况下设置NSInteger ivar简短回答:在NSInteger之后移除所有星星并且它将起作用。
答案 1 :(得分:0)
解决!我试图使用NSCoder存储由三个NSNumber组成的对象'Card'。现在我将三个NSNumber存储在一个NSMutableArray中,我将它写入一个plist文件,没有'Card'类它可以工作,32位和64位。感谢您的帮助,对于虚拟对象,最好简化代码;)