我只是想知道如何将这些数据存储在plist中,输出相同。现在我正在使用NSObject。我将这些存储在单独的字典中吗?但是我怎样才能回调它们来填充tableData数组。
Exercises *exercise1 = [Exercises new];
exercise1.name = @"Barbell Rollouts";
exercise1.materials = @"30 min";
exercise1.imageFile = @"BarbellRollouts.jpg";
exercise1.sets = @"2";
exercise1.reps = @"10";
exercise1.instructions = @"Hello";
exercise1.status = @"Dynamic";
Exercises *exercise2 = [Exercises new];
exercise2.name = @"Barbell Trunk Rotation";
exercise2.materials = @"30 min";
exercise2.imageFile = @"BarbellTrunkRotation.jpg";
exercise2.sets = @"2";
exercise2.reps = @"10";
exercise2.instructions = @"";
exercise2.status = @"Dynamic";
Exercises *exercise3 = [Exercises new];
exercise3.name = @"Bent Knee Leg Raises";
exercise3.materials = @"30 min";
exercise3.imageFile = @"BentKneeLegRaises.jpg";
exercise3.sets = @"2";
exercise3.reps = @"10";
exercise3.instructions = @"";
exercise3.status = @"Dynamic";
Exercises *exercise4 = [Exercises new];
exercise4.name = @"Bicycle Manouver";
exercise4.materials = @"30 min";
exercise4.imageFile = @"BicycleManouver.jpg";
exercise4.sets = @"2";
exercise4.reps = @"10";
exercise4.instructions = @"";
exercise4.status = @"Dynamic";
Exercises *exercise5 = [Exercises new];
exercise5.name = @"Boat Pose";
exercise5.materials = @"30 min";
exercise5.imageFile = @"BoatPose.jpg";
exercise5.sets = @"2";
exercise5.reps = @"10";
exercise5.instructions = @"";
exercise5.status = @"Static";
Exercises *exercise6 = [Exercises new];
exercise6.name = @"Bosu Boat Pose";
exercise6.materials = @"30 min";
exercise6.imageFile = @"BosuBoatPose.jpg";
exercise6.sets = @"2";
exercise6.reps = @"10";
exercise6.instructions = @"";
exercise6.status = @"Static";
tableData = [NSArray arrayWithObjects:exercise1,exercise2,exercise3,exercise4,exercise5,exercise6,nil];
答案 0 :(得分:2)
您需要实施NSCoding
protocol
在标题中:
@interface Exercise : NSObject <NSCoding>
// Your public property list
@end
在您的实施文件中:
@interface Exercise()
// Your private property list
@end
@implementation Exercise
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}
self.name = [decoder decodeObjectForKey:@"name"];
self.sets = [decoder decodeIntegerForKey:@"sets"];
// similar approach for other properties
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeInteger:self.sets forKey:@"sets"];
// similar approach for other properties
}
@end
您现在应该可以使用NSKeyedArchiver
保存到您的plist中,NSKeyedUnarchiver
可以将其保存回来:
[NSKeyedArchiver archiveRootObject:exercise toFile:@"/path/to/file"];
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/file"];