nsstring

Objective-C: -[NSString wordCount]

こ雲淡風輕ζ 提交于 2019-11-27 22:32:29
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 -[NSString wordCount] - seems a bit simpler But, isn't there a simpler way? Why not just do the

NSData to NSString with JSON response

感情迁移 提交于 2019-11-27 22:28:34
NSData* jsonData is the http response contains JSON data. NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"jsonString: %@", jsonString); I got the result: { "result": "\u8aaa" } What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"? If you convert the JSON data { "result" : "\u8aaa" } to a NSDictionary (e.g. using NSJSONSerialization ) and print the dictionary NSError *error; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@", jsonDict)

objective-c code to right pad a NSString?

若如初见. 提交于 2019-11-27 22:14:15
Can someone give a code example of how to right pad an NSString in objective-c please? For example want these strings: Testing 123 Long String Hello World Short if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give: Testing 123 xxx Hello World xxx Short xxx That is a 2nd column would like up. Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you

When not to alloc and init an NSString

末鹿安然 提交于 2019-11-27 21:54:38
问题 Whenever I need to create a new NSString variable I always alloc and init it. It seems that there are times when you don't want to do this. How do you know when to alloc and init an NSString and when not to? 回答1: Whenever I need to create a new NSString variable I always alloc and init it. No, that doesn't make sense. The variable exists from the moment the program encounters the point where you declare it: NSString *myString; This variable is not an NSString. It is storage for a pointer to

How do I draw an NSString at an angle?

别说谁变了你拦得住时间么 提交于 2019-11-27 21:42:22
Is there a set of string attributes I can specify that will draw the text at an angle when I call: [label drawAtPoint:textStart withAttributes:attributes]; Here's an example that uses a transform to rotate the drawing context. Essentially it's just like setting a color or shadow, just make sure to use -concat instead of -set . CGFloat rotateDeg = 4.0f; NSAffineTransform *rotate = [[NSAffineTransform alloc] init]; [rotate rotateByDegrees:rotateDeg]; [rotate concat]; // Lock focus if needed and draw strings, images here. [rotate release]; NSString itself doesn't have rotation, but you can rotate

Getting Optional(“”) when trying to get value from KeyChain

冷暖自知 提交于 2019-11-27 21:41:19
问题 When I try to get my keyChain value, it return a string containing: Optional("[thing in the KeyChain]") so, I tried to remove "Optional" by using a loop: var str = KeychainService.loadToken() for(var i = 0; i < 9 ; i++) { str[i] = "" } But i get a error: NSString does not have a member named 'subscript' The KeychainService class: import Foundation import Security let serviceIdentifier = "MySerivice" let userAccount = "authenticatedUser" let accessGroup = "MySerivice" // Arguments for the

Replace only the first instance of a substring in an NSString

≯℡__Kan透↙ 提交于 2019-11-27 20:24:26
So if you have an NSString that goes: @"My blue car is bigger than my blue shoes or my blue bicycle"; I would like a method that replaces only the first instance of blue with green, to produce: @"My green car is bigger than my blue shoes or my blue bicycle"; How does one do this? Jonathan Grynspan Assuming the following inputs: NSString *myString = @"My blue car is bigger then my blue shoes or my blue bicycle"; NSString *original = @"blue"; NSString *replacement = @"green"; The algorithm is quite simple: NSRange rOriginal = [myString rangeOfString:original]; if (NSNotFound != rOriginal

Convert first number in an NSString into an Integer?

匆匆过客 提交于 2019-11-27 20:19:52
I have an NSString like so: @"200hello" or @"0 something" What I would like to be able to do is take the first occuring number in the NSString and convert it into an int. So that @"200hello" would become int = 200. and @"0 something" would become int = 0. int value; BOOL success = [[NSScanner scannerWithString:@"1000safkaj"] scanInteger:&value]; If the number is not always at the beginning: NSCharacterSet* nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; int value = [[@"adfsdg1000safkaj" stringByTrimmingCharactersInSet:nonDigits] intValue]; Steve Ciarcia once said a single

How to split long NSString into pages

喜夏-厌秋 提交于 2019-11-27 20:15:11
I have a long NSString I want to display over a couple of pages. But to do this, I need to find out how much text will actually fit on the page. [NSString sizeWithFont: ...] Is not enough, it will just tell me if the text fits in the rectangle or not, if its does not, it will silently truncate the string, but it won't tell me where it truncated! I need to know the first word that does not fit on the page, so I can split the string and draw that part of it on the next page. (and repeat) Any ideas how to solve this? Only idea I have myself so far is to repeatedly call sizeWithFont

How to Convert NSInteger to a binary (string) value

烂漫一生 提交于 2019-11-27 20:14:17
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. Adam Rosenfield 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 it yourself: NSMutableString *str = [NSMutableString stringWithFormat:@""]; for(NSInteger