nsmutablestring

NSString ,NSMutableString用法以及一些常用方法

你说的曾经没有我的故事 提交于 2020-03-01 03:08:22
1、NSString的用法 //1、创建常量字符串。 NSString *string1 = @"这是一个NSString对象"; //2、创建空字符串,给予赋值。实例化一个对象 NSString *string2 = [[NSString alloc] init]; NSString *string = [NSString string]; //3、综合1、2方法,提升性能:initWithString方法 NSString *string3 = [[NSString alloc] initWithString:@"This is a string3"]; NSString *string = [NSString stringWithString:@"创建一个字符串"]; // 用这种方法xcode会让你修改为创建一个字符串常量的结构 //4、用标准C语言字符串创建OC字符串:initWithCString方法(注意编码方式参数的选择) char *string4c = "This is a C String!中文也支持"; NSString *string4 = [[NSString alloc] initWithCString:string4c encoding:NSUTF8StringEncoding]; // 这种创建方式在从网上获取数据出现“乱码”时

Need help fixing memory leak - NSMutableString

对着背影说爱祢 提交于 2020-01-25 08:05:06
问题 Have been playing around with instruments with not much luck in figuring out how to solve this memory leak. Firstly the code: -(NSString *) randomizeHint:(NSString *) wordToShuffle{ NSMutableString * outputstring = [NSMutableString stringWithCapacity:[wordToShuffle length]]; NSMutableSet * usedNumberSet = [NSMutableSet setWithCapacity:[wordToShuffle length]]; for (int i=0; i<[wordToShuffle length]; i++) { int randomnum = arc4random()%[wordToShuffle length]; while ([usedNumberSet

Appending unichar to NSMutableString

二次信任 提交于 2020-01-02 02:24:07
问题 How do you append a unichar character to NSMutableString ? unichar c = 'T'; NSMutableString *s = [NSMutableString stringWithString:@"MyString"]; // want s to become "MyStringT" https://discussions.apple.com/thread/1679290 suggests: [s appendString:[NSString stringWithCharacters:&c length:1]]; Looks too long / complicated... 回答1: Use [NSString appendFormat:], so for above: [s appendFormat:@"%C", c]; Enjoy! 来源: https://stackoverflow.com/questions/29357205/appending-unichar-to-nsmutablestring

How to initialize NSString to NSMutableString?

99封情书 提交于 2019-12-29 06:57:50
问题 Is there any way to initialize NSString to NSMutableString? and also reverse order? -(void)st:(NSString *)st { NSMutableString str = st; // gives warning.. NSLog(@"string: %@", str); } 回答1: NSString is an immutable representation (or a readonly view at worst). So you would need to either cast to a NSMutableString if you know it's mutable or make a mutable copy: -(void)st:(NSString *)st { NSMutableString *str = [[st mutableCopy] autorelease]; NSLog(@"string: %@", str); } I autoreleased it

Appending to the end of a file with NSMutableString

这一生的挚爱 提交于 2019-12-27 21:24:53
问题 I have a log file that I'm trying to append data to the end of. I have an NSMutableString textToWrite variable, and I am doing the following: [textToWrite writeToFile:filepath atomically:YES encoding: NSUnicodeStringEncoding error:&err]; However, when I do this all the text inside the file is replaced with the text in textToWrite. How can I instead append to the end of the file? (Or even better, how can I append to the end of the file on a new line?) 回答1: I guess you could do a couple of

half word is going on next line created using nsmutableattributedstring

风流意气都作罢 提交于 2019-12-25 08:30:14
问题 Before I start, details_Label is UITextView . I have wrong naming because earlier it was label but I changed to UITextView for copy text and other functionalities. Below is what I have details_Label.text = [NSString stringWithFormat:@"%@", [[[detailsArray objectAtIndex:0] valueForKey:@"Details"] substringToIndex:200]]; NSMutableAttributedString *attributedString; attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", details_Label.text]

problem in copy string for other class

荒凉一梦 提交于 2019-12-25 02:19:46
问题 In my app I am sending a request to the web server and getting some results, then I am parsing this result and storing this result string into other class object string. For example: @interface GraphView : UIViewController<UITabBarControllerDelegate> { NSMutableString *graphTempString; } @property(nonatomic,retain) NSMutableString *graphTempString; @end TSUClass .m implementing NSURLConnection(), connectionDidFinishLoading(), -(void) parser:(NSXMLParser *) parser didStartElement:(NSString *)

Change text of an attributed string and retain attributes in Swift

本秂侑毒 提交于 2019-12-23 19:25:05
问题 For output in a database program, I have certain text that I've inserted marks to indicate bold or italics, as well as some text that is substituted for images. For instance: "%Important% ^All employees to the breakroom^" should have final output as: Important All employees to the breakroom I have code written to find the text with "%" signs around it and "^" signs, but the trouble I have now is the text outputs like: %Important% ^ All employees to the breakroom ^ I'd like to remove these %

Sensitive data: NSString VS NSMutableString (iPhone)

ぃ、小莉子 提交于 2019-12-19 07:10:54
问题 I have some sensitive data I want to clear directly after use. Currently, the sensitive data is in the form of NSString. NSString is in my understanding immutable, meaning that I can't really clear the data. NSMutableString seems more appropriate, though, as it is mutable and has methods like replaceCharactersInRange and deleteCharactersInRange. I have no knowledge of the implementation details so I wonder if NSMutableString would serve my purpose? 回答1: I would be afraid NSMutableString would

Append string with variable

拥有回忆 提交于 2019-12-17 23:39:34
问题 I'm a java guy coming over to Objective-C. In java, to add a variable to a string you'd have to do something along the lines of: someString = "This string is equal to " + someNumber + "."; I can't figure out how to do it in Objective-C though. I have an NSMutableString that I'd like to add to the middle of a string. How do I go about doing this? I've tried: NSString *someText = @"Lorem ipsum " + someMutableString; NSString *someText = @"Lorem ipsum " + [someMutableString stringForm]; and a