I have a struct as defined by the following:
typedef struct {
NSString *SportName;
NSInteger numberOfPeriods;
CFTimeInterval periodLength;
NSString *periodName;
CFTimeInterval secondaryClockTime;
NSString *LeftSecondaryCounterName;
NSString *RightSecondaryCounterName;
bool PossessionArrow;
} GamePreset;
Is it possible to write variables of type GamePreset to a plist file?
There’s no built-in support for writing an arbitrary
structas a property list. You have to write a method that converts the struct to a property list.In theory, you could do it at run-time by parsing the result of
@encode(GamePreset), which is replaced (at compile-time) by a string describingGamePreset:This has serious drawbacks. One is that you don’t get the field names, so your plist won’t be particularly useful unless the reader knows the
structformat, and any change to thestruct‘s layout will make saved plists unreadable.Another big drawback is that the
@encodestring doesn’t have any information about structure padding. You have to account for it when parsing the string. I think you can useNSGetSizeAndAlignmentto help with this.If you want to risk your sanity and try this, read the “Type Encodings” section of the Objective-C Runtime Programming Guide.
I recommend you just bite the bullet and write a simple function that takes a
GamePreset *and returns anNSDictionary *using hardcoded knowledge of the structure layout.