![](/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.