2012-01-30 30 views
1

編輯:標題更改爲更好地反映我的問題。爲什麼NSLog格式文本正確,但NSMutableArray不?

我在解析大量包含XML數據換行符和引號的文本時遇到問題。我通過AppDelegate運行解析器,以便在應用程序打開時預先加載。我的問題是文本在解析器didEndElement部分中被正確拉入,但是當我的視圖從AppDelegate的NSMutableArray調用文本時,\ n和其他html標籤在它們不應該出現時出現。截圖發佈在這個問題的底部。

AppDelegate.h相關代碼:

@interface AppDelegate : UIResponder <UIApplicationDelegate, NSXMLParserDelegate> 
{ 
    NSXMLParser    *exhibitParser; 
    NSMutableArray   *exhibitInfo; 
} 

@property (nonatomic, retain) NSXMLParser *exhibitParser; 
@property (nonatomic, retain) NSMutableArray *exhibitInfo; 

- (void) parseAllData; 

@end 

AppDelegate.m:

#import "AppDelegate.h" 
#import "ExhibitParser.h" 


@implementation AppDelegate 

@synthesize exhibitParser, exhibitInfo; 
@synthesize window = _window; 

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //delay to allow for splash screen  
    [self performSelector:@selector(parseAllData) withObject:nil afterDelay:2.0f]; 

    return YES; 
} 

- (void)parseAllData 
{  
    exhibitParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.website-Goes-Here.com/variable=%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"gallery_preference"]]]]; 

    ExhibitParser *exParser = [[ExhibitParser alloc] initExhibitParser]; 

    [exhibitParser setDelegate:exParser]; 
    //[parser parse]; 

    BOOL exsuccess = [exhibitParser parse]; 

    if(exsuccess) 
    NSLog(@"No Exhibit Errors"); 
    else 
    NSLog(@"Exhibit ERROR!!"); 

} 

ExhibitParser.h:

#import <Foundation/Foundation.h> 

@class AppDelegate, ExhibitInfo; 

@interface ExhibitParser : NSObject 
{ 
    NSString   *currentExhibit; 

    NSMutableString *descAttribute; 
    NSMutableDictionary *dict; 

    AppDelegate  *exAppDelegate; 
    ExhibitInfo  *aExItems; 
} 

@property (nonatomic, retain) NSString   *currentExhibit; 
@property (nonatomic, retain) NSMutableString  *descAttribute; 
@property (nonatomic, retain) NSMutableDictionary *dict; 

@property (nonatomic, retain) ExhibitInfo   *aExItems; 

- (ExhibitParser *) initExhibitParser; 

@end 

ExhibitParser.m:

(注:中的一系列格式化didEndElement採用XML文本,使它在我們的iPad上看起來更好。多數民衆贊成被寫在這裏的\ n正確顯示在下面的NSLog的換行符,但顯示爲字面意思是「\ n」一次顯示在ExhibitView)

#import "ExhibitParser.h" 
#import "AppDelegate.h" 
#import "ExhibitInfo.h" 

@implementation ExhibitParser 
@synthesize currentExhibit, descAttribute, dict, aExItems; 

- (ExhibitParser *) initExhibitParser 
{ 
    self = [super init]; 

    exAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    return self; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    currentExhibit = [elementName copy]; 

    if ([elementName isEqualToString:@"ExhibitData"]) 
    { 
     exAppDelegate.exhibitInfo = [[NSMutableArray alloc] init]; 
    } 

    if ([elementName isEqualToString:@"exhibit"]) 
    { 
     aExItems = [[ExhibitInfo alloc] init]; 

     dict = [[NSMutableDictionary alloc] init]; 
     descAttribute = [[NSMutableString alloc] init]; 
    } 

    if ([attributeDict objectForKey:@"exhibitfact"]) 
    { 
     descAttribute = [attributeDict objectForKey:@"exhibitfact"]; 
     [dict setObject:descAttribute forKey:@"exhibitfact"]; 
    } 

} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 

    if([elementName isEqualToString:@"exhibit"]) 
    { 
     [exAppDelegate.exhibitInfo addObject:aExItems]; 
     [dict setObject:descAttribute forKey:@"exhibitfact"]; 

     //Formatting Series to make it pretty 
     NSString *descriptionRaw = [NSString stringWithFormat:@"%@", descAttribute]; 
     NSString *descriptionFinal = [descriptionRaw stringByReplacingOccurrencesOfString:@" " withString:@"\n"]; 
     NSString *descriptionEdited = [descriptionFinal stringByReplacingOccurrencesOfString:@" " withString:@" "]; 

     //This Log displays the text perfectly 
     NSLog (@"%@", descriptionEdited); 
     [aExItems setDescription:descriptionEdited]; 
    } 


} 

//I have read that foundCharacters can cause issues with hidden characters such as \n, so I've tried leaving this in and commenting it out. It seems to have no effect either way. 

//- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
//{ 
//  
// if([self.currentExhibit isEqualToString:@"description"]) 
// { 
//  [self.descAttribute appendString:string]; 
// } 
//} 

@end 

ExhibitInfo.h:

#import <Foundation/Foundation.h> 

@interface ExhibitInfo : NSObject 
{ 
    NSString *description; 
} 

@property (nonatomic, retain) NSString *description; 

@end 

ExhibitInfo.m:

#import "ExhibitInfo.h" 

@implementation ExhibitInfo 
@synthesize description; 

@end 

ExhibitView.m(其中文本框將與解析的數據填充):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setTitle:@"Exhibit Information"]; 

    exAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    exhibitText.text = [NSString stringWithFormat:@"%@", [exAppDelegate.exhibitInfo description]]; 

} 

因此,我得到完全大腦粉碎的是,ExhibitParser.m中的NSLog正確顯示了完整的文本體,但是在ExhibitView.m中,當我用相信應該完全相同的方式填充exhibitText.text時NSLog裏有什麼,事實並非如此。它顯示\ n並在整個身體周圍打開和關閉()。這是因爲它在被ExhibitView調用之前被寫入NSMutableArray?爲什麼NSLog和視圖顯示有什麼區別?

截圖中的NSLog輸出的 - 正確顯示: 它是如何顯示在視圖中,當應用程序運行(注意在開始和結束時,\ n無論是從我的格式,並從括號NSLog (@"%@", descriptionEdited);

截圖換行符,報價在開始和結束,\旁邊的文本主體的中部)報價: exhibitText.text = [NSString stringWithFormat:@"%@", [exAppDelegate.exhibitInfo description]];

回答

1

當你發現你的元素嘗試刪除新行字符是這樣的:

if ([attributeDict objectForKey:@"exhibitfact"]) 
     { 
      descAttribute = [[attributeDict objectForKey:@"exhibitfact"] stringByReplacingOccurrencesOfString:@"/n" withString:@""];   

      [dict setObject:descAttribute forKey:@"exhibitfact"]; 
     } 
+0

這是有效的,但並沒有真正回答我的問題,這就是爲什麼它在NSLog實例中顯示完美,但沒有顯示在視圖上的文本框中。您的方法確實會阻止顯示文本「\ n」,但它不會創建它表示的換行符。我在didEndElement中使用的格式不會顯示無關的代碼並將\ n解釋爲換行符。 (見截圖)。我看不到如何應用您提供的方法來刪除前後顯示的括號。感謝您及時的回覆。我會更多地討論這個問題並回報。 – BobbyScon 2012-01-30 18:46:27

相關問題