我正在编写一个应用程序,允许用户计算他们可能选择到不同位置的多条路线,然后拥有他们可以查看的已保存数据的存档。我遇到的问题是保存数据以供持久使用。我已经阅读了NSCoding协议,但它似乎不适合我的对象需求,因为我的对象中存在不兼容的属性,如CLCoordinateLocation2D(我知道这个我可以分成更多的组件,如纬度和经度,但我是认为必须有更好的方法)但还有其他方法。我来自java,我需要做的就是实现可序列化的接口,我很高兴,但我是Objective-C的新手,并且不确定解决问题的最佳方法。
以下是我需要序列化的对象结构。
@interface BestRouteBrain : NSObject < CLLocationManagerDelegate, NSCoding > {
NSDictionary *segments;
BestRouteTimer *timer;
BestRouteSegment *currentSegment;
//Manages attributes that communicate with GPS
CLLocationManager *locationManager;
Firebase *firebase;
//NSDictionary *routesByTimeOfDay;
//NSDictionary *routesByAverage;
}
@property ( strong ) CLLocationManager *locationManager;
@property BestRouteSegment *currentSegment;
@property BestRouteTimer *timer;
@property NSDictionary *segments;
@property Firebase *firebase;
基本上,我需要序列化的是NSDictionary *segments
。以下是细分市场的内容
@interface BestRouteSegment :NSObject <NSCoding>{
// Name of segment
NSString *segmentName;
// String reprsenting starting location for segment
NSString *startingLocation;
// String representing ending location for segment
NSString *endingLocation;
//Key is name of route and value is a route object;
NSDictionary *routesForSegment;
CLLocationCoordinate2D startCoord;
CLLocationCoordinate2D endCoord;
}
@property CLLocationCoordinate2D startCoord;
@property CLLocationCoordinate2D endCoord;
@property NSString *startingLocation;
@property NSString *endingLocation;
@property NSString *segmentName;
@property NSDictionary *routesForSegment;
此对象中的所有属性也需要序列化,包括NSDictionary *routesForSegment
。这是路线的样子,
@interface Route : NSObject {
// Name of route given by user
NSString *routeName;
// The total average time for all trips using this route
double avgRouteTime;
// Array of trips which used this route
NSArray *allTripsFromRoute;
}
@property NSString *routeName;
@property double avgRouteTime;
@property NSArray *allTripsFromRoute;
所有这些属性也需要序列化。最后,这是旅行的样子。
@interface BestRouteTrip : NSObject {
// Time elapsed for current trip
double tripTime;
// String representing time of day - Departure time is
NSString *timeOfDay;
NSDate *morningBeforeTrafic;// from 12:00am to 6:30am
NSDate *morningTrafic; // from 6:30am to 9:30am
NSDate *morningAfterTrafic; // from 9:30am to 12:00pm
NSDate *afternoon; // from 12:00pm to 3:30pm
NSDate *eveningTrafic; // from 3:30pm to 6:30pm
NSDate *eveningAfterTrafic; // from 6:30pm to 12:00p
}
@property double tripTime;
@property NSString *timeOfDay;
@property NSDate *morningBeforeTrafic;// from 12:00am to 6:30am
@property NSDate *morningTrafic; // from 6:30am to 9:30am
@property NSDate *morningAfterTrafic; // from 9:30am to 12:00pm
@property NSDate *afternoon; // from 12:00pm to 3:30pm
@property NSDate *eveningTrafic; // from 3:30pm to 6:30pm
@property NSDate *eveningAfterTrafic; // from 6:30pm to 12:00p
我需要序列化的唯一两个值是double tripTime
和NSString *timeOfDay
。
总结:大脑有一个需要序列化的片段字典。该段具有要序列化的属性,包括路径字典(另一个自定义对象)。路由具有要序列化的属性,包括一系列Trips。最后,旅行有两个需要序列化的值。
提前感谢您的任何帮助。
答案 0 :(得分:1)
好的,所以我通过在NSCoding
内部实现BestRouteSegment
协议来解决这个问题,该协议对我的段进行无错编码。以下是遵守上述协议的方法。
- (void)encodeWithCoder:(NSCoder *)encoder {
NSLog(@"Encode in segment was called");
[ encoder encodeDouble:self.startCoord.latitude
forKey:@"startCoord.latitude" ];
[ encoder encodeDouble:self.startCoord.longitude
forKey:@"startCoord.longitude" ];
[ encoder encodeDouble:self.endCoord.latitude
forKey:@"endCoord.latitude" ];
[ encoder encodeDouble:self.endCoord.longitude
forKey:@"endCoord.longitude" ];
[ encoder encodeObject:self.segmentName
forKey:@"segmentName" ];
[ encoder encodeObject:self.startingLocation
forKey:@"startingLocation" ];
[ encoder encodeObject:self.endingLocation
forKey:@"endingLocation" ];
[ encoder encodeObject:self.routesForSegment
forKey:@"routesForSegment" ];
}
- (id)initWithCoder:(NSCoder *)adecoder {
NSLog(@"Init with coder was called in segment");
if ( self = [ super init ] ) {
self.startCoord =
CLLocationCoordinate2DMake(
[ adecoder decodeDoubleForKey:@"startCoord.lattitude" ],
[ adecoder decodeDoubleForKey:@"startCoord.longitude" ]
);
self.endCoord =
CLLocationCoordinate2DMake(
[ adecoder decodeDoubleForKey:@"endCoord.lattitude" ],
[ adecoder decodeDoubleForKey:@"endCoord.longitude" ]
);
self.segmentName =
[ adecoder decodeObjectForKey:@"segmentName" ];
self.startingLocation =
[ adecoder decodeObjectForKey:@"startingLocation" ];
self.endingLocation =
[ adecoder decodeObjectForKey:@"endingLocation" ];
self.routesForSegment =
[ adecoder decodeObjectForKey:@"routesForSegment" ];
}
return self;
}