nsstring

Replace tab in NSString with \t

北战南征 提交于 2019-12-31 03:03:13
问题 I'm parsing json with SBJson. I receive an error "-JSONValue failed. Error is: Unescaped control character [0x09]" when parsing my json string, it has unencoded tab. How to replace this tab in my json string to \t? If i remove tab manualy, everything is good. Or how to encode this tab properly? 回答1: You can use stringByReplacingOccurrencesOfString:withString: : NSString * newJsonString = [oldJsonString stringByReplacingOccurrencesOfString:@"\t" withString:@"\\t"]; 来源: https://stackoverflow

Removing unicode and backslash escapes from NSString converted from NSData

£可爱£侵袭症+ 提交于 2019-12-31 01:49:18
问题 I am converting the response data from a web request to an NSString in the following manner: NSData *data = self.responseData; if (!data) { return nil; } NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)[self.response textEncodingName])); NSString *responseString = [[NSString alloc] initWithData:data encoding:encoding]; However the resulting string looks like this: "birthday":"04\/01\/1990", "email":"some

NSString to const char * convert with Greek characters

折月煮酒 提交于 2019-12-30 14:44:14
问题 I'm trying to convert an NSString which contains a greek word to a const char. I'm trying to convert the string with UTF-8 encoding which is for greek language and when i'm logging the char out, it has junk in it. Please a little help here.. //this is the greek word NSString *letter = textFieldLetter.text; //NSString to const char conversion for the sql query const char *cLetter = (const char *)[letter cStringUsingEncoding:NSUTF8StringEncoding]; //this contains junk! NSLog(@"letter: %s"

Is there a way to “auto detect” the encoding of a resource when loading it using stringFromContentsOfURL?

随声附和 提交于 2019-12-30 10:47:07
问题 Is there a way to "auto detect" the encoding of a resource when loading it using stringFromContentsOfURL? The current (non-depracated) method, + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; , wants a URL encoding. I've noticed that getting it wrong does make a difference for what I want to do. Is there a way to check this somehow and always get it right? (Right now I'm using UTF8.) 回答1: I'd try this function from the docs Returns a string

Show NSData as binary in a NSString

风流意气都作罢 提交于 2019-12-30 06:46:43
问题 I have a binary file (file.bin) in the resources folder, I want to read it and show it as binary. The idea its to put the binary information into a array, but, at first, I'm trying to show it in a UILabel, like that: ` NSData *databuffer; NSString *stringdata; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"bin"]; NSData *myData = [NSData dataWithContentsOfFile:filePath]; if (myData) { stringdata = [NSString stringWithFormat:@"%@",[myData description]]; labelfile

How to concatenate an nsstring url

谁说胖子不能爱 提交于 2019-12-30 06:41:06
问题 How can I concatenate an NSString URL? For example: http://isomewebsite.com/username=[someuservariable]&password=[somevariable] 回答1: Try with below code. NSString* myURLString = [NSString stringWithFormat:@"somewebsite.com/username=%@&password=%@", myUserName,myPassword]; NSURL *url = [NSURL URLWithString:myURLString]; Here is the blog tutorial will help you know more about Handling url authentication challenges accessing password protected servers 回答2: You can use the method stringWithFormat

UILabel + IRR, KRW and KHR currencies with wrong symbol

核能气质少年 提交于 2019-12-30 05:24:25
问题 I'm experiencing issues when converting decimal to currency for Korean Won, Cambodian Riel and Iranian Rial and showing the result to the UILabel text. Conversion itself passes just fine and I can see correct currency symbol at the debugger, even the NSLog prints the symbol well. If I assign this NSString instance to the UILabel text, the currency symbol is shown as a crossed box instead of the correct symbol. There is no other code between, does not matter what font I use. I tried to print ₩

UIKit's [NSString sizeWithFont:constrainedToSize:] in AppKit

霸气de小男生 提交于 2019-12-30 04:58:08
问题 Is there any equivalent method in AppKit (for Cocoa on Mac OS X) that does the same thing as UIKit's [NSString sizeWithFont:constrainedToSize:] ? If not, how could I go about getting the amount of space needed to render a particular string constrained to a width/height? Update: Below is a snippet of code I'm using that I expect would produce the results I'm after. NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize: [NSFont smallSystemFontSize]],

Are NSStrings stored on the heap or on the stack and what is a good way to initialize one

只愿长相守 提交于 2019-12-30 03:16:09
问题 I have 2 new questions: 1) Consider this line: NSString *myString = [[NSString alloc] initWithString: @"Value"]; There were two things I learned, but I would like confirmation: As I learned, the "alloc" message indicates that the instance of NSString will be stored in the "heap" memory. I understood also that primitive variables such as "chars" are stored in the "stack" memory. Does this mean that: the instance of NSString is stored in the heap memory; AND that this object has an iVar pointer

NSString with some html tags, how can I search for <img> tag and get the content of url

半世苍凉 提交于 2019-12-30 01:25:27
问题 I have a NSString with some html tags, how can I search for tag and get the content of url? I'm not sure if I must use Hpple or a simple Regex expression. In both cases can I have some example? 回答1: One way to do this is using NSScanner. See this example: NSString *url = nil; NSString *htmlString = ... NSScanner *theScanner = [NSScanner scannerWithString:htmlString]; // find start of IMG tag [theScanner scanUpToString:@"<img" intoString:nil]; if (![theScanner isAtEnd]) { [theScanner