我将我的网站上的数据同步到我的应用程序,我正在使用NSXMLParser来执行此操作。问题是我把我的数据库中的所有字段定义为字符串。当所有内容都是字符串时,同步过程可以正常工作,但是当我尝试将这些数据用于其他目的时,这会让我心痛不已。数据类型和XMLParser
谁能帮我确定我的领域具有正确的数据类型的同步过程,下面的代码:
.M
// Array for WORKOUT.
NSMutableString *currentID, *currentUserID, *currentWalkID, *currentDate, *currentDistance, *currentRepeats, *currentType, *currentIntensity,
*currentComments, *currentTime, *currentWeight, *currentHeight;
我知道它是与此的NSMutableString,显然一切被定义为一个字符串。
.H
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
// Check for the WORKOUT details in the XML feed.
if ([elementName isEqualToString:@"workout"])
{
// clear out our workout item caches...
item = [[NSMutableDictionary alloc] init];
currentID = [[NSMutableString alloc] init];
currentUserID = [[NSMutableString alloc] init];
currentWalkID = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentDistance = [[NSMutableString alloc] init];
currentRepeats = [[NSMutableString alloc] init];
currentType = [[NSMutableString alloc] init];
currentIntensity = [[NSMutableString alloc] init];
currentComments = [[NSMutableString alloc] init];
currentTime = [[NSMutableString alloc] init];
currentWeight = [[NSMutableString alloc] init];
currentHeight = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"workout"])
{
Workout *newWorkout = [NSEntityDescription insertNewObjectForEntityForName:@"Workout" inManagedObjectContext: self.managedObjectContext];
// save values to an item, then store that item into the array...
[item setObject:currentID forKey:@"workout_id"];
[item setObject:currentUserID forKey:@"workout_user_id"];
[item setObject:currentWalkID forKey:@"workout_walk_id"];
[item setObject:currentDate forKey:@"workout_date"];
[item setObject:currentDistance forKey:@"workout_distance"];
[item setObject:currentRepeats forKey:@"workout_repeats"];
[item setObject:currentType forKey:@"workout_type"];
[item setObject:currentIntensity forKey:@"workout_intensity"];
[item setObject:currentComments forKey:@"workout_comments"];
[item setObject:currentTime forKey:@"workout_time"];
[item setObject:currentWeight forKey:@"workout_weight"];
[item setObject:currentHeight forKey:@"workout_height"];
newWorkout.workout_id = currentID;
newWorkout.workout_user_id = currentUserID;
newWorkout.workout_walk_id = currentWalkID;
newWorkout.workout_date = currentDate;
newWorkout.workout_distance = currentDistance;
newWorkout.workout_repeats = currentRepeats;
newWorkout.workout_type = currentType;
newWorkout.workout_intensity = currentIntensity;
newWorkout.workout_comments = currentComments;
newWorkout.workout_time = currentTime;
newWorkout.workout_weight = currentWeight;
newWorkout.workout_height = currentHeight;
[self.workoutArray addObject:newWorkout];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// save the characters for the current item...
if ([currentElement isEqualToString:@"workout_id"]) {
[currentID appendString:string];
} else if ([currentElement isEqualToString:@"workout_user_id"]) {
[currentUserID appendString:string];
} else if ([currentElement isEqualToString:@"workout_walk_id"]) {
[currentWalkID appendString:string];
} else if ([currentElement isEqualToString:@"workout_date"]) {
[currentDate appendString:string];
} else if ([currentElement isEqualToString:@"workout_distance"]) {
[currentDistance appendString:string];
} else if ([currentElement isEqualToString:@"workout_repeats"]) {
[currentRepeats appendString:string];
} else if ([currentElement isEqualToString:@"workout_type"]) {
[currentType appendString:string];
} else if ([currentElement isEqualToString:@"workout_intensity"]) {
[currentIntensity appendString:string];
} else if ([currentElement isEqualToString:@"workout_comments"]) {
[currentComments appendString:string];
} else if ([currentElement isEqualToString:@"workout_time"]) {
[currentTime appendString:string];
} else if ([currentElement isEqualToString:@"workout_weight"]) {
[currentWeight appendString:string];
} else if ([currentElement isEqualToString:@"workout_height"]) {
[currentHeight appendString:string];
}
在不知道锻炼实体的情况下回答你的问题有点困难。请告诉我们哪些字段有什么类型。此外,在我看来,你正在泄漏该项目词典和所有可变的字符串。在分配新对象之前,您应该释放旧对象。 – tonklon 2010-07-23 09:56:53
锻炼实体中的所有字段都是NSString。 – Stephen 2010-07-23 10:03:36
我不明白你的问题。如果您需要其他类型,请更改实体的定义。你想知道如何将一个字符串转换为不同的类型? – tonklon 2010-07-23 10:07:52