2010-03-12 13 views
18

Ich habe eine "Kontakt" -Klasse mit zwei Eigenschaften: firstName und lastName. Als ich eines Kontakts vollständigen Namen angezeigt werden soll, ist hier, was ich tue:Keine Zeichenkette mit [NSString stringWithFormat:] erscheint als "(null)"

NSString *fullName = [NSString stringWithFormat:@"%@ %@", contact.firstName, contact.lastName]; 

Aber wenn die Vor- und/oder nachName auf Null gesetzt ist, erhalte ich eine „(null)“ in der fullname String. Um dies zu verhindern, ist hier, was ich tue:

NSString *first = contact.firstName; 
if(first == nil) first = @""; 
NSString *last = contact.lastName; 
if(last == nil) last = @""; 
NSString *fullName = [NSString stringWithFormat:@"%@ %@", first, last]; 

Kennt jemand eine bessere/mehr prägnante Art und Weise, dies zu tun?

Antwort

58

Angenommen, Sie sind in Ordnung mit firstName<space> oder <space>lastName:

NSString *fullName = [NSString stringWithFormat:@"%@ %@", 
    contact.firstName ?: @"", contact.lastName ?: @""]; 

(a ?: b ist ein GCC extension die für a ? a : b steht, ohne a zweimal auswertet.)

+0

Groß Beispiel und nützlicher Link – JSA986

+2

Sie diesen Ansatz verwenden können, ohne auf die verzichten zu müssen, nicht nur die folgende danach fullname = [fullname stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; –

7

NSString *fullName = [NSString stringWithFormat:@"%@ %@", first ? first : @"", last ? last : @""]; ist sicherlich etwas knapper, aber es hat den gleichen Fehler, den Ihr ursprünglicher Code hat, der ist, dass fullName wäre "firstName" oder "lastName" (beachten Sie die Leerzeichen), wenn das eine oder das andere nicht existierte. Daher müssen Sie Code schreiben wie

NSMutableString* fullName = [NSMutableString string]; 
if(contact.firstName) { [fullName appendString:contact.firstName]; } 
if(contact.firstName && contact.lastName) { [fullName appendString:@" "]; } 
if(contact.lastName) { [fullName appendString:contact.lastName]; } 
return fullName; 

, damit es richtig funktioniert.

1

Hier ist, wie ich accompished ...

NSString *lastName = (NSString *)ABRecordCopyValue(personRef, kABPersonLastNameProperty); 
cell.text = [NSString stringWithFormat:@"%@%@",lastName?[NSString stringWithFormat:@"%@ ",lastName]:@"",(NSString *)ABRecordCopyValue(personRef, kABPersonFirstNameProperty)?:@""]; 
0

Dies ist, wie ich es tat. Es ist nicht so kompakt wie die anderen, aber es ist besser lesbar (was mir immer am wichtigsten ist).

Es hat auch den Vorteil, nachfolgende Leerzeichen vom Anfang und vom Ende zu entfernen.

// Remove any nulls from the first or last name 
firstName = [NSString stringWithFormat:@"%@", (firstName ? firstName : @"")]; 
lastName = [NSString stringWithFormat:@"%@", (lastName ? lastName : @"")]; 

// Concat the strings 
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

// Remove any trailing whitespace 
fullName = NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
Verwandte Themen