nsstring

iPhone Dev - NSString Creation

ⅰ亾dé卋堺 提交于 2019-12-20 07:48:38
问题 I'm really confused with NSStrings. Like when should I do NSString *aString = @"Hello"; of should it be: NSString *aString = [[NSString alloc] initWithString:@"Hello"]; But then its different when you're assigning a value to an NSString property isn't it? Can someone clear this up for me? Thanks!! 回答1: In general you should do the first, but they are mostly functionally the same. You can treat constant NSStrings just like normal NSString string objects, for instance: [@"Hello" length] will

How can I escape slashes and quotes in Objective-C?

与世无争的帅哥 提交于 2019-12-20 07:34:55
问题 I would like to do the following [controller setMessageBody:[NSString stringWithFormat:@"<strong>%@</strong> <br> <br> %@ <br><br> %@ <br><br> Sent From MyApp",self.articleTitle, self.articleDescription, self.articleURL] isHTML:YES]; on the last %@ I would like to do <a href="%@">Hello</a> But I am not sure how to escape it properly? 回答1: Just use @"....<a href=\"%%@\">Hello</a>..." if you put it in the format, or use @"<a href=\"%@\">Hello</a>" if you include it using %@ in the format string

Replace specific words in NSString

穿精又带淫゛_ 提交于 2019-12-20 05:43:23
问题 what is the best way to get and replace specific words in string ? for example I have NSString * currentString = @"one {two}, thing {thing} good"; now I need find each {currentWord} and apply function for it [self replaceWord:currentWord] then replace currentWord with result from function -(NSString*)replaceWord:(NSString*)currentWord; 回答1: The following example shows how you can use NSRegularExpression and enumerateMatchesInString to accomplish the task. I have just used uppercaseString as

saving images in string format (to use in xml)..not working

前提是你 提交于 2019-12-20 05:18:14
问题 i am converting image from picker into nsdata(jpeg representation) and then converting it into nsstring using the following code NSData *data=UIImageJPEGRepresentation(image,1.0); NSString *imageString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; [[NSUserDefaults standardUserDefaults] setObject:imageString forKey:@"image_name"]; and at the other end where i need to display the image the uiimage is formed as follows. NSString *imageString=[[NSString alloc] init];

Difference bewteen declaring a NSString with alloc and init and assigning with @“myString”

落爺英雄遲暮 提交于 2019-12-20 04:52:33
问题 Currently I'm troubled with a small understanding issue on Objective-C. As example following Aaron Hillegass's book, I'm wondering about assigning an NSString in the init method of a class a value like in this example (For people who know the book, this is used in the Person class of RaiseMan): - (id)init { if(![super init]) return nil; myString = @"New entry"; return self; } This string isn't allocated by me, so normally I shouldn't bother about releasing it. BUT! What happens in a setter

Difference bewteen declaring a NSString with alloc and init and assigning with @“myString”

人走茶凉 提交于 2019-12-20 04:51:10
问题 Currently I'm troubled with a small understanding issue on Objective-C. As example following Aaron Hillegass's book, I'm wondering about assigning an NSString in the init method of a class a value like in this example (For people who know the book, this is used in the Person class of RaiseMan): - (id)init { if(![super init]) return nil; myString = @"New entry"; return self; } This string isn't allocated by me, so normally I shouldn't bother about releasing it. BUT! What happens in a setter

Compare similarity of two NSStrings

拜拜、爱过 提交于 2019-12-20 04:28:06
问题 I would like to compare two NSString s. If the user types "Stonehhengge", should get a "border on answer"="almost equals", because the right answer is: "Stonhenge". I would like a percentage of how much one string equals another. If "Stonhenge" is 9 letters, and the typed text contain 8 letters from "Stonhenge", but it's not equal to "Stonhenge", for example: "Stonehange" is not equal to "Stonhenge", but it's near - I'd like to know how near a match the strings are. I only know isEqual: . If

iOS对字典里某一个key的value 进行操作

谁说胖子不能爱 提交于 2019-12-20 04:03:33
iOS对字典中相应的value进行操作 无论是数组里面的字典 还是 字典里面的字典 +(NSDictionary *)nullDic:(NSDictionary *)myDic { NSArray *keyArr = [myDic allKeys]; NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init]; for (int i = 0; i < keyArr.count; i ++) { id obj = [myDic objectForKey:keyArr[i]]; obj = [self changeType:obj]; //在这里加入判断 你想要修改的key 然后value 进行修改 if ([keyArr[i] isEqualToString:@"StartTime"]) { if ([obj isKindOfClass:[NSString class]]) { obj = [(NSString *)obj stringByReplacingOccurrencesOfString:@"T" withString:@" "]; } } else if ([keyArr[i] isEqualToString:@"EndTime"]) { if ([obj isKindOfClass:[NSString

Check if String contains any String from array

假装没事ソ 提交于 2019-12-20 03:42:17
问题 I know I can check if a string contains another string like this NSString *string = @"hello bla bla"; if ([string rangeOfString:@"bla"].location == NSNotFound) { NSLog(@"string does not contain bla"); } else { NSLog(@"string contains bla!"); } But what if I have an NSArray *arary = @[@"one",@"two", @"three", @"four"] and I wanted to check if a string contains either one of these without just loop or have a bunch of or's ( || ). So it would be something like this if (array contains one or two

Check if String contains any String from array

依然范特西╮ 提交于 2019-12-20 03:42:13
问题 I know I can check if a string contains another string like this NSString *string = @"hello bla bla"; if ([string rangeOfString:@"bla"].location == NSNotFound) { NSLog(@"string does not contain bla"); } else { NSLog(@"string contains bla!"); } But what if I have an NSArray *arary = @[@"one",@"two", @"three", @"four"] and I wanted to check if a string contains either one of these without just loop or have a bunch of or's ( || ). So it would be something like this if (array contains one or two