2010-07-23 38 views
0

我將我的網站上的數據同步到我的應用程序,我正在使用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]; 
} 
+0

在不知道鍛鍊實體的情況下回答你的問題有點困難。請告訴我們哪些字段有什麼類型。此外,在我看來,你正在泄漏該項目詞典和所有可變的字符串。在分配新對象之前,您應該釋放舊對象。 – tonklon 2010-07-23 09:56:53

+0

鍛鍊實體中的所有字段都是NSString。 – Stephen 2010-07-23 10:03:36

+0

我不明白你的問題。如果您需要其他類型,請更改實體的定義。你想知道如何將一個字符串轉換爲不同的類型? – tonklon 2010-07-23 10:07:52

回答

0

XML只能攜帶字符串。 Wheather它是元素的內部文本或屬性的值,最後,一切都只是一個字符串。

如果要通過XML發送其他任何內容,發送端必須將其編碼爲一個字符串,並且接收端必須解碼該字符串。爲了這一點,雙方必須就格式達成一致,例如對於整數值,一種可能性是將125編碼爲「125」,或將浮點數2.5編碼爲「2.5」。如果你使用這種格式的數字,你可以使用NSString的integerValue和floatValue對它們進行解碼。

float test = [@"2.5" floatValue]; 

有在xsd定義,你可以使用一些標準格式,但不會幫助你對它們進行解碼,如果你沒有一個解析器,這是否適合你。 (NSXMLParser不是這樣) 如果您使用其他格式請看NSScanner。對於日期和時間,你想看看NSDateFormatter。

如果您需要幫助轉換,請發佈您使用的格式,即XML的摘錄。