1

我只是想知道如何将这些数据存储在具有相同输出的 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];
4

1 回答 1

2

您需要实现NSCoding 协议

在您的标题中:

@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"];
于 2013-08-23T23:47:37.010 回答