nsstring

Objective-C: -[NSString wordCount]

我与影子孤独终老i 提交于 2019-11-26 23:10:36
问题 What's a simple implementation of the following NSString category method that returns the number of words in self , where words are separated by any number of consecutive spaces or newline characters? Also, the string will be less than 140 characters, so in this case, I prefer simplicity & readability at the sacrifice of a bit of performance. @interface NSString (Additions) - (NSUInteger)wordCount; @end I found the following solutions: implementation of -[NSString wordCount] implementation of

How to Convert NSInteger to a binary (string) value

柔情痞子 提交于 2019-11-26 22:54:32
问题 I am trying to figure out how to convert an NSInteger, say 56, to an NSString that is a binary representation of the original (int) value. Perhaps someone knows a formatting technique that can accept 56 and return "111000" within Objective C. Thanks All. 回答1: There's no built-in formatting operator to do that. If you wanted to convert it to a hexadecimal string, you could do: NSString *str = [NSString stringWithFormat:@"%x", theNumber]; To convert it to a binary string, you'll have to build

NSString - Unicode to ASCII equivalent

旧街凉风 提交于 2019-11-26 22:53:51
问题 I need to convert NSString in unicode to NSString in ASCII changing all local characters: Ą to A, Ś to S, Ó to O, ü to u, And so on... What is the simplest way to do it? 回答1: -[NSString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] . All of the examples you gave are handled as you want. Looks like characters with no obvious analog, such as ☃, go to '?'. 回答2: NSString *unicode = @"Chào mừng đến với Việt Nam."; NSString *standard = [unicode

NSString: newline escape in plist

爱⌒轻易说出口 提交于 2019-11-26 22:40:50
问题 I'm writing a property list to be in the resources bundle of my application. An NSString object in the plist needs to have line-breaks in it. I tried \n , but that doesn't work. What do I do to have newlines in my string in the plist? Thanks. 回答1: If you're editing the plist in Xcode's inbuild plist editor, you can press option-return to enter a line break within a string value. 回答2: I found a simpler solution: NSString *newString = [oldString stringByReplacingOccurrencesOfString:@"\\n"

Add backslash to String in Objective-c

那年仲夏 提交于 2019-11-26 22:10:21
问题 I have a problem identical to this problem here. I even want to encode the same infromation as him (it's a date/time for asp.net)... When ever I try to add a backslash i get two backslashes since I used \. Everyone in the thread above has claimed that this is a problem with NSLog and that NSString does treat \\ as a \ . I have checked this further by using a packet sniffer to examine the packets I'm sending to the webserver and I can confirm that it is transmitting a double backslash instead

How to declare a two dimensional array of string type in Objective-C?

吃可爱长大的小学妹 提交于 2019-11-26 22:06:24
How do I declare a two dimensional array of string type in Objective-C? Jesse Rusak First, you might consider using a class to hold your inner array's strings, or loading it from a plist file (in which it is easy to make an 2d array of strings). For direct declarations, you have a couple of options. If you want to use an NSArray, you'll have to manually create the structure like this: NSMutableArray *strings = [NSMutableArray array]; for(int i = 0; i < DESIRED_MAJOR_SIZE; i++) { [strings addObject: [NSMutableArray arrayWithObject:@"" count:DESIRED_MINOR_SIZE]]; } Or, using array literals, you

Check if two NSStrings are similar

我只是一个虾纸丫 提交于 2019-11-26 22:05:51
问题 I present a tricky question that I am not sure how to approach. So, I have formulated a plist containing dictionaries which contain two objects: The Country Name The Plug Size Of The Country There are only 210 countries/facts though. And, I have enabled to search through a list of many many countries, in which there might be a fact or not. But here is my problem, I am using a web service called Geonames and the user can use a search bar display controller to search for countries, and these

NSString: isEqual vs. isEqualToString

廉价感情. 提交于 2019-11-26 21:57:37
What is the difference between isEqual: and isEqualToString: ? Why are classes adding isEqualTo* methods (isEqualToArray for NSArray, isEqualToData for NSData, ...) instead of just overriding isEqual: ? Abizern isEqual: compares a string to an object, and will return NO if the object is not a string. isEqualToString: is faster if you know both objects are strings, as the documentation states: Special Considerations When you know both objects are strings, this method is a faster way to check equality than isEqual: . isEqualTo<Class> is used to provide specific checks for equality. For instance;

NSString is integer?

匆匆过客 提交于 2019-11-26 21:56:07
How to check if the content of a NSString is an integer value? Is there any readily available way? There got to be some better way then doing something like this: - (BOOL)isInteger:(NSString *)toCheck { if([toCheck intValue] != 0) { return true; } else if([toCheck isEqualToString:@"0"]) { return true; } else { return false; } } Stephen Darlington You could use the -intValue or -integerValue methods. Returns zero if the string doesn't start with an integer, which is a bit of a shame as zero is a valid value for an integer. A better option might be to use [NSScanner scanInt:] which returns a

How to add commas to number every 3 digits in Objective C?

▼魔方 西西 提交于 2019-11-26 21:31:26
If I have a number int aNum = 2000000 how do I format this so that I can display it as the NSString 2,000,000? Don't do your own number formatting. You will almost certainly not get all the edge cases right or correctly handle all possible locales. Use the NSNumberFormatter for formatting numeric data to a localized string representation. You would use the NSNumberFormatter instance method -setGroupingSeparator: to set the grouping separator to @"," (or better yet [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator] ; thanks @ntesler) and -setGroupingSize: to put a grouping