nsstring

How to split a string with newlines

女生的网名这么多〃 提交于 2019-11-29 10:56:36
问题 I read from a csv file, and want to split the long string that I get using stringWithContentsOfFile, which is a multi line string, with individual lines representing rows in the csv file. How do I do this? 回答1: You can break the string into arrays of string and then manipulate as you want. NSArray *brokenByLines=[yourString componentsSeparatedByString:@"\n"] 回答2: Just in case anyone stumbles across this question like I did. This will work with any newline characters: NSCharacterSet *separator

NSString immutable allows to change its values?

天大地大妈咪最大 提交于 2019-11-29 10:52:51
The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1. NSString *test =[[NSString alloc] initWithString:@"TEST"]; [test release]; test = @"TEST2"; Perhaps the following example will add upon the replies from Mark and Niels and help clarify things. Immutable strings // Setup two variables to point to the same string

Objective-c convert Long and float to String

回眸只為那壹抹淺笑 提交于 2019-11-29 10:49:11
问题 I need to convert two numbers to string in Objective-C. One is a long number and the other is a float. I searched on the internet for a solution and everyone uses stringWithFormat: but I can't make it work. I try NSString *myString = [NSString stringWithFormat: @"%f", floatValue] for 12345678.1234 and get "12345678.00000" as output and NSString *myString = [NSString stringWithFormat: @"%d", longValue] Can somebody show me how to use stringWithFormat: correctly? 回答1: This article discusses how

Proper way to convert char* to NSString without initWithCString?

血红的双手。 提交于 2019-11-29 10:41:59
I'm pulling a listing of results from a SQLite3 database into a UITableView . The code where I pull text from the database is like so: char *menuNameChars = (char *) sqlite3_column_text(statement, 1); NSString *menuName = [[NSString alloc] initWithUTF8String:menuNameChars]; NSLog(@"Retrieved %s,%@ from DB.", menuNameChars, menuName); When I use initWithUTF8String , sometimes the information is copied properly from the database. Sometimes though, the information is displayed properly from the char* , but not from the NSString : 2011-10-24 22:26:54.325 [23974:207] Retrieved Cravin Chicken

How to quickly check if NSString object is a valid url?

徘徊边缘 提交于 2019-11-29 10:22:33
Help me to write the code like "if my string is a valid URL do smth" Is it possible to write this in a couple strings of code? I will assume that by URL, you are referring to a string identifying a internet resource location. If you have an idea about the format of the input string , then why not manually check if the string starts with http:// , https:// or any other scheme you need. If you expect other protocols, you can also add them to the check list (e.g. ftp:// , mailto:// , etc) if ([myString hasPrefix:@"http://"] || [myString hasPrefix:@"https://"]) { // do something } If you are

How to put a character limit on a UITextField

喜夏-厌秋 提交于 2019-11-29 10:18:42
问题 I would like to put a character limit on a UITextField but don't know how. I want it to have a maximum of 16 characters in it. How do I do this. 回答1: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [textField.text length] + [string length] - range.length; return (newLength > 12) ? NO : YES; } 来源: https://stackoverflow.com/questions/5760150/how-to-put-a-character-limit-on-a-uitextfield

NSString copy not copying?

笑着哭i 提交于 2019-11-29 10:18:11
NSString *myString = @"sample string"; NSString *newString = [myString copy]; If I set a breakpoint after these two lines, the pointer for myString is the same as the pointer for newString. WTF? Isn't NSString copy supposed to return a pointer to a new object? Or am I missing something fundamental about how copy is supposed to work? Since NSString is not mutable it might just internally increase ref count and be done with it. When you release one of those NSStrings it might just decrement ref count - standard memory management. Do you see any issues with that? Think about this: NSMutableString

Why are these two NSString pointers the same?

て烟熏妆下的殇ゞ 提交于 2019-11-29 10:17:50
When I alloc and init two NSString variables and compare their pointers, they are the same. Here's a snippet that shows this: NSString *s1 = [[NSString alloc] initWithString:@"hello world"]; NSString *s2 = [[NSString alloc] initWithString:@"hello world"]; if (s1 == s2) { NSLog(@"=="); }else { NSLog(@"!="); } Why are s1 and s2 the same? There are three things going on here: Firstly, the two identical string literals you're passing in to initWithString: will have the same address to start. This is an obvious optimization for constant data. Secondly, when you nest alloc and init with strings, the

heightForRowAtIndexPath for longer NSStrings

好久不见. 提交于 2019-11-29 09:36:50
问题 I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2 . This is how I do it for UITableViewCellStyleDefault : CGSize textSize = {300.f, 200000.0f}; CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; size.height += 30.0f; result = MAX(size.height, 44.0f); And for UITableViewCellStyleValue2 : CGSize textSize =

Localizable.strings causing plist parsing error

爱⌒轻易说出口 提交于 2019-11-29 09:19:03
I have just localized the file Localizable.strings in my Xcode project in order to localise my application to a few different languages. However, having edited each of the files in the standard "key = value" format, I receive the following parsing error, which does not specify a file making it hard to track down what it is referring to. CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary on line 10. Parsing will be abandoned. Break on _CFPropertyListMissingSemicolon to debug. I have looked through each of the *.strings files, specifically on line 10, and