1

我正在编写一个应用程序,该应用程序允许用户为他们可能选择到不同位置的多条路线计时,然后保存他们可以查看的已保存数据的存档。我遇到的问题是保存数据以供持久使用。我已经阅读了 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 tripTimeNSString *timeOfDay

摘要:大脑有一个需要序列化的片段字典。该段具有要序列化的属性,包括路由字典(另一个自定义对象)。Routes 具有要序列化的属性,包括一系列 Trips。最后,trips 有两个值需要序列化。

提前感谢您的任何帮助。

4

1 回答 1

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;
}
于 2013-10-31T01:34:26.747 回答