Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an alternative, that gets all names. Also, it does not remove 's' at the end of languages that uses "de" or "'s". Also, it capitalizes the first letter of each name.</p> <p>Method implementation:</p> <pre><code>- (NSArray*) newNamesFromDeviceName: (NSString *) deviceName { NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString:@" '’\\"]; NSArray* words = [deviceName componentsSeparatedByCharactersInSet:characterSet]; NSMutableArray* names = [[NSMutableArray alloc] init]; bool foundShortWord = false; for (NSString *word in words) { if ([word length] &lt;= 2) foundShortWord = true; if ([word compare:@"iPhone"] != 0 &amp;&amp; [word compare:@"iPod"] != 0 &amp;&amp; [word compare:@"iPad"] != 0 &amp;&amp; [word length] &gt; 2) { word = [word stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[word substringToIndex:1] uppercaseString]]; [names addObject:word]; } } if (!foundShortWord &amp;&amp; [names count] &gt; 1) { int lastNameIndex = [names count] - 1; NSString* name = [names objectAtIndex:lastNameIndex]; unichar lastChar = [name characterAtIndex:[name length] - 1]; if (lastChar == 's') { [names replaceObjectAtIndex:lastNameIndex withObject:[name substringToIndex:[name length] - 1]]; } } return names; } </code></pre> <p>Usage:</p> <pre><code>// Add default values for first name and last name NSString* deviceName = [[UIDevice currentDevice] name]; NSArray* names = [self newNamesFromDeviceName:deviceName]; // This example sets the first and second names as the text property for some text boxes. [self.txtFirstName setText:[names objectAtIndex:0]]; [self.txtLastName setText:[names objectAtIndex:1]]; [names release]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload