![](/img/trans.png)
[英]Best way to save data in iPod Touch/iPhone Objective-C
[英]Best way to save a hierarchy of custom Objective-c objects for persistence
我正在编写一个应用程序,该应用程序允许用户为可能选择的到达不同位置的多条路线计时,然后将其保存为可查看数据的存档。 我遇到的问题是保存数据以供永久使用。 我已经阅读了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数组。 最后,行程有两个需要序列化的值。
在此先感谢您的协助。
好的,所以我通过在BestRouteSegment
内实现NSCoding
协议解决了这个问题,该协议对我的段进行了编码而没有错误。 这是遵守上述协议的方法。
- (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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.