2010-05-16 5 views
0

ma première question! J'ai été capable de coder la plupart de ce lecteur RSS sans avoir besoin d'aide (grâce à de nombreuses recherches via stackoverflow!) Mais je suis perplexe ici.Comment supprimer 3 caractères de la fin d'un NSURL?

NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; 
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; 

NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces]; 
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; 
urlbase = [filteredArray componentsJoinedByString:@" "]; 

NSLog(@"%@ %i" , urlbase, 4353); 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 

Le tableau de links3 est un NSMutableArray avec des cordes. Les premières lignes fonctionnent parfaitement en éliminant l'espace au début de chaque chaîne de ce tableau, qui est stocké dans 'urlbase', donc ils ont l'air bien quand ils sortent. Quand nous NSLog urlbase, nous voyons:

http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE 

Mais, lorsque nous utilisons: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]

Nous voyons: http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE%0A

Comment puis-je résoudre ce problème? Puis-je retirer ces éléments de la queue en quelque sorte? Merci!

Code de travail:

NSMutableArray *links3 = [[NSMutableArray alloc] init]; 
RSSAppDelegate *appDelegate = (RSSAppDelegate *)[[UIApplication sharedApplication] delegate]; 
links3 = appDelegate.links; 
NSLog(@"%@ %i", [links3 objectAtIndex:indexPath.row], 3453); 
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; 
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; 

NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces]; 
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; 
urlbase = [filteredArray componentsJoinedByString:@" "]; 

NSLog(@"%@ %i" , urlbase, 4353); 


NSLog(@"%@ %i" , urlbase, 345346); 
NSString* fixed = [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 

NSURL *hellothere = [NSURL URLWithString:fixed]; 
NSLog(@"%@ %i", hellothere, 54); 


    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fixed]]; 

Hé, je ne l'ai pas dit qu'il était assez. Merci Diederik Hoogenboom!

+1

% 0A est un caractère de saut de ligne (fin de ligne). C'est probablement déjà dans urlbase que vous ne le voyez pas dans NSLog parce que c'est un espace. Regardez comment vous l'avez eu là-bas en premier lieu. – progrmr

Répondre

1

Essayez ceci:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] 

La dernière partie de votre chaîne d'URL est un saut de ligne.

+0

Merci beaucoup! Cela a bien fonctionné. – saywhatman

+0

Bon à entendre! Pouvez-vous s'il vous plaît marquer cette réponse comme celle qui l'a résolu, merci. – diederikh

Questions connexes