我正在编写一个应用程序,该应用程序允许用户为他们可能选择到不同位置的多条路线计时,然后保存他们可以查看的已保存数据的存档。我遇到的问题是保存数据以供持久使用。我已经阅读了 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
。
摘要:大脑有一个需要序列化的片段字典。该段具有要序列化的属性,包括路由字典(另一个自定义对象)。Routes 具有要序列化的属性,包括一系列 Trips。最后,trips 有两个值需要序列化。
提前感谢您的任何帮助。