iOS关于NSCharacterSet 的用法

  1. / Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6.     [self test];  
  7.       
  8.     // Reference output...  
  9.     dumpCharacterSet( @"alphanumericCharacterSet" );  
  10.     dumpCharacterSet( @"controlCharacterSet" );  
  11.     dumpCharacterSet( @"decimalDigitCharacterSet" );  
  12.     dumpCharacterSet( @"decomposableCharacterSet" );  
  13.     dumpCharacterSet( @"illegalCharacterSet" );  
  14.     dumpCharacterSet( @"letterCharacterSet" );  
  15.     dumpCharacterSet( @"lowercaseLetterCharacterSet" );  
  16.     dumpCharacterSet( @"nonBaseCharacterSet" );  
  17.     dumpCharacterSet( @"punctuationCharacterSet" );  
  18.     dumpCharacterSet( @"uppercaseLetterCharacterSet" );  
  19.     dumpCharacterSet( @"whitespaceAndNewlineCharacterSet" );  
  20.     dumpCharacterSet( @"whitespaceCharacterSet" );  
  21. }  
  22.   
  23. - (void)test  
  24. {  
  25.     // Set up for reading testString  
  26.     NSString *testString = @"Los Angeles;8.25;0.580561574;1,Tokyo;1.9;0.643872234;1;Honolulu,0;0;0;Toronto;7.9;5.3322;3;";  
  27.     // Parse CSV with NSScanner  
  28.     NSScanner *myScanner = [NSScanner scannerWithString:testString];  
  29.       
  30.     NSString    *location;  
  31.     float        theRevenue;  
  32.     float        thePercent;  
  33.     int            theRank;  
  34.       
  35.     // Set up data delimiter using semicolon//分号  
  36.       
  37.     NSCharacterSet *CharacterSet;  
  38.       
  39.     //Returns a character set containing the characters in a given string.  
  40.     CharacterSet = [NSCharacterSet characterSetWithCharactersInString:@";,"];  
  41.       
  42.     // Double check scanner string  
  43.     NSLog (@"Scanner string\n");  
  44.       
  45.     //Returns the string with which the receiver was created or initialized.  
  46.     NSLog (@"%@",[myScanner string]);  
  47.       
  48.     // scanner loop start  
  49.     while ([myScanner isAtEnd] == NO) {  
  50.           
  51.         if ( [myScanner scanUpToCharactersFromSet:CharacterSet intoString:&location] ) {  
  52.             NSLog (@"%@",location);  
  53.         }          
  54.           
  55.         // Skipping the ; and ,delimiter  
  56.         if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL])  
  57.             ;  
  58.         // Read Revenue data up to ; delimiter and skipping  
  59.         //Scans for a float value, returning a found value by reference.  
  60.         if([myScanner scanFloat:&theRevenue])  
  61.             NSLog(@"%lf",theRevenue);  
  62.         if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL])  
  63.             ;  
  64.           
  65.         // Read Percentage data up to ; delimiter and skipping  
  66.         if([myScanner scanFloat:&thePercent])  
  67.             NSLog(@"%lf",thePercent);  
  68.         if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL])  
  69.             ;  
  70.           
  71.         // Read Ranking data up to ; delimiter and skipping  
  72.         if([myScanner scanInt:&theRank])  
  73.             NSLog(@"%i",theRank);  
  74.         if([myScanner scanString:@";" intoString:NULL] || [myScanner scanString:@"," intoString:NULL])  
  75.             ;  
  76.     }      
  77.   
  78.       
  79.       
  80. }  
  81.   
  82. void dumpCharacterSet( NSString *name )  
  83. {  
  84.     unichar idx;  
  85.     NSCharacterSet *cset = [NSCharacterSet performSelector: NSSelectorFromString(name)];  
  86.       
  87.     printf("Character set (0-127): %s\n7-Bit: ", [name UTF8String]);  
  88.       
  89.     for( idx = 0; idx < 256; idx++ )  
  90.     {  
  91.         if ( 128 == idx ) {  
  92.             printf( "\n8-Bit: " );  
  93.         }  
  94.           
  95.         //Returns a Boolean value that indicates whether a given character is in the receiver.  
  96.         if ([cset characterIsMember: idx])  
  97.         {  
  98.             //判断字符c是否为可打印字符(含空格)  
  99.             if ( isprint(idx) ) {  
  100.                 printf( "%c ", idx);  
  101.             }  
  102.             else {  
  103.                 printf( "%02x ", idx);  
  104.             }  
  105.         }  
  106.     }  
  107.     printf("\n\n");  




使用xcode4.0编译,有错误,说<p class="p1"><span class="s1">void</span> dumpCharacterSet( NSString *name )方法有冲突,但是呢,,还能运行。。。</p><p class="p1">xcode4.0的bug</p>

你可能感兴趣的:(iOS关于NSCharacterSet 的用法)